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.127 by root, Sat Nov 17 23:40:02 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>
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)
63template<typename T, int N>
64static 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)
126template<typename T> static inline T div (T val, T div) 114template<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
131template<> inline float div (float val, float div) { return val / div; } 119template<> inline float div (float val, float div) { return val / div; }
132template<> inline double div (double val, double div) { return val / div; } 120template<> inline double div (double val, double div) { return val / div; }
133 121
134// div, round-up 122// div, round-up
135template<typename T> static inline T div_ru (T val, T div) 123template<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
140template<typename T> static inline T div_rd (T val, T div) 128template<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
147template<typename T> 135template<typename T>
310 298
311// for symmetry 299// for symmetry
312template<typename T> 300template<typename T>
313inline void sfree (T *ptr, int n = 1) noexcept 301inline 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 }
386 sfree ((char *)p, s); 374 sfree ((char *)p, s);
387 } 375 }
388}; 376};
389 377
390// a STL-compatible allocator that uses g_slice 378// a STL-compatible allocator that uses g_slice
391// boy, this is verbose 379// boy, this is much less verbose in newer C++ versions
392template<typename Tp> 380template<typename Tp>
393struct slice_allocator 381struct slice_allocator
394{ 382{
395 typedef size_t size_type; 383 using value_type = Tp;
396 typedef ptrdiff_t difference_type;
397 typedef Tp *pointer;
398 typedef const Tp *const_pointer;
399 typedef Tp &reference;
400 typedef const Tp &const_reference;
401 typedef Tp value_type;
402
403 template <class U>
404 struct rebind
405 {
406 typedef slice_allocator<U> other;
407 };
408 384
409 slice_allocator () noexcept { } 385 slice_allocator () noexcept { }
410 slice_allocator (const slice_allocator &) noexcept { } 386 template<class U> slice_allocator (const slice_allocator<U> &) noexcept {}
411 template<typename Tp2>
412 slice_allocator (const slice_allocator<Tp2> &) noexcept { }
413 387
414 ~slice_allocator () { } 388 value_type *allocate (std::size_t n)
415
416 pointer address (reference x) const { return &x; }
417 const_pointer address (const_reference x) const { return &x; }
418
419 pointer allocate (size_type n, const_pointer = 0)
420 { 389 {
421 return salloc<Tp> (n); 390 return salloc<Tp> (n);
422 } 391 }
423 392
424 void deallocate (pointer p, size_type n) 393 void deallocate (value_type *p, std::size_t n)
425 { 394 {
426 sfree<Tp> (p, n); 395 sfree<Tp> (p, n);
427 } 396 }
428
429 size_type max_size () const noexcept
430 {
431 return size_t (-1) / sizeof (Tp);
432 }
433
434 void construct (pointer p, const Tp &val)
435 {
436 ::new (p) Tp (val);
437 }
438
439 void destroy (pointer p)
440 {
441 p->~Tp ();
442 }
443}; 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}
444 410
445// basically a memory area, but refcounted 411// basically a memory area, but refcounted
446struct refcnt_buf 412struct refcnt_buf
447{ 413{
448 char *data; 414 char *data;
617 583
618 std::size_t operator ()(const shstr &s) const 584 std::size_t operator ()(const shstr &s) const
619 { 585 {
620 return strhsh (s); 586 return strhsh (s);
621 } 587 }
588
589 typedef ska::power_of_two_hash_policy hash_policy;
622}; 590};
623 591
624struct str_equal 592struct str_equal
625{ 593{
626 bool operator ()(const char *a, const char *b) const 594 bool operator ()(const char *a, const char *b) const

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines