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.113 by root, Fri Apr 22 02:03:11 2011 UTC vs.
Revision 1.124 by root, Fri Nov 18 20:20:05 2016 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 (©) 2005,2006,2007,2008,2009,2010 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.
10 * 10 *
11 * This program is distributed in the hope that it will be useful, 11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details. 14 * GNU General Public License for more details.
15 * 15 *
16 * You should have received a copy of the Affero GNU General Public License 16 * You should have received a copy of the Affero GNU General Public License
17 * and the GNU General Public License along with this program. If not, see 17 * and the GNU General Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>. 18 * <http://www.gnu.org/licenses/>.
19 * 19 *
20 * The authors can be reached via e-mail to <support@deliantra.net> 20 * The authors can be reached via e-mail to <support@deliantra.net>
21 */ 21 */
22 22
23#ifndef UTIL_H__ 23#ifndef UTIL_H__
24#define UTIL_H__ 24#define UTIL_H__
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;
86void cleanup (const char *cause, bool make_core = false); 86void cleanup (const char *cause, bool make_core = false);
87void fork_abort (const char *msg); 87void 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 (U)a < b ? (U)a : 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 (U)a > b ? (U)a : b; } 92template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
93template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? (T)a : v >(T)b ? (T)b : v; } 93template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? (T)a : v >(T)b ? (T)b : v; }
94 94
95template<typename T, typename U> static inline void min_it (T &v, U m) { v = min (v, (T)m); } 95template<typename T, typename U> static inline void min_it (T &v, U m) { v = min (v, (T)m); }
96template<typename T, typename U> static inline void max_it (T &v, U m) { v = max (v, (T)m); } 96template<typename T, typename U> static inline void max_it (T &v, U m) { v = max (v, (T)m); }
97template<typename T, typename U, typename V> static inline void clamp_it (T &v, U a, V b) { v = clamp (v, (T)a, (T)b); } 97template<typename T, typename U, typename V> static inline void clamp_it (T &v, U a, V b) { v = clamp (v, (T)a, (T)b); }
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
242inline int 242inline int
243idistance (int dx, int dy) 243idistance (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_
252#else 252#else
253 return dx_ + dy_ - min (dx_, dy_) * 5 / 8; 253 return dx_ + dy_ - min (dx_, dy_) * 5 / 8;
254#endif 254#endif
255} 255}
256 256
257// can be substantially faster than floor, if your value range allows for it
258template<typename T>
259inline T
260fastfloor (T x)
261{
262 return std::floor (x);
263}
264
265inline float
266fastfloor (float x)
267{
268 return sint32(x) - (x < 0);
269}
270
271inline double
272fastfloor (double x)
273{
274 return sint64(x) - (x < 0);
275}
276
257/* 277/*
258 * absdir(int): Returns a number between 1 and 8, which represent 278 * absdir(int): Returns a number between 1 and 8, which represent
259 * the "absolute" direction of a number (it actually takes care of 279 * the "absolute" direction of a number (it actually takes care of
260 * "overflow" in previous calculations of a direction). 280 * "overflow" in previous calculations of a direction).
261 */ 281 */
304 if (expect_true (ptr)) 324 if (expect_true (ptr))
305 { 325 {
306 slice_alloc -= n * sizeof (T); 326 slice_alloc -= n * sizeof (T);
307 if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); 327 if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T));
308 g_slice_free1 (n * sizeof (T), (void *)ptr); 328 g_slice_free1 (n * sizeof (T), (void *)ptr);
309 assert (slice_alloc >= 0);//D
310 } 329 }
311} 330}
312 331
313// nulls the pointer 332// nulls the pointer
314template<typename T> 333template<typename T>
388 typedef const Tp *const_pointer; 407 typedef const Tp *const_pointer;
389 typedef Tp &reference; 408 typedef Tp &reference;
390 typedef const Tp &const_reference; 409 typedef const Tp &const_reference;
391 typedef Tp value_type; 410 typedef Tp value_type;
392 411
393 template <class U> 412 template <class U>
394 struct rebind 413 struct rebind
395 { 414 {
396 typedef slice_allocator<U> other; 415 typedef slice_allocator<U> other;
397 }; 416 };
398 417
427 } 446 }
428 447
429 void destroy (pointer p) 448 void destroy (pointer p)
430 { 449 {
431 p->~Tp (); 450 p->~Tp ();
451 }
452};
453
454// basically a memory area, but refcounted
455struct refcnt_buf
456{
457 char *data;
458
459 refcnt_buf (size_t size = 0);
460 refcnt_buf (void *data, size_t size);
461
462 refcnt_buf (const refcnt_buf &src)
463 {
464 data = src.data;
465 inc ();
466 }
467
468 ~refcnt_buf ();
469
470 refcnt_buf &operator =(const refcnt_buf &src);
471
472 operator char *()
473 {
474 return data;
475 }
476
477 size_t size () const
478 {
479 return _size ();
480 }
481
482protected:
483 enum {
484 overhead = sizeof (uint32_t) * 2
485 };
486
487 uint32_t &_size () const
488 {
489 return ((unsigned int *)data)[-2];
490 }
491
492 uint32_t &_refcnt () const
493 {
494 return ((unsigned int *)data)[-1];
495 }
496
497 void _alloc (uint32_t size)
498 {
499 data = ((char *)salloc<char> (size + overhead)) + overhead;
500 _size () = size;
501 _refcnt () = 1;
502 }
503
504 void _dealloc ();
505
506 void inc ()
507 {
508 ++_refcnt ();
509 }
510
511 void dec ()
512 {
513 if (!--_refcnt ())
514 _dealloc ();
432 } 515 }
433}; 516};
434 517
435INTERFACE_CLASS (attachable) 518INTERFACE_CLASS (attachable)
436struct refcnt_base 519struct refcnt_base
514 // with good distribution. 597 // with good distribution.
515 // FNV-1a is faster on many cpus because the multiplication 598 // FNV-1a is faster on many cpus because the multiplication
516 // runs concurrently with the looping logic. 599 // runs concurrently with the looping logic.
517 // we modify the hash a bit to improve its distribution 600 // we modify the hash a bit to improve its distribution
518 uint32_t hash = STRHSH_NULL; 601 uint32_t hash = STRHSH_NULL;
519 602
520 while (*s) 603 while (*s)
521 hash = (hash ^ *s++) * 16777619U; 604 hash = (hash ^ *s++) * 16777619U;
522 605
523 return hash ^ (hash >> 16); 606 return hash ^ (hash >> 16);
524} 607}
525 608
526static inline uint32_t 609static inline uint32_t
527memhsh (const char *s, size_t len) 610memhsh (const char *s, size_t len)
528{ 611{
529 uint32_t hash = STRHSH_NULL; 612 uint32_t hash = STRHSH_NULL;
530 613
531 while (len--) 614 while (len--)
532 hash = (hash ^ *s++) * 16777619U; 615 hash = (hash ^ *s++) * 16777619U;
533 616
534 return hash; 617 return hash;
535} 618}
578 } 661 }
579}; 662};
580 663
581// This container blends advantages of linked lists 664// This container blends advantages of linked lists
582// (efficiency) with vectors (random access) by 665// (efficiency) with vectors (random access) by
583// by using an unordered vector and storing the vector 666// using an unordered vector and storing the vector
584// index inside the object. 667// index inside the object.
585// 668//
586// + memory-efficient on most 64 bit archs 669// + memory-efficient on most 64 bit archs
587// + O(1) insert/remove 670// + O(1) insert/remove
588// + free unique (but varying) id for inserted objects 671// + free unique (but varying) id for inserted objects
625 insert (&obj); 708 insert (&obj);
626 } 709 }
627 710
628 void erase (T *obj) 711 void erase (T *obj)
629 { 712 {
630 unsigned int pos = obj->*indexmember; 713 object_vector_index pos = obj->*indexmember;
631 obj->*indexmember = 0; 714 obj->*indexmember = 0;
632 715
633 if (pos < this->size ()) 716 if (pos < this->size ())
634 { 717 {
635 (*this)[pos - 1] = (*this)[this->size () - 1]; 718 (*this)[pos - 1] = (*this)[this->size () - 1];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines