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.110 by root, Fri Jul 2 02:00:47 2010 UTC vs.
Revision 1.117 by root, Tue Jan 3 11:23:41 2012 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 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.
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); }
114 114
115// sign0 returns -1, 0 or +1 115// sign0 returns -1, 0 or +1
116template<typename T> 116template<typename T>
117static inline T sign0 (T v) { return v ? sign (v) : 0; } 117static inline T sign0 (T v) { return v ? sign (v) : 0; }
118 118
119//clashes with C++0x
119template<typename T, typename U> 120template<typename T, typename U>
120static inline T copysign (T a, U b) { return a > 0 ? b : -b; } 121static inline T copysign (T a, U b) { return a > 0 ? b : -b; }
121 122
122// div* only work correctly for div > 0 123// div* only work correctly for div > 0
123// div, with correct rounding (< 0.5 downwards, >=0.5 upwards) 124// div, with correct rounding (< 0.5 downwards, >=0.5 upwards)
251#else 252#else
252 return dx_ + dy_ - min (dx_, dy_) * 5 / 8; 253 return dx_ + dy_ - min (dx_, dy_) * 5 / 8;
253#endif 254#endif
254} 255}
255 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
256/* 277/*
257 * absdir(int): Returns a number between 1 and 8, which represent 278 * absdir(int): Returns a number between 1 and 8, which represent
258 * the "absolute" direction of a number (it actually takes care of 279 * the "absolute" direction of a number (it actually takes care of
259 * "overflow" in previous calculations of a direction). 280 * "overflow" in previous calculations of a direction).
260 */ 281 */
303 if (expect_true (ptr)) 324 if (expect_true (ptr))
304 { 325 {
305 slice_alloc -= n * sizeof (T); 326 slice_alloc -= n * sizeof (T);
306 if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); 327 if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T));
307 g_slice_free1 (n * sizeof (T), (void *)ptr); 328 g_slice_free1 (n * sizeof (T), (void *)ptr);
308 assert (slice_alloc >= 0);//D
309 } 329 }
310} 330}
311 331
312// nulls the pointer 332// nulls the pointer
313template<typename T> 333template<typename T>
429 { 449 {
430 p->~Tp (); 450 p->~Tp ();
431 } 451 }
432}; 452};
433 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 ++_refcnt ();
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 (unsigned int) * 2
485 };
486
487 unsigned int &_size () const
488 {
489 return ((unsigned int *)data)[-2];
490 }
491
492 unsigned int &_refcnt () const
493 {
494 return ((unsigned int *)data)[-1];
495 }
496
497 void _alloc (unsigned int size)
498 {
499 data = ((char *)salloc<char> (size + overhead)) + overhead;
500 _size () = size;
501 _refcnt () = 1;
502 }
503
504 void dec ()
505 {
506 if (!--_refcnt ())
507 sfree<char> (data - overhead, size () + overhead);
508 }
509};
510
434INTERFACE_CLASS (attachable) 511INTERFACE_CLASS (attachable)
435struct refcnt_base 512struct refcnt_base
436{ 513{
437 typedef int refcnt_t; 514 typedef int refcnt_t;
438 mutable refcnt_t ACC (RW, refcnt); 515 mutable refcnt_t ACC (RW, refcnt);
511 // use FNV-1a hash (http://isthe.com/chongo/tech/comp/fnv/) 588 // use FNV-1a hash (http://isthe.com/chongo/tech/comp/fnv/)
512 // it is about twice as fast as the one-at-a-time one, 589 // it is about twice as fast as the one-at-a-time one,
513 // with good distribution. 590 // with good distribution.
514 // FNV-1a is faster on many cpus because the multiplication 591 // FNV-1a is faster on many cpus because the multiplication
515 // runs concurrently with the looping logic. 592 // runs concurrently with the looping logic.
593 // we modify the hash a bit to improve its distribution
516 uint32_t hash = STRHSH_NULL; 594 uint32_t hash = STRHSH_NULL;
517 595
518 while (*s) 596 while (*s)
519 hash = (hash ^ *s++) * 16777619U; 597 hash = (hash ^ *s++) * 16777619U;
520 598
521 return hash; 599 return hash ^ (hash >> 16);
522} 600}
523 601
524static inline uint32_t 602static inline uint32_t
525memhsh (const char *s, size_t len) 603memhsh (const char *s, size_t len)
526{ 604{
641 { 719 {
642 erase (&obj); 720 erase (&obj);
643 } 721 }
644}; 722};
645 723
724/////////////////////////////////////////////////////////////////////////////
725
726// something like a vector or stack, but without
727// out of bounds checking
728template<typename T>
729struct fixed_stack
730{
731 T *data;
732 int size;
733 int max;
734
735 fixed_stack ()
736 : size (0), data (0)
737 {
738 }
739
740 fixed_stack (int max)
741 : size (0), max (max)
742 {
743 data = salloc<T> (max);
744 }
745
746 void reset (int new_max)
747 {
748 sfree (data, max);
749 size = 0;
750 max = new_max;
751 data = salloc<T> (max);
752 }
753
754 void free ()
755 {
756 sfree (data, max);
757 data = 0;
758 }
759
760 ~fixed_stack ()
761 {
762 sfree (data, max);
763 }
764
765 T &operator[](int idx)
766 {
767 return data [idx];
768 }
769
770 void push (T v)
771 {
772 data [size++] = v;
773 }
774
775 T &pop ()
776 {
777 return data [--size];
778 }
779
780 T remove (int idx)
781 {
782 T v = data [idx];
783
784 data [idx] = data [--size];
785
786 return v;
787 }
788};
789
790/////////////////////////////////////////////////////////////////////////////
791
646// basically does what strncpy should do, but appends "..." to strings exceeding length 792// basically does what strncpy should do, but appends "..." to strings exceeding length
647// returns the number of bytes actually used (including \0) 793// returns the number of bytes actually used (including \0)
648int assign (char *dst, const char *src, int maxsize); 794int assign (char *dst, const char *src, int maxsize);
649 795
650// type-safe version of assign 796// type-safe version of assign

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines