--- deliantra/server/include/util.h 2008/04/15 03:16:02 1.68 +++ deliantra/server/include/util.h 2008/05/06 16:55:26 1.75 @@ -1,7 +1,7 @@ /* * This file is part of Deliantra, the Roguelike Realtime MMORPG. * - * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team + * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team * * Deliantra is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,8 +22,9 @@ #ifndef UTIL_H__ #define UTIL_H__ -#define DEBUG_SALLOC 0 -#define PREFER_MALLOC 1 +#define DEBUG_POISON 0x00 // poison memory before freeing it if != 0 +#define DEBUG_SALLOC 0 // add a debug wrapper around all sallocs +#define PREFER_MALLOC 0 // use malloc and not the slice allocator #if __GNUC__ >= 3 # define is_constant(c) __builtin_constant_p (c) @@ -219,11 +220,20 @@ if (expect_true (ptr)) { slice_alloc -= n * sizeof (T); + if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); g_slice_free1 (n * sizeof (T), (void *)ptr); assert (slice_alloc >= 0);//D } } +// nulls the pointer +template +inline void sfree0 (T *&ptr, int n = 1) throw () +{ + sfree (ptr, n); + ptr = 0; +} + // makes dynamically allocated objects zero-initialised struct zero_initialised { @@ -254,6 +264,35 @@ } }; +// makes dynamically allocated objects zero-initialised +struct slice_allocated +{ + void *operator new (size_t s, void *p) + { + return p; + } + + void *operator new (size_t s) + { + return salloc (s); + } + + void *operator new[] (size_t s) + { + return salloc (s); + } + + void operator delete (void *p, size_t s) + { + sfree ((char *)p, s); + } + + void operator delete[] (void *p, size_t s) + { + sfree ((char *)p, s); + } +}; + // a STL-compatible allocator that uses g_slice // boy, this is verbose template @@ -356,7 +395,7 @@ typedef tausworthe_random_generator rand_gen; -extern rand_gen rndm; +extern rand_gen rndm, rmg_rndm; INTERFACE_CLASS (attachable) struct refcnt_base