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) |
… | |
… | |
597 | // with good distribution. |
597 | // with good distribution. |
598 | // FNV-1a is faster on many cpus because the multiplication |
598 | // FNV-1a is faster on many cpus because the multiplication |
599 | // runs concurrently with the looping logic. |
599 | // runs concurrently with the looping logic. |
600 | // we modify the hash a bit to improve its distribution |
600 | // we modify the hash a bit to improve its distribution |
601 | uint32_t hash = STRHSH_NULL; |
601 | uint32_t hash = STRHSH_NULL; |
602 | |
602 | |
603 | while (*s) |
603 | while (*s) |
604 | hash = (hash ^ *s++) * 16777619U; |
604 | hash = (hash ^ *s++) * 16777619U; |
605 | |
605 | |
606 | return hash ^ (hash >> 16); |
606 | return hash ^ (hash >> 16); |
607 | } |
607 | } |
608 | |
608 | |
609 | static inline uint32_t |
609 | static inline uint32_t |
610 | memhsh (const char *s, size_t len) |
610 | memhsh (const char *s, size_t len) |
611 | { |
611 | { |
612 | uint32_t hash = STRHSH_NULL; |
612 | uint32_t hash = STRHSH_NULL; |
613 | |
613 | |
614 | while (len--) |
614 | while (len--) |
615 | hash = (hash ^ *s++) * 16777619U; |
615 | hash = (hash ^ *s++) * 16777619U; |
616 | |
616 | |
617 | return hash; |
617 | return hash; |
618 | } |
618 | } |