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.126 by root, Sat Nov 17 23:33:18 2018 UTC vs.
Revision 1.132 by root, Thu Dec 20 04:40:15 2018 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * 6 *
6 * Deliantra is free software: you can redistribute it and/or modify it under 7 * Deliantra is free software: you can redistribute it and/or modify it under
7 * the terms of the Affero GNU General Public License as published by the 8 * the terms of the Affero GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your 9 * Free Software Foundation, either version 3 of the License, or (at your
21 */ 22 */
22 23
23#ifndef UTIL_H__ 24#ifndef UTIL_H__
24#define UTIL_H__ 25#define UTIL_H__
25 26
26#include <compiler.h>
27
28#define DEBUG_POISON 0x00 // poison memory before freeing it if != 0 27#define DEBUG_POISON 0x00 // poison memory before freeing it if != 0
29#define DEBUG_SALLOC 0 // add a debug wrapper around all sallocs 28#define DEBUG_SALLOC 0 // add a debug wrapper around all sallocs
30#define PREFER_MALLOC 0 // use malloc and not the slice allocator 29#define PREFER_MALLOC 0 // use malloc and not the slice allocator
31 30
32#include <pthread.h> 31#include <pthread.h>
36#include <new> 35#include <new>
37#include <vector> 36#include <vector>
38 37
39#include <glib.h> 38#include <glib.h>
40 39
40#include <flat_hash_map.hpp>
41
41#include <shstr.h> 42#include <shstr.h>
42#include <traits.h> 43#include <traits.h>
44
45#include "ecb.h"
43 46
44#if DEBUG_SALLOC 47#if DEBUG_SALLOC
45# define g_slice_alloc0(s) debug_slice_alloc0(s) 48# define g_slice_alloc0(s) debug_slice_alloc0(s)
46# define g_slice_alloc(s) debug_slice_alloc(s) 49# define g_slice_alloc(s) debug_slice_alloc(s)
47# define g_slice_free1(s,p) debug_slice_free1(s,p) 50# define g_slice_free1(s,p) debug_slice_free1(s,p)
52# define g_slice_alloc0(s) calloc (1, (s)) 55# define g_slice_alloc0(s) calloc (1, (s))
53# define g_slice_alloc(s) malloc ((s)) 56# define g_slice_alloc(s) malloc ((s))
54# define g_slice_free1(s,p) free ((p)) 57# define g_slice_free1(s,p) free ((p))
55#endif 58#endif
56 59
57// use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever)
58#define auto(var,expr) decltype(expr) var = (expr)
59
60#if cplusplus_does_not_suck /* still sucks in codesize with gcc 6, although local types work now */
61// does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm)
62template<typename T, int N>
63static inline int array_length (const T (&arr)[N])
64{
65 return N;
66}
67#else
68#define array_length(name) (sizeof (name) / sizeof (name [0]))
69#endif
70
71// very ugly macro that basically declares and initialises a variable 60// very ugly macro that basically declares and initialises a variable
72// that is in scope for the next statement only 61// that is in scope for the next statement only
73// 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
74// (note: works great for pointers) 63// (note: works great for pointers)
75// most ugly macro I ever wrote 64// most ugly macro I ever wrote
122 111
123// div* only work correctly for div > 0 112// div* only work correctly for div > 0
124// div, with correct rounding (< 0.5 downwards, >=0.5 upwards) 113// div, with correct rounding (< 0.5 downwards, >=0.5 upwards)
125template<typename T> static inline T div (T val, T div) 114template<typename T> static inline T div (T val, T div)
126{ 115{
127 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;
128} 117}
129 118
130template<> inline float div (float val, float div) { return val / div; } 119template<> inline float div (float val, float div) { return val / div; }
131template<> inline double div (double val, double div) { return val / div; } 120template<> inline double div (double val, double div) { return val / div; }
132 121
133// div, round-up 122// div, round-up
134template<typename T> static inline T div_ru (T val, T div) 123template<typename T> static inline T div_ru (T val, T div)
135{ 124{
136 return expect_false (val < 0) ? - ((-val ) / div) : (val + div - 1) / div; 125 return ecb_expect_false (val < 0) ? - ((-val ) / div) : (val + div - 1) / div;
137} 126}
138// div, round-down 127// div, round-down
139template<typename T> static inline T div_rd (T val, T div) 128template<typename T> static inline T div_rd (T val, T div)
140{ 129{
141 return expect_false (val < 0) ? - ((-val + (div - 1) ) / div) : (val ) / div; 130 return ecb_expect_false (val < 0) ? - ((-val + (div - 1) ) / div) : (val ) / div;
142} 131}
143 132
144// lerp* only work correctly for min_in < max_in 133// lerp* only work correctly for min_in < max_in
145// 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
146template<typename T> 135template<typename T>
309 298
310// for symmetry 299// for symmetry
311template<typename T> 300template<typename T>
312inline void sfree (T *ptr, int n = 1) noexcept 301inline void sfree (T *ptr, int n = 1) noexcept
313{ 302{
314 if (expect_true (ptr)) 303 if (ecb_expect_true (ptr))
315 { 304 {
316 slice_alloc -= n * sizeof (T); 305 slice_alloc -= n * sizeof (T);
317 if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); 306 if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T));
318 g_slice_free1 (n * sizeof (T), (void *)ptr); 307 g_slice_free1 (n * sizeof (T), (void *)ptr);
319 } 308 }
385 sfree ((char *)p, s); 374 sfree ((char *)p, s);
386 } 375 }
387}; 376};
388 377
389// a STL-compatible allocator that uses g_slice 378// a STL-compatible allocator that uses g_slice
390// boy, this is verbose 379// boy, this is much less verbose in newer C++ versions
391template<typename Tp> 380template<typename Tp>
392struct slice_allocator 381struct slice_allocator
393{ 382{
394 typedef size_t size_type; 383 using value_type = Tp;
395 typedef ptrdiff_t difference_type;
396 typedef Tp *pointer;
397 typedef const Tp *const_pointer;
398 typedef Tp &reference;
399 typedef const Tp &const_reference;
400 typedef Tp value_type;
401
402 template <class U>
403 struct rebind
404 {
405 typedef slice_allocator<U> other;
406 };
407 384
408 slice_allocator () noexcept { } 385 slice_allocator () noexcept { }
409 slice_allocator (const slice_allocator &) noexcept { } 386 template<class U> slice_allocator (const slice_allocator<U> &) noexcept {}
410 template<typename Tp2>
411 slice_allocator (const slice_allocator<Tp2> &) noexcept { }
412 387
413 ~slice_allocator () { } 388 value_type *allocate (std::size_t n)
414
415 pointer address (reference x) const { return &x; }
416 const_pointer address (const_reference x) const { return &x; }
417
418 pointer allocate (size_type n, const_pointer = 0)
419 { 389 {
420 return salloc<Tp> (n); 390 return salloc<Tp> (n);
421 } 391 }
422 392
423 void deallocate (pointer p, size_type n) 393 void deallocate (value_type *p, std::size_t n)
424 { 394 {
425 sfree<Tp> (p, n); 395 sfree<Tp> (p, n);
426 } 396 }
427
428 size_type max_size () const noexcept
429 {
430 return size_t (-1) / sizeof (Tp);
431 }
432
433 void construct (pointer p, const Tp &val)
434 {
435 ::new (p) Tp (val);
436 }
437
438 void destroy (pointer p)
439 {
440 p->~Tp ();
441 }
442}; 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}
443 410
444// basically a memory area, but refcounted 411// basically a memory area, but refcounted
445struct refcnt_buf 412struct refcnt_buf
446{ 413{
447 char *data; 414 char *data;
616 583
617 std::size_t operator ()(const shstr &s) const 584 std::size_t operator ()(const shstr &s) const
618 { 585 {
619 return strhsh (s); 586 return strhsh (s);
620 } 587 }
588
589 typedef ska::power_of_two_hash_policy hash_policy;
621}; 590};
622 591
623struct str_equal 592struct str_equal
624{ 593{
625 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