ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
(Generate patch)

Comparing deliantra/server/include/util.h (file contents):
Revision 1.129 by root, Sat Dec 1 20:22:13 2018 UTC vs.
Revision 1.131 by root, Wed Dec 5 21:18:37 2018 UTC

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>
41 39
42#include <flat_hash_map.hpp> 40#include <flat_hash_map.hpp>
43 41
44#include <shstr.h> 42#include <shstr.h>
45#include <traits.h> 43#include <traits.h>
44
45#include "ecb.h"
46 46
47#if DEBUG_SALLOC 47#if DEBUG_SALLOC
48# define g_slice_alloc0(s) debug_slice_alloc0(s) 48# define g_slice_alloc0(s) debug_slice_alloc0(s)
49# define g_slice_alloc(s) debug_slice_alloc(s) 49# define g_slice_alloc(s) debug_slice_alloc(s)
50# define g_slice_free1(s,p) debug_slice_free1(s,p) 50# define g_slice_free1(s,p) debug_slice_free1(s,p)
111 111
112// div* only work correctly for div > 0 112// div* only work correctly for div > 0
113// div, with correct rounding (< 0.5 downwards, >=0.5 upwards) 113// div, with correct rounding (< 0.5 downwards, >=0.5 upwards)
114template<typename T> static inline T div (T val, T div) 114template<typename T> static inline T div (T val, T div)
115{ 115{
116 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;
117} 117}
118 118
119template<> inline float div (float val, float div) { return val / div; } 119template<> inline float div (float val, float div) { return val / div; }
120template<> inline double div (double val, double div) { return val / div; } 120template<> inline double div (double val, double div) { return val / div; }
121 121
122// div, round-up 122// div, round-up
123template<typename T> static inline T div_ru (T val, T div) 123template<typename T> static inline T div_ru (T val, T div)
124{ 124{
125 return expect_false (val < 0) ? - ((-val ) / div) : (val + div - 1) / div; 125 return ecb_expect_false (val < 0) ? - ((-val ) / div) : (val + div - 1) / div;
126} 126}
127// div, round-down 127// div, round-down
128template<typename T> static inline T div_rd (T val, T div) 128template<typename T> static inline T div_rd (T val, T div)
129{ 129{
130 return expect_false (val < 0) ? - ((-val + (div - 1) ) / div) : (val ) / div; 130 return ecb_expect_false (val < 0) ? - ((-val + (div - 1) ) / div) : (val ) / div;
131} 131}
132 132
133// lerp* only work correctly for min_in < max_in 133// lerp* only work correctly for min_in < max_in
134// 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
135template<typename T> 135template<typename T>
298 298
299// for symmetry 299// for symmetry
300template<typename T> 300template<typename T>
301inline void sfree (T *ptr, int n = 1) noexcept 301inline void sfree (T *ptr, int n = 1) noexcept
302{ 302{
303 if (expect_true (ptr)) 303 if (ecb_expect_true (ptr))
304 { 304 {
305 slice_alloc -= n * sizeof (T); 305 slice_alloc -= n * sizeof (T);
306 if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); 306 if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T));
307 g_slice_free1 (n * sizeof (T), (void *)ptr); 307 g_slice_free1 (n * sizeof (T), (void *)ptr);
308 } 308 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines