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 (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / 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 | * |
5 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
6 | * 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 |
7 | * 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 |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * option) any later version. |
9 | * option) any later version. |
… | |
… | |
55 | #endif |
55 | #endif |
56 | |
56 | |
57 | // use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) |
57 | // use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) |
58 | #define auto(var,expr) decltype(expr) var = (expr) |
58 | #define auto(var,expr) decltype(expr) var = (expr) |
59 | |
59 | |
60 | #if cplusplus_does_not_suck |
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) |
61 | // does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) |
62 | template<typename T, int N> |
62 | template<typename T, int N> |
63 | static inline int array_length (const T (&arr)[N]) |
63 | static inline int array_length (const T (&arr)[N]) |
64 | { |
64 | { |
65 | return N; |
65 | return N; |
… | |
… | |
237 | #if 0 |
237 | #if 0 |
238 | // and has a max. error of 6 in the range -100..+100. |
238 | // and has a max. error of 6 in the range -100..+100. |
239 | #else |
239 | #else |
240 | // and has a max. error of 9 in the range -100..+100. |
240 | // and has a max. error of 9 in the range -100..+100. |
241 | #endif |
241 | #endif |
242 | inline int |
242 | inline int |
243 | idistance (int dx, int dy) |
243 | idistance (int dx, int dy) |
244 | { |
244 | { |
245 | unsigned int dx_ = abs (dx); |
245 | unsigned int dx_ = abs (dx); |
246 | unsigned int dy_ = abs (dy); |
246 | unsigned int dy_ = abs (dy); |
247 | |
247 | |
248 | #if 0 |
248 | #if 0 |
249 | return dx_ > dy_ |
249 | return dx_ > dy_ |
… | |
… | |
299 | for (uint32_t idxvar, mask_ = mask; \ |
299 | for (uint32_t idxvar, mask_ = mask; \ |
300 | mask_ && ((idxvar = least_significant_bit (mask_)), mask_ &= ~(1 << idxvar), 1);) |
300 | mask_ && ((idxvar = least_significant_bit (mask_)), mask_ &= ~(1 << idxvar), 1);) |
301 | |
301 | |
302 | extern ssize_t slice_alloc; // statistics |
302 | extern ssize_t slice_alloc; // statistics |
303 | |
303 | |
304 | void *salloc_ (int n) throw (std::bad_alloc); |
304 | void *salloc_ (int n); |
305 | void *salloc_ (int n, void *src) throw (std::bad_alloc); |
305 | void *salloc_ (int n, void *src); |
306 | |
306 | |
307 | // strictly the same as g_slice_alloc, but never returns 0 |
307 | // strictly the same as g_slice_alloc, but never returns 0 |
308 | template<typename T> |
308 | template<typename T> |
309 | inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } |
309 | inline T *salloc (int n = 1) { return (T *)salloc_ (n * sizeof (T)); } |
310 | |
310 | |
311 | // also copies src into the new area, like "memdup" |
311 | // also copies src into the new area, like "memdup" |
312 | // if src is 0, clears the memory |
312 | // if src is 0, clears the memory |
313 | template<typename T> |
313 | template<typename T> |
314 | inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
314 | inline T *salloc (int n, T *src) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
315 | |
315 | |
316 | // clears the memory |
316 | // clears the memory |
317 | template<typename T> |
317 | template<typename T> |
318 | inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } |
318 | inline T *salloc0(int n = 1) { return (T *)salloc_ (n * sizeof (T), 0); } |
319 | |
319 | |
320 | // for symmetry |
320 | // for symmetry |
321 | template<typename T> |
321 | template<typename T> |
322 | inline void sfree (T *ptr, int n = 1) throw () |
322 | inline void sfree (T *ptr, int n = 1) noexcept |
323 | { |
323 | { |
324 | if (expect_true (ptr)) |
324 | if (expect_true (ptr)) |
325 | { |
325 | { |
326 | slice_alloc -= n * sizeof (T); |
326 | slice_alloc -= n * sizeof (T); |
327 | if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); |
327 | if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); |
… | |
… | |
329 | } |
329 | } |
330 | } |
330 | } |
331 | |
331 | |
332 | // nulls the pointer |
332 | // nulls the pointer |
333 | template<typename T> |
333 | template<typename T> |
334 | inline void sfree0 (T *&ptr, int n = 1) throw () |
334 | inline void sfree0 (T *&ptr, int n = 1) noexcept |
335 | { |
335 | { |
336 | sfree<T> (ptr, n); |
336 | sfree<T> (ptr, n); |
337 | ptr = 0; |
337 | ptr = 0; |
338 | } |
338 | } |
339 | |
339 | |
… | |
… | |
407 | typedef const Tp *const_pointer; |
407 | typedef const Tp *const_pointer; |
408 | typedef Tp &reference; |
408 | typedef Tp &reference; |
409 | typedef const Tp &const_reference; |
409 | typedef const Tp &const_reference; |
410 | typedef Tp value_type; |
410 | typedef Tp value_type; |
411 | |
411 | |
412 | template <class U> |
412 | template <class U> |
413 | struct rebind |
413 | struct rebind |
414 | { |
414 | { |
415 | typedef slice_allocator<U> other; |
415 | typedef slice_allocator<U> other; |
416 | }; |
416 | }; |
417 | |
417 | |
418 | slice_allocator () throw () { } |
418 | slice_allocator () noexcept { } |
419 | slice_allocator (const slice_allocator &) throw () { } |
419 | slice_allocator (const slice_allocator &) noexcept { } |
420 | template<typename Tp2> |
420 | template<typename Tp2> |
421 | slice_allocator (const slice_allocator<Tp2> &) throw () { } |
421 | slice_allocator (const slice_allocator<Tp2> &) noexcept { } |
422 | |
422 | |
423 | ~slice_allocator () { } |
423 | ~slice_allocator () { } |
424 | |
424 | |
425 | pointer address (reference x) const { return &x; } |
425 | pointer address (reference x) const { return &x; } |
426 | const_pointer address (const_reference x) const { return &x; } |
426 | const_pointer address (const_reference x) const { return &x; } |
… | |
… | |
433 | void deallocate (pointer p, size_type n) |
433 | void deallocate (pointer p, size_type n) |
434 | { |
434 | { |
435 | sfree<Tp> (p, n); |
435 | sfree<Tp> (p, n); |
436 | } |
436 | } |
437 | |
437 | |
438 | size_type max_size () const throw () |
438 | size_type max_size () const noexcept |
439 | { |
439 | { |
440 | return size_t (-1) / sizeof (Tp); |
440 | return size_t (-1) / sizeof (Tp); |
441 | } |
441 | } |
442 | |
442 | |
443 | void construct (pointer p, const Tp &val) |
443 | void construct (pointer p, const Tp &val) |
… | |
… | |
460 | refcnt_buf (void *data, size_t size); |
460 | refcnt_buf (void *data, size_t size); |
461 | |
461 | |
462 | refcnt_buf (const refcnt_buf &src) |
462 | refcnt_buf (const refcnt_buf &src) |
463 | { |
463 | { |
464 | data = src.data; |
464 | data = src.data; |
465 | ++_refcnt (); |
465 | inc (); |
466 | } |
466 | } |
467 | |
467 | |
468 | ~refcnt_buf (); |
468 | ~refcnt_buf (); |
469 | |
469 | |
470 | refcnt_buf &operator =(const refcnt_buf &src); |
470 | refcnt_buf &operator =(const refcnt_buf &src); |
… | |
… | |
479 | return _size (); |
479 | return _size (); |
480 | } |
480 | } |
481 | |
481 | |
482 | protected: |
482 | protected: |
483 | enum { |
483 | enum { |
484 | overhead = sizeof (unsigned int) * 2 |
484 | overhead = sizeof (uint32_t) * 2 |
485 | }; |
485 | }; |
486 | |
486 | |
487 | unsigned int &_size () const |
487 | uint32_t &_size () const |
488 | { |
488 | { |
489 | return ((unsigned int *)data)[-2]; |
489 | return ((unsigned int *)data)[-2]; |
490 | } |
490 | } |
491 | |
491 | |
492 | unsigned int &_refcnt () const |
492 | uint32_t &_refcnt () const |
493 | { |
493 | { |
494 | return ((unsigned int *)data)[-1]; |
494 | return ((unsigned int *)data)[-1]; |
495 | } |
495 | } |
496 | |
496 | |
497 | void _alloc (unsigned int size) |
497 | void _alloc (uint32_t size) |
498 | { |
498 | { |
499 | data = ((char *)salloc<char> (size + overhead)) + overhead; |
499 | data = ((char *)salloc<char> (size + overhead)) + overhead; |
500 | _size () = size; |
500 | _size () = size; |
501 | _refcnt () = 1; |
501 | _refcnt () = 1; |
502 | } |
502 | } |
503 | |
503 | |
|
|
504 | void _dealloc (); |
|
|
505 | |
|
|
506 | void inc () |
|
|
507 | { |
|
|
508 | ++_refcnt (); |
|
|
509 | } |
|
|
510 | |
504 | void dec () |
511 | void dec () |
505 | { |
512 | { |
506 | if (!--_refcnt ()) |
513 | if (!--_refcnt ()) |
507 | sfree<char> (data - overhead, size () + overhead); |
514 | _dealloc (); |
508 | } |
515 | } |
509 | }; |
516 | }; |
510 | |
517 | |
511 | INTERFACE_CLASS (attachable) |
518 | INTERFACE_CLASS (attachable) |
512 | struct refcnt_base |
519 | struct refcnt_base |
… | |
… | |
590 | // with good distribution. |
597 | // with good distribution. |
591 | // FNV-1a is faster on many cpus because the multiplication |
598 | // FNV-1a is faster on many cpus because the multiplication |
592 | // runs concurrently with the looping logic. |
599 | // runs concurrently with the looping logic. |
593 | // we modify the hash a bit to improve its distribution |
600 | // we modify the hash a bit to improve its distribution |
594 | uint32_t hash = STRHSH_NULL; |
601 | uint32_t hash = STRHSH_NULL; |
595 | |
602 | |
596 | while (*s) |
603 | while (*s) |
597 | hash = (hash ^ *s++) * 16777619U; |
604 | hash = (hash ^ *s++) * 16777619U; |
598 | |
605 | |
599 | return hash ^ (hash >> 16); |
606 | return hash ^ (hash >> 16); |
600 | } |
607 | } |
601 | |
608 | |
602 | static inline uint32_t |
609 | static inline uint32_t |
603 | memhsh (const char *s, size_t len) |
610 | memhsh (const char *s, size_t len) |
604 | { |
611 | { |
605 | uint32_t hash = STRHSH_NULL; |
612 | uint32_t hash = STRHSH_NULL; |
606 | |
613 | |
607 | while (len--) |
614 | while (len--) |
608 | hash = (hash ^ *s++) * 16777619U; |
615 | hash = (hash ^ *s++) * 16777619U; |
609 | |
616 | |
610 | return hash; |
617 | return hash; |
611 | } |
618 | } |