--- deliantra/server/include/util.h 2007/01/19 15:15:50 1.34 +++ deliantra/server/include/util.h 2007/04/16 15:41:27 1.40 @@ -1,6 +1,8 @@ #ifndef UTIL_H__ #define UTIL_H__ +//#define PREFER_MALLOC + #if __GNUC__ >= 3 # define is_constant(c) __builtin_constant_p (c) #else @@ -18,7 +20,7 @@ #include // use a gcc extension for auto declarations until ISO C++ sanctifies them -#define AUTODECL(var,expr) typeof(expr) var = (expr) +#define auto(var,expr) typeof(expr) var = (expr) // very ugly macro that basicaly declares and initialises a variable // that is in scope for the next statement only @@ -37,12 +39,59 @@ void fork_abort (const char *msg); -template static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } -template static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } -template static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } +// rationale for using (U) not (T) is to reduce signed/unsigned issues, +// as a is often a constant while b is the variable. it is still a bug, though. +template static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } +template static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } +template static inline T clamp (T v, U a, V b) { return v < (T)a ? (T)a : v >(T)b ? (T)b : v; } template static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } +// lots of stuff taken from FXT + +/* Rotate right. This is used in various places for checksumming */ +//TODO: that sucks, use a better checksum algo +static inline uint32_t +rotate_right (uint32_t c, uint32_t count = 1) +{ + return (c << (32 - count)) | (c >> count); +} + +static inline uint32_t +rotate_left (uint32_t c, uint32_t count = 1) +{ + return (c >> (32 - count)) | (c << count); +} + +// Return abs(a-b) +// Both a and b must not have the most significant bit set +static inline uint32_t +upos_abs_diff (uint32_t a, uint32_t b) +{ + long d1 = b - a; + long d2 = (d1 & (d1 >> 31)) << 1; + + return d1 - d2; // == (b - d) - (a + d); +} + +// Both a and b must not have the most significant bit set +static inline uint32_t +upos_min (uint32_t a, uint32_t b) +{ + int32_t d = b - a; + d &= d >> 31; + return a + d; +} + +// Both a and b must not have the most significant bit set +static inline uint32_t +upos_max (uint32_t a, uint32_t b) +{ + int32_t d = b - a; + d &= d >> 31; + return b - d; +} + // this is much faster than crossfires original algorithm // on modern cpus inline int @@ -133,7 +182,11 @@ template inline void sfree (T *ptr, int n = 1) throw () { +#ifdef PREFER_MALLOC + free (ptr); +#else g_slice_free1 (n * sizeof (T), (void *)ptr); +#endif } // a STL-compatible allocator that uses g_slice @@ -348,7 +401,7 @@ void erase (T *obj) { assert (obj->*index); - int pos = obj->*index; + unsigned int pos = obj->*index; obj->*index = 0; if (pos < this->size ())