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.133 by root, Sat Oct 8 21:54:05 2022 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>
278 for (uint32_t idxvar, mask_ = mask; \ 278 for (uint32_t idxvar, mask_ = mask; \
279 mask_ && ((idxvar = ecb_ctz32 (mask_)), mask_ &= ~(1 << idxvar), 1);) 279 mask_ && ((idxvar = ecb_ctz32 (mask_)), mask_ &= ~(1 << idxvar), 1);)
280 280
281extern ssize_t slice_alloc; // statistics 281extern ssize_t slice_alloc; // statistics
282 282
283void *salloc_ (int n); 283void *salloc_ (int n) noexcept;
284void *salloc_ (int n, void *src); 284void *salloc_ (int n, void *src) noexcept;
285 285
286// strictly the same as g_slice_alloc, but never returns 0 286// strictly the same as g_slice_alloc, but never returns 0
287template<typename T> 287template<typename T>
288inline T *salloc (int n = 1) { return (T *)salloc_ (n * sizeof (T)); } 288inline T *salloc (int n = 1) { return (T *)salloc_ (n * sizeof (T)); }
289 289
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 }
374 sfree ((char *)p, s); 374 sfree ((char *)p, s);
375 } 375 }
376}; 376};
377 377
378// a STL-compatible allocator that uses g_slice 378// a STL-compatible allocator that uses g_slice
379// boy, this is verbose 379// boy, this is much less verbose in newer C++ versions
380template<typename Tp> 380template<typename Tp>
381struct slice_allocator 381struct slice_allocator
382{ 382{
383 typedef size_t size_type; 383 using value_type = Tp;
384 typedef ptrdiff_t difference_type;
385 typedef Tp *pointer;
386 typedef const Tp *const_pointer;
387 typedef Tp &reference;
388 typedef const Tp &const_reference;
389 typedef Tp value_type;
390
391 template <class U>
392 struct rebind
393 {
394 typedef slice_allocator<U> other;
395 };
396 384
397 slice_allocator () noexcept { } 385 slice_allocator () noexcept { }
398 slice_allocator (const slice_allocator &) noexcept { } 386 template<class U> slice_allocator (const slice_allocator<U> &) noexcept {}
399 template<typename Tp2>
400 slice_allocator (const slice_allocator<Tp2> &) noexcept { }
401 387
402 ~slice_allocator () { } 388 value_type *allocate (std::size_t n)
403
404 pointer address (reference x) const { return &x; }
405 const_pointer address (const_reference x) const { return &x; }
406
407 pointer allocate (size_type n, const_pointer = 0)
408 { 389 {
409 return salloc<Tp> (n); 390 return salloc<Tp> (n);
410 } 391 }
411 392
412 void deallocate (pointer p, size_type n) 393 void deallocate (value_type *p, std::size_t n)
413 { 394 {
414 sfree<Tp> (p, n); 395 sfree<Tp> (p, n);
415 } 396 }
416
417 size_type max_size () const noexcept
418 {
419 return size_t (-1) / sizeof (Tp);
420 }
421
422 void construct (pointer p, const Tp &val)
423 {
424 ::new (p) Tp (val);
425 }
426
427 void destroy (pointer p)
428 {
429 p->~Tp ();
430 }
431}; 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}
432 410
433// basically a memory area, but refcounted 411// basically a memory area, but refcounted
434struct refcnt_buf 412struct refcnt_buf
435{ 413{
436 char *data; 414 char *data;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines