… | |
… | |
20 | */ |
20 | */ |
21 | |
21 | |
22 | #ifndef UTIL_H__ |
22 | #ifndef UTIL_H__ |
23 | #define UTIL_H__ |
23 | #define UTIL_H__ |
24 | |
24 | |
25 | #define DEBUG_SALLOC 0 |
25 | #define DEBUG_POISON 0x00 // poison memory before freeing it if != 0 |
26 | #define PREFER_MALLOC 0 |
26 | #define DEBUG_SALLOC 0 // add a debug wrapper around all sallocs |
|
|
27 | #define PREFER_MALLOC 0 // use malloc and not the slice allocator |
27 | |
28 | |
28 | #if __GNUC__ >= 3 |
29 | #if __GNUC__ >= 3 |
29 | # define is_constant(c) __builtin_constant_p (c) |
30 | # define is_constant(c) __builtin_constant_p (c) |
30 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
31 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
31 | # define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
32 | # define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
… | |
… | |
65 | void *g_slice_alloc0 (unsigned long size); |
66 | void *g_slice_alloc0 (unsigned long size); |
66 | void g_slice_free1 (unsigned long size, void *ptr); |
67 | void g_slice_free1 (unsigned long size, void *ptr); |
67 | #elif PREFER_MALLOC |
68 | #elif PREFER_MALLOC |
68 | # define g_slice_alloc0(s) calloc (1, (s)) |
69 | # define g_slice_alloc0(s) calloc (1, (s)) |
69 | # define g_slice_alloc(s) malloc ((s)) |
70 | # define g_slice_alloc(s) malloc ((s)) |
70 | # define g_slice_free1(s,p) free ((s)) |
71 | # define g_slice_free1(s,p) free ((p)) |
71 | #endif |
72 | #endif |
72 | |
73 | |
73 | // use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) |
74 | // use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) |
74 | #define auto(var,expr) decltype(expr) var = (expr) |
75 | #define auto(var,expr) decltype(expr) var = (expr) |
75 | |
76 | |
… | |
… | |
217 | inline void sfree (T *ptr, int n = 1) throw () |
218 | inline void sfree (T *ptr, int n = 1) throw () |
218 | { |
219 | { |
219 | if (expect_true (ptr)) |
220 | if (expect_true (ptr)) |
220 | { |
221 | { |
221 | slice_alloc -= n * sizeof (T); |
222 | slice_alloc -= n * sizeof (T); |
|
|
223 | if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); |
222 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
224 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
223 | assert (slice_alloc >= 0);//D |
225 | assert (slice_alloc >= 0);//D |
224 | } |
226 | } |
|
|
227 | } |
|
|
228 | |
|
|
229 | // nulls the pointer |
|
|
230 | template<typename T> |
|
|
231 | inline void sfree0 (T *&ptr, int n = 1) throw () |
|
|
232 | { |
|
|
233 | sfree<T> (ptr, n); |
|
|
234 | ptr = 0; |
225 | } |
235 | } |
226 | |
236 | |
227 | // makes dynamically allocated objects zero-initialised |
237 | // makes dynamically allocated objects zero-initialised |
228 | struct zero_initialised |
238 | struct zero_initialised |
229 | { |
239 | { |
… | |
… | |
606 | #else |
616 | #else |
607 | #define SMUTEX_INITIALISER PTHREAD_MUTEX_INITIALIZER |
617 | #define SMUTEX_INITIALISER PTHREAD_MUTEX_INITIALIZER |
608 | #endif |
618 | #endif |
609 | |
619 | |
610 | #define SMUTEX(name) smutex name = SMUTEX_INITIALISER |
620 | #define SMUTEX(name) smutex name = SMUTEX_INITIALISER |
611 | #define SMUTEX_LOCK(name) pthread_mutex_lock (&(name)) |
621 | #define SMUTEX_LOCK(name) pthread_mutex_lock (&(name)) |
612 | #define SMUTEX_UNLOCK(name) pthread_mutex_unlock (&(name)) |
622 | #define SMUTEX_UNLOCK(name) pthread_mutex_unlock (&(name)) |
613 | |
623 | |
|
|
624 | typedef pthread_cond_t scond; |
|
|
625 | |
|
|
626 | #define SCOND(name) scond name = PTHREAD_COND_INITIALIZER |
|
|
627 | #define SCOND_SIGNAL(name) pthread_cond_signal (&(name)) |
|
|
628 | #define SCOND_BROADCAST(name) pthread_cond_broadcast (&(name)) |
|
|
629 | #define SCOND_WAIT(name,mutex) pthread_cond_wait (&(name), &(mutex)) |
|
|
630 | |
614 | #endif |
631 | #endif |
615 | |
632 | |