… | |
… | |
22 | */ |
22 | */ |
23 | |
23 | |
24 | #ifndef UTIL_H__ |
24 | #ifndef UTIL_H__ |
25 | #define UTIL_H__ |
25 | #define UTIL_H__ |
26 | |
26 | |
27 | #include <compiler.h> |
|
|
28 | |
|
|
29 | #define DEBUG_POISON 0x00 // poison memory before freeing it if != 0 |
27 | #define DEBUG_POISON 0x00 // poison memory before freeing it if != 0 |
30 | #define DEBUG_SALLOC 0 // add a debug wrapper around all sallocs |
28 | #define DEBUG_SALLOC 0 // add a debug wrapper around all sallocs |
31 | #define PREFER_MALLOC 0 // use malloc and not the slice allocator |
29 | #define PREFER_MALLOC 0 // use malloc and not the slice allocator |
32 | |
30 | |
33 | #include <pthread.h> |
31 | #include <pthread.h> |
… | |
… | |
37 | #include <new> |
35 | #include <new> |
38 | #include <vector> |
36 | #include <vector> |
39 | |
37 | |
40 | #include <glib.h> |
38 | #include <glib.h> |
41 | |
39 | |
|
|
40 | #include <flat_hash_map.hpp> |
|
|
41 | |
42 | #include <shstr.h> |
42 | #include <shstr.h> |
43 | #include <traits.h> |
43 | #include <traits.h> |
|
|
44 | |
|
|
45 | #include "ecb.h" |
44 | |
46 | |
45 | #if DEBUG_SALLOC |
47 | #if DEBUG_SALLOC |
46 | # define g_slice_alloc0(s) debug_slice_alloc0(s) |
48 | # define g_slice_alloc0(s) debug_slice_alloc0(s) |
47 | # define g_slice_alloc(s) debug_slice_alloc(s) |
49 | # define g_slice_alloc(s) debug_slice_alloc(s) |
48 | # define g_slice_free1(s,p) debug_slice_free1(s,p) |
50 | # define g_slice_free1(s,p) debug_slice_free1(s,p) |
… | |
… | |
53 | # define g_slice_alloc0(s) calloc (1, (s)) |
55 | # define g_slice_alloc0(s) calloc (1, (s)) |
54 | # define g_slice_alloc(s) malloc ((s)) |
56 | # define g_slice_alloc(s) malloc ((s)) |
55 | # define g_slice_free1(s,p) free ((p)) |
57 | # define g_slice_free1(s,p) free ((p)) |
56 | #endif |
58 | #endif |
57 | |
59 | |
58 | // use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) |
|
|
59 | #define auto(var,expr) decltype(expr) var = (expr) |
|
|
60 | |
|
|
61 | #if cplusplus_does_not_suck /* still sucks in codesize with gcc 6, although local types work now */ |
|
|
62 | // does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) |
|
|
63 | template<typename T, int N> |
|
|
64 | static inline int array_length (const T (&arr)[N]) |
|
|
65 | { |
|
|
66 | return N; |
|
|
67 | } |
|
|
68 | #else |
|
|
69 | #define array_length(name) (sizeof (name) / sizeof (name [0])) |
|
|
70 | #endif |
|
|
71 | |
|
|
72 | // very ugly macro that basically declares and initialises a variable |
60 | // very ugly macro that basically declares and initialises a variable |
73 | // that is in scope for the next statement only |
61 | // that is in scope for the next statement only |
74 | // 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 |
75 | // (note: works great for pointers) |
63 | // (note: works great for pointers) |
76 | // most ugly macro I ever wrote |
64 | // most ugly macro I ever wrote |
… | |
… | |
123 | |
111 | |
124 | // div* only work correctly for div > 0 |
112 | // div* only work correctly for div > 0 |
125 | // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) |
113 | // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) |
126 | template<typename T> static inline T div (T val, T div) |
114 | template<typename T> static inline T div (T val, T div) |
127 | { |
115 | { |
128 | 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; |
129 | } |
117 | } |
130 | |
118 | |
131 | template<> inline float div (float val, float div) { return val / div; } |
119 | template<> inline float div (float val, float div) { return val / div; } |
132 | template<> inline double div (double val, double div) { return val / div; } |
120 | template<> inline double div (double val, double div) { return val / div; } |
133 | |
121 | |
134 | // div, round-up |
122 | // div, round-up |
135 | 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) |
136 | { |
124 | { |
137 | return expect_false (val < 0) ? - ((-val ) / div) : (val + div - 1) / div; |
125 | return ecb_expect_false (val < 0) ? - ((-val ) / div) : (val + div - 1) / div; |
138 | } |
126 | } |
139 | // div, round-down |
127 | // div, round-down |
140 | 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) |
141 | { |
129 | { |
142 | return expect_false (val < 0) ? - ((-val + (div - 1) ) / div) : (val ) / div; |
130 | return ecb_expect_false (val < 0) ? - ((-val + (div - 1) ) / div) : (val ) / div; |
143 | } |
131 | } |
144 | |
132 | |
145 | // lerp* only work correctly for min_in < max_in |
133 | // lerp* only work correctly for min_in < max_in |
146 | // 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 |
147 | template<typename T> |
135 | template<typename T> |
… | |
… | |
310 | |
298 | |
311 | // for symmetry |
299 | // for symmetry |
312 | template<typename T> |
300 | template<typename T> |
313 | inline void sfree (T *ptr, int n = 1) noexcept |
301 | inline void sfree (T *ptr, int n = 1) noexcept |
314 | { |
302 | { |
315 | if (expect_true (ptr)) |
303 | if (ecb_expect_true (ptr)) |
316 | { |
304 | { |
317 | slice_alloc -= n * sizeof (T); |
305 | slice_alloc -= n * sizeof (T); |
318 | if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); |
306 | if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); |
319 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
307 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
320 | } |
308 | } |
… | |
… | |
617 | |
605 | |
618 | std::size_t operator ()(const shstr &s) const |
606 | std::size_t operator ()(const shstr &s) const |
619 | { |
607 | { |
620 | return strhsh (s); |
608 | return strhsh (s); |
621 | } |
609 | } |
|
|
610 | |
|
|
611 | typedef ska::power_of_two_hash_policy hash_policy; |
622 | }; |
612 | }; |
623 | |
613 | |
624 | struct str_equal |
614 | struct str_equal |
625 | { |
615 | { |
626 | bool operator ()(const char *a, const char *b) const |
616 | bool operator ()(const char *a, const char *b) const |