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.123 by root, Wed Nov 16 23:42:01 2016 UTC vs.
Revision 1.126 by root, Sat Nov 17 23:33:18 2018 UTC

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)
62template<typename T, int N> 62template<typename T, int N>
63static inline int array_length (const T (&arr)[N]) 63static inline int array_length (const T (&arr)[N])
64{ 64{
65 return N; 65 return N;
81 81
82// in range excluding end 82// in range excluding end
83#define IN_RANGE_EXC(val,beg,end) \ 83#define IN_RANGE_EXC(val,beg,end) \
84 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) 84 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
85 85
86void cleanup (const char *cause, bool make_core = false); 86ecb_cold void cleanup (const char *cause, bool make_core = false);
87void fork_abort (const char *msg); 87ecb_cold void fork_abort (const char *msg);
88 88
89// rationale for using (U) not (T) is to reduce signed/unsigned issues, 89// rationale for using (U) not (T) is to reduce signed/unsigned issues,
90// as a is often a constant while b is the variable. it is still a bug, though. 90// as a is often a constant while b is the variable. it is still a bug, though.
91template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } 91template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
92template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } 92template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
283absdir (int d) 283absdir (int d)
284{ 284{
285 return ((d - 1) & 7) + 1; 285 return ((d - 1) & 7) + 1;
286} 286}
287 287
288// avoid ctz name because netbsd or freebsd spams it's namespace with it
289#if GCC_VERSION(3,4)
290static inline int least_significant_bit (uint32_t x)
291{
292 return __builtin_ctz (x);
293}
294#else
295int least_significant_bit (uint32_t x);
296#endif
297
298#define for_all_bits_sparse_32(mask, idxvar) \ 288#define for_all_bits_sparse_32(mask, idxvar) \
299 for (uint32_t idxvar, mask_ = mask; \ 289 for (uint32_t idxvar, mask_ = mask; \
300 mask_ && ((idxvar = least_significant_bit (mask_)), mask_ &= ~(1 << idxvar), 1);) 290 mask_ && ((idxvar = ecb_ctz32 (mask_)), mask_ &= ~(1 << idxvar), 1);)
301 291
302extern ssize_t slice_alloc; // statistics 292extern ssize_t slice_alloc; // statistics
303 293
304void *salloc_ (int n) throw (std::bad_alloc); 294void *salloc_ (int n);
305void *salloc_ (int n, void *src) throw (std::bad_alloc); 295void *salloc_ (int n, void *src);
306 296
307// strictly the same as g_slice_alloc, but never returns 0 297// strictly the same as g_slice_alloc, but never returns 0
308template<typename T> 298template<typename T>
309inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } 299inline T *salloc (int n = 1) { return (T *)salloc_ (n * sizeof (T)); }
310 300
311// also copies src into the new area, like "memdup" 301// also copies src into the new area, like "memdup"
312// if src is 0, clears the memory 302// if src is 0, clears the memory
313template<typename T> 303template<typename T>
314inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } 304inline T *salloc (int n, T *src) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
315 305
316// clears the memory 306// clears the memory
317template<typename T> 307template<typename T>
318inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } 308inline T *salloc0(int n = 1) { return (T *)salloc_ (n * sizeof (T), 0); }
319 309
320// for symmetry 310// for symmetry
321template<typename T> 311template<typename T>
322inline void sfree (T *ptr, int n = 1) throw () 312inline void sfree (T *ptr, int n = 1) noexcept
323{ 313{
324 if (expect_true (ptr)) 314 if (expect_true (ptr))
325 { 315 {
326 slice_alloc -= n * sizeof (T); 316 slice_alloc -= n * sizeof (T);
327 if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); 317 if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T));
329 } 319 }
330} 320}
331 321
332// nulls the pointer 322// nulls the pointer
333template<typename T> 323template<typename T>
334inline void sfree0 (T *&ptr, int n = 1) throw () 324inline void sfree0 (T *&ptr, int n = 1) noexcept
335{ 325{
336 sfree<T> (ptr, n); 326 sfree<T> (ptr, n);
337 ptr = 0; 327 ptr = 0;
338} 328}
339 329
413 struct rebind 403 struct rebind
414 { 404 {
415 typedef slice_allocator<U> other; 405 typedef slice_allocator<U> other;
416 }; 406 };
417 407
418 slice_allocator () throw () { } 408 slice_allocator () noexcept { }
419 slice_allocator (const slice_allocator &) throw () { } 409 slice_allocator (const slice_allocator &) noexcept { }
420 template<typename Tp2> 410 template<typename Tp2>
421 slice_allocator (const slice_allocator<Tp2> &) throw () { } 411 slice_allocator (const slice_allocator<Tp2> &) noexcept { }
422 412
423 ~slice_allocator () { } 413 ~slice_allocator () { }
424 414
425 pointer address (reference x) const { return &x; } 415 pointer address (reference x) const { return &x; }
426 const_pointer address (const_reference x) const { return &x; } 416 const_pointer address (const_reference x) const { return &x; }
433 void deallocate (pointer p, size_type n) 423 void deallocate (pointer p, size_type n)
434 { 424 {
435 sfree<Tp> (p, n); 425 sfree<Tp> (p, n);
436 } 426 }
437 427
438 size_type max_size () const throw () 428 size_type max_size () const noexcept
439 { 429 {
440 return size_t (-1) / sizeof (Tp); 430 return size_t (-1) / sizeof (Tp);
441 } 431 }
442 432
443 void construct (pointer p, const Tp &val) 433 void construct (pointer p, const Tp &val)
536 // p if not null 526 // p if not null
537 refcnt_base::refcnt_t *refcnt_ref () { return p ? &p->refcnt : &refcnt_dummy; } 527 refcnt_base::refcnt_t *refcnt_ref () { return p ? &p->refcnt : &refcnt_dummy; }
538 528
539 void refcnt_dec () 529 void refcnt_dec ()
540 { 530 {
541 if (!is_constant (p)) 531 if (!ecb_is_constant (p))
542 --*refcnt_ref (); 532 --*refcnt_ref ();
543 else if (p) 533 else if (p)
544 --p->refcnt; 534 --p->refcnt;
545 } 535 }
546 536
547 void refcnt_inc () 537 void refcnt_inc ()
548 { 538 {
549 if (!is_constant (p)) 539 if (!ecb_is_constant (p))
550 ++*refcnt_ref (); 540 ++*refcnt_ref ();
551 else if (p) 541 else if (p)
552 ++p->refcnt; 542 ++p->refcnt;
553 } 543 }
554 544
814 804
815int similar_direction (int a, int b); 805int similar_direction (int a, int b);
816 806
817// like v?sprintf, but returns a "static" buffer 807// like v?sprintf, but returns a "static" buffer
818char *vformat (const char *format, va_list ap); 808char *vformat (const char *format, va_list ap);
819char *format (const char *format, ...) attribute ((format (printf, 1, 2))); 809char *format (const char *format, ...) ecb_attribute ((format (printf, 1, 2)));
820 810
821// safety-check player input which will become object->msg 811// safety-check player input which will become object->msg
822bool msg_is_safe (const char *msg); 812bool msg_is_safe (const char *msg);
823 813
824///////////////////////////////////////////////////////////////////////////// 814/////////////////////////////////////////////////////////////////////////////

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines