… | |
… | |
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 | } |
225 | } |
227 | } |
226 | |
228 | |
… | |
… | |
606 | #else |
608 | #else |
607 | #define SMUTEX_INITIALISER PTHREAD_MUTEX_INITIALIZER |
609 | #define SMUTEX_INITIALISER PTHREAD_MUTEX_INITIALIZER |
608 | #endif |
610 | #endif |
609 | |
611 | |
610 | #define SMUTEX(name) smutex name = SMUTEX_INITIALISER |
612 | #define SMUTEX(name) smutex name = SMUTEX_INITIALISER |
611 | #define SMUTEX_LOCK(name) pthread_mutex_lock (&(name)) |
613 | #define SMUTEX_LOCK(name) pthread_mutex_lock (&(name)) |
612 | #define SMUTEX_UNLOCK(name) pthread_mutex_unlock (&(name)) |
614 | #define SMUTEX_UNLOCK(name) pthread_mutex_unlock (&(name)) |
613 | |
615 | |
|
|
616 | typedef pthread_cond_t scond; |
|
|
617 | |
|
|
618 | #define SCOND(name) scond name = PTHREAD_COND_INITIALIZER |
|
|
619 | #define SCOND_SIGNAL(name) pthread_cond_signal (&(name)) |
|
|
620 | #define SCOND_BROADCAST(name) pthread_cond_broadcast (&(name)) |
|
|
621 | #define SCOND_WAIT(name,mutex) pthread_cond_wait (&(name), &(mutex)) |
|
|
622 | |
614 | #endif |
623 | #endif |
615 | |
624 | |