1 | #ifndef UTIL_H__ |
1 | #ifndef UTIL_H__ |
2 | #define UTIL_H__ |
2 | #define UTIL_H__ |
|
|
3 | |
|
|
4 | //#define PREFER_MALLOC |
3 | |
5 | |
4 | #if __GNUC__ >= 3 |
6 | #if __GNUC__ >= 3 |
5 | # define is_constant(c) __builtin_constant_p (c) |
7 | # define is_constant(c) __builtin_constant_p (c) |
6 | #else |
8 | #else |
7 | # define is_constant(c) 0 |
9 | # define is_constant(c) 0 |
… | |
… | |
35 | #define IN_RANGE_EXC(val,beg,end) \ |
37 | #define IN_RANGE_EXC(val,beg,end) \ |
36 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
38 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
37 | |
39 | |
38 | void fork_abort (const char *msg); |
40 | void fork_abort (const char *msg); |
39 | |
41 | |
|
|
42 | // rationale for using (U) not (T) is to reduce signed/unsigned issues, |
|
|
43 | // as a is often a constant while b is the variable. it is still a bug, though. |
40 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
44 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
41 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
45 | template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
42 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } |
46 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? (T)a : v >(T)b ? (T)b : v; } |
43 | |
47 | |
44 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
48 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
45 | |
49 | |
46 | // this is much faster than crossfires original algorithm |
50 | // this is much faster than crossfires original algorithm |
47 | // on modern cpus |
51 | // on modern cpus |
… | |
… | |
131 | |
135 | |
132 | // for symmetry |
136 | // for symmetry |
133 | template<typename T> |
137 | template<typename T> |
134 | inline void sfree (T *ptr, int n = 1) throw () |
138 | inline void sfree (T *ptr, int n = 1) throw () |
135 | { |
139 | { |
|
|
140 | #ifdef PREFER_MALLOC |
|
|
141 | free (ptr); |
|
|
142 | #else |
136 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
143 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
|
|
144 | #endif |
137 | } |
145 | } |
138 | |
146 | |
139 | // a STL-compatible allocator that uses g_slice |
147 | // a STL-compatible allocator that uses g_slice |
140 | // boy, this is verbose |
148 | // boy, this is verbose |
141 | template<typename Tp> |
149 | template<typename Tp> |