--- deliantra/server/include/util.h 2018/11/17 23:33:18 1.126 +++ deliantra/server/include/util.h 2018/12/20 04:40:15 1.132 @@ -1,6 +1,7 @@ /* * This file is part of Deliantra, the Roguelike Realtime MMORPG. * + * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team * * Deliantra is free software: you can redistribute it and/or modify it under @@ -23,8 +24,6 @@ #ifndef UTIL_H__ #define UTIL_H__ -#include - #define DEBUG_POISON 0x00 // poison memory before freeing it if != 0 #define DEBUG_SALLOC 0 // add a debug wrapper around all sallocs #define PREFER_MALLOC 0 // use malloc and not the slice allocator @@ -38,9 +37,13 @@ #include +#include + #include #include +#include "ecb.h" + #if DEBUG_SALLOC # define g_slice_alloc0(s) debug_slice_alloc0(s) # define g_slice_alloc(s) debug_slice_alloc(s) @@ -54,20 +57,6 @@ # define g_slice_free1(s,p) free ((p)) #endif -// use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) -#define auto(var,expr) decltype(expr) var = (expr) - -#if cplusplus_does_not_suck /* still sucks in codesize with gcc 6, although local types work now */ -// does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) -template -static inline int array_length (const T (&arr)[N]) -{ - return N; -} -#else -#define array_length(name) (sizeof (name) / sizeof (name [0])) -#endif - // very ugly macro that basically declares and initialises a variable // that is in scope for the next statement only // works only for stuff that can be assigned 0 and converts to false @@ -124,7 +113,7 @@ // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) template static inline T div (T val, T div) { - return expect_false (val < 0) ? - ((-val + (div - 1) / 2) / div) : (val + div / 2) / div; + return ecb_expect_false (val < 0) ? - ((-val + (div - 1) / 2) / div) : (val + div / 2) / div; } template<> inline float div (float val, float div) { return val / div; } @@ -133,12 +122,12 @@ // div, round-up template static inline T div_ru (T val, T div) { - return expect_false (val < 0) ? - ((-val ) / div) : (val + div - 1) / div; + return ecb_expect_false (val < 0) ? - ((-val ) / div) : (val + div - 1) / div; } // div, round-down template static inline T div_rd (T val, T div) { - return expect_false (val < 0) ? - ((-val + (div - 1) ) / div) : (val ) / div; + return ecb_expect_false (val < 0) ? - ((-val + (div - 1) ) / div) : (val ) / div; } // lerp* only work correctly for min_in < max_in @@ -311,7 +300,7 @@ template inline void sfree (T *ptr, int n = 1) noexcept { - if (expect_true (ptr)) + if (ecb_expect_true (ptr)) { slice_alloc -= n * sizeof (T); if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); @@ -387,59 +376,37 @@ }; // a STL-compatible allocator that uses g_slice -// boy, this is verbose +// boy, this is much less verbose in newer C++ versions template struct slice_allocator { - typedef size_t size_type; - typedef ptrdiff_t difference_type; - typedef Tp *pointer; - typedef const Tp *const_pointer; - typedef Tp &reference; - typedef const Tp &const_reference; - typedef Tp value_type; - - template - struct rebind - { - typedef slice_allocator other; - }; + using value_type = Tp; slice_allocator () noexcept { } - slice_allocator (const slice_allocator &) noexcept { } - template - slice_allocator (const slice_allocator &) noexcept { } - - ~slice_allocator () { } - - pointer address (reference x) const { return &x; } - const_pointer address (const_reference x) const { return &x; } + template slice_allocator (const slice_allocator &) noexcept {} - pointer allocate (size_type n, const_pointer = 0) + value_type *allocate (std::size_t n) { return salloc (n); } - void deallocate (pointer p, size_type n) + void deallocate (value_type *p, std::size_t n) { sfree (p, n); } +}; - size_type max_size () const noexcept - { - return size_t (-1) / sizeof (Tp); - } - - void construct (pointer p, const Tp &val) - { - ::new (p) Tp (val); - } +template +bool operator == (const slice_allocator &, const slice_allocator &) noexcept +{ + return true; +} - void destroy (pointer p) - { - p->~Tp (); - } -}; +template +bool operator != (const slice_allocator &x, const slice_allocator &y) noexcept +{ + return !(x == y); +} // basically a memory area, but refcounted struct refcnt_buf @@ -618,6 +585,8 @@ { return strhsh (s); } + + typedef ska::power_of_two_hash_policy hash_policy; }; struct str_equal