Compare commits
3 commits
00b1bd30f4
...
77b3f80ccc
Author | SHA1 | Date | |
---|---|---|---|
Bruno BELANYI | 77b3f80ccc | ||
Bruno BELANYI | 8259ab1614 | ||
Bruno BELANYI | c4a3886504 |
32
c/ex3.c
Normal file
32
c/ex3.c
Normal file
|
@ -0,0 +1,32 @@
|
|||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
static unsigned char reverse_byte(unsigned char c) {
|
||||
unsigned char res = 0;
|
||||
for (size_t i = 0; i < CHAR_BIT; ++i) {
|
||||
unsigned char bit = (c & (1 << i));
|
||||
res |= bit << (CHAR_BIT - i - 1);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static void swap_bytes(unsigned char* lhs, unsigned char* rhs) {
|
||||
unsigned char tmp = *lhs;
|
||||
*lhs = *rhs;
|
||||
*rhs = tmp;
|
||||
}
|
||||
|
||||
void reverse_bytes(unsigned char* buf, size_t n) {
|
||||
if (!buf || !n)
|
||||
return;
|
||||
|
||||
// No need to worry about applying reverse_byte twice in the middle by
|
||||
// mistake if done in a preliminary pass
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
buf[i] = reverse_byte(buf[i]);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < ((n + 1) / 2); ++i) {
|
||||
swap_bytes(&buf[i], &buf[n - 1 - i]);
|
||||
}
|
||||
}
|
28
cpp/ex1.cc
Normal file
28
cpp/ex1.cc
Normal file
|
@ -0,0 +1,28 @@
|
|||
#ifdef USE_ATOMIC_N
|
||||
#include <atomic>
|
||||
#endif
|
||||
|
||||
class CountInstances {
|
||||
public:
|
||||
CountInstances() {
|
||||
++n_instances_;
|
||||
}
|
||||
|
||||
~CountInstances() {
|
||||
--n_instances_;
|
||||
}
|
||||
|
||||
int nobjs() const {
|
||||
return n_instances_; // If using atomics, load could use a more relaxed
|
||||
// memory order, such as memory_order_acquire
|
||||
}
|
||||
|
||||
private:
|
||||
#ifdef USE_ATOMIC_N
|
||||
// Can be used in a multi-thread program
|
||||
static atomic<int> n_instances_;
|
||||
#else
|
||||
// Using a bare int mean races if used in a multi-threaded context
|
||||
static int n_instances_;
|
||||
#endif
|
||||
};
|
1
cpp/ex2.cc
Symbolic link
1
cpp/ex2.cc
Symbolic link
|
@ -0,0 +1 @@
|
|||
../c/ex1.c
|
Loading…
Reference in a new issue