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.128 by root, Tue Nov 27 18:47:35 2018 UTC vs.
Revision 1.132 by root, Thu Dec 20 04:40:15 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)
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)
65template<typename T, int N>
66static 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)
128template<typename T> static inline T div (T val, T div) 114template<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
133template<> inline float div (float val, float div) { return val / div; } 119template<> inline float div (float val, float div) { return val / div; }
134template<> inline double div (double val, double div) { return val / div; } 120template<> inline double div (double val, double div) { return val / div; }
135 121
136// div, round-up 122// div, round-up
137template<typename T> static inline T div_ru (T val, T div) 123template<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
142template<typename T> static inline T div_rd (T val, T div) 128template<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
149template<typename T> 135template<typename T>
312 298
313// for symmetry 299// for symmetry
314template<typename T> 300template<typename T>
315inline void sfree (T *ptr, int n = 1) noexcept 301inline 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 }
388 sfree ((char *)p, s); 374 sfree ((char *)p, s);
389 } 375 }
390}; 376};
391 377
392// a STL-compatible allocator that uses g_slice 378// a STL-compatible allocator that uses g_slice
393// boy, this is verbose 379// boy, this is much less verbose in newer C++ versions
394template<typename Tp> 380template<typename Tp>
395struct slice_allocator 381struct slice_allocator
396{ 382{
397 typedef size_t size_type; 383 using value_type = Tp;
398 typedef ptrdiff_t difference_type;
399 typedef Tp *pointer;
400 typedef const Tp *const_pointer;
401 typedef Tp &reference;
402 typedef const Tp &const_reference;
403 typedef Tp value_type;
404
405 template <class U>
406 struct rebind
407 {
408 typedef slice_allocator<U> other;
409 };
410 384
411 slice_allocator () noexcept { } 385 slice_allocator () noexcept { }
412 slice_allocator (const slice_allocator &) noexcept { } 386 template<class U> slice_allocator (const slice_allocator<U> &) noexcept {}
413 template<typename Tp2>
414 slice_allocator (const slice_allocator<Tp2> &) noexcept { }
415 387
416 ~slice_allocator () { } 388 value_type *allocate (std::size_t n)
417
418 pointer address (reference x) const { return &x; }
419 const_pointer address (const_reference x) const { return &x; }
420
421 pointer allocate (size_type n, const_pointer = 0)
422 { 389 {
423 return salloc<Tp> (n); 390 return salloc<Tp> (n);
424 } 391 }
425 392
426 void deallocate (pointer p, size_type n) 393 void deallocate (value_type *p, std::size_t n)
427 { 394 {
428 sfree<Tp> (p, n); 395 sfree<Tp> (p, n);
429 } 396 }
430
431 size_type max_size () const noexcept
432 {
433 return size_t (-1) / sizeof (Tp);
434 }
435
436 void construct (pointer p, const Tp &val)
437 {
438 ::new (p) Tp (val);
439 }
440
441 void destroy (pointer p)
442 {
443 p->~Tp ();
444 }
445}; 397};
398
399template<class T, class U>
400bool operator == (const slice_allocator<T> &, const slice_allocator<U> &) noexcept
401{
402 return true;
403}
404
405template<class T, class U>
406bool operator != (const slice_allocator<T> &x, const slice_allocator<U> &y) noexcept
407{
408 return !(x == y);
409}
446 410
447// basically a memory area, but refcounted 411// basically a memory area, but refcounted
448struct refcnt_buf 412struct refcnt_buf
449{ 413{
450 char *data; 414 char *data;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines