--- deliantra/server/include/util.h 2006/12/16 03:08:26 1.17 +++ deliantra/server/include/util.h 2006/12/25 11:25:49 1.24 @@ -44,14 +44,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 *salloc (int size) 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" -void *salloc (int size, void *src) throw (std::bad_alloc); +// 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 sfree (void *ptr, int size) throw () +template +inline void sfree (T *ptr, int n = 1) throw () { - g_slice_free1 (size, ptr); + g_slice_free1 (n * sizeof (T), (void *)ptr); } // a STL-compatible allocator that uses g_slice @@ -85,12 +98,12 @@ pointer allocate (size_type n, const_pointer = 0) { - return static_cast(salloc (n * sizeof (Tp))); + return salloc (n); } void deallocate (pointer p, size_type n) { - sfree (static_cast(p), n * sizeof (Tp)); + sfree (p, n); } size_type max_size ()const throw () @@ -109,21 +122,6 @@ } }; -struct refcounted -{ - refcounted () : refcnt (0) { } -// virtual ~refcounted (); - void refcnt_inc () { ++refcnt; } - void refcnt_dec () { --refcnt; } - bool dead () { return refcnt == 0; } - mutable int refcnt; -#if 0 -private: - static refcounted *rc_first; - refcounted *rc_next; -#endif -}; - template struct refptr { @@ -155,6 +153,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