… | |
… | |
55 | # define g_slice_alloc0(s) calloc (1, (s)) |
55 | # define g_slice_alloc0(s) calloc (1, (s)) |
56 | # define g_slice_alloc(s) malloc ((s)) |
56 | # define g_slice_alloc(s) malloc ((s)) |
57 | # define g_slice_free1(s,p) free ((p)) |
57 | # define g_slice_free1(s,p) free ((p)) |
58 | #endif |
58 | #endif |
59 | |
59 | |
60 | // use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) |
|
|
61 | #define auto(var,expr) decltype(expr) var = (expr) |
|
|
62 | |
|
|
63 | #if cplusplus_does_not_suck /* still sucks in codesize with gcc 6, although local types work now */ |
|
|
64 | // does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) |
|
|
65 | template<typename T, int N> |
|
|
66 | static inline int array_length (const T (&arr)[N]) |
|
|
67 | { |
|
|
68 | return N; |
|
|
69 | } |
|
|
70 | #else |
|
|
71 | #define array_length(name) (sizeof (name) / sizeof (name [0])) |
|
|
72 | #endif |
|
|
73 | |
|
|
74 | // very ugly macro that basically declares and initialises a variable |
60 | // very ugly macro that basically declares and initialises a variable |
75 | // that is in scope for the next statement only |
61 | // that is in scope for the next statement only |
76 | // works only for stuff that can be assigned 0 and converts to false |
62 | // works only for stuff that can be assigned 0 and converts to false |
77 | // (note: works great for pointers) |
63 | // (note: works great for pointers) |
78 | // most ugly macro I ever wrote |
64 | // most ugly macro I ever wrote |
… | |
… | |
125 | |
111 | |
126 | // div* only work correctly for div > 0 |
112 | // div* only work correctly for div > 0 |
127 | // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) |
113 | // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) |
128 | template<typename T> static inline T div (T val, T div) |
114 | template<typename T> static inline T div (T val, T div) |
129 | { |
115 | { |
130 | return expect_false (val < 0) ? - ((-val + (div - 1) / 2) / div) : (val + div / 2) / div; |
116 | return ecb_expect_false (val < 0) ? - ((-val + (div - 1) / 2) / div) : (val + div / 2) / div; |
131 | } |
117 | } |
132 | |
118 | |
133 | template<> inline float div (float val, float div) { return val / div; } |
119 | template<> inline float div (float val, float div) { return val / div; } |
134 | template<> inline double div (double val, double div) { return val / div; } |
120 | template<> inline double div (double val, double div) { return val / div; } |
135 | |
121 | |
136 | // div, round-up |
122 | // div, round-up |
137 | template<typename T> static inline T div_ru (T val, T div) |
123 | template<typename T> static inline T div_ru (T val, T div) |
138 | { |
124 | { |
139 | return expect_false (val < 0) ? - ((-val ) / div) : (val + div - 1) / div; |
125 | return ecb_expect_false (val < 0) ? - ((-val ) / div) : (val + div - 1) / div; |
140 | } |
126 | } |
141 | // div, round-down |
127 | // div, round-down |
142 | template<typename T> static inline T div_rd (T val, T div) |
128 | template<typename T> static inline T div_rd (T val, T div) |
143 | { |
129 | { |
144 | return expect_false (val < 0) ? - ((-val + (div - 1) ) / div) : (val ) / div; |
130 | return ecb_expect_false (val < 0) ? - ((-val + (div - 1) ) / div) : (val ) / div; |
145 | } |
131 | } |
146 | |
132 | |
147 | // lerp* only work correctly for min_in < max_in |
133 | // lerp* only work correctly for min_in < max_in |
148 | // Linear intERPolate, scales val from min_in..max_in to min_out..max_out |
134 | // Linear intERPolate, scales val from min_in..max_in to min_out..max_out |
149 | template<typename T> |
135 | template<typename T> |
… | |
… | |
312 | |
298 | |
313 | // for symmetry |
299 | // for symmetry |
314 | template<typename T> |
300 | template<typename T> |
315 | inline void sfree (T *ptr, int n = 1) noexcept |
301 | inline void sfree (T *ptr, int n = 1) noexcept |
316 | { |
302 | { |
317 | if (expect_true (ptr)) |
303 | if (ecb_expect_true (ptr)) |
318 | { |
304 | { |
319 | slice_alloc -= n * sizeof (T); |
305 | slice_alloc -= n * sizeof (T); |
320 | if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); |
306 | if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); |
321 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
307 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
322 | } |
308 | } |