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.130 by root, Wed Dec 5 19:03:27 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)
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
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