--- deliantra/server/include/util.h 2006/09/12 21:10:32 1.12 +++ deliantra/server/include/util.h 2006/12/30 10:16:10 1.25 @@ -8,9 +8,17 @@ #endif #include +#include +#include #include +#include +#include + +// use a gcc extension for auto declarations until ISO C++ sanctifies them +#define AUTODECL(var,expr) typeof(expr) var = (expr) + // makes dynamically allocated objects zero-initialised struct zero_initialised { @@ -41,12 +49,27 @@ } }; +void *salloc_ (int n) throw (std::bad_alloc); +void *salloc_ (int n, void *src) throw (std::bad_alloc); + // strictly the same as g_slice_alloc, but never returns 0 -void *alloc (int s) throw (std::bad_alloc); +template +inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } + +// also copies src into the new area, like "memdup" +// if src is 0, clears the memory +template +inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } + +// clears the memory +template +inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } + // for symmetry -inline void dealloc (void *p, int s) throw () +template +inline void sfree (T *ptr, int n = 1) throw () { - g_slice_free1 (s, p); + g_slice_free1 (n * sizeof (T), (void *)ptr); } // a STL-compatible allocator that uses g_slice @@ -80,12 +103,12 @@ pointer allocate (size_type n, const_pointer = 0) { - return static_cast(alloc (n * sizeof (Tp))); + return salloc (n); } void deallocate (pointer p, size_type n) { - dealloc (static_cast(p), n); + sfree (p, n); } size_type max_size ()const throw () @@ -104,15 +127,6 @@ } }; -struct refcounted -{ - mutable int refcnt; - refcounted () : refcnt (0) { } - void refcnt_inc () { ++refcnt; } - void refcnt_dec () { --refcnt; - if (refcnt < 0)abort();}//D -}; - template struct refptr { @@ -144,6 +158,12 @@ operator T *() const { return p; } }; +typedef refptr maptile_ptr; +typedef refptr object_ptr; +typedef refptr arch_ptr; +typedef refptr client_ptr; +typedef refptr player_ptr; + struct str_hash { std::size_t operator ()(const char *s) const @@ -178,8 +198,6 @@ } }; -#include - template struct unordered_vector : std::vector > { @@ -215,5 +233,12 @@ assign ((char *)&dst, src, N); } +typedef double tstamp; + +// return current time as timestampe +tstamp now (); + +int similar_direction (int a, int b); + #endif