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.67 by root, Fri Apr 11 21:09:53 2008 UTC vs.
Revision 1.74 by root, Sun May 4 14:12:37 2008 UTC

20 */ 20 */
21 21
22#ifndef UTIL_H__ 22#ifndef UTIL_H__
23#define UTIL_H__ 23#define UTIL_H__
24 24
25#define DEBUG_SALLOC 0 25#define DEBUG_POISON 0x00 // poison memory before freeing it if != 0
26#define PREFER_MALLOC 0 26#define DEBUG_SALLOC 0 // add a debug wrapper around all sallocs
27#define PREFER_MALLOC 0 // use malloc and not the slice allocator
27 28
28#if __GNUC__ >= 3 29#if __GNUC__ >= 3
29# define is_constant(c) __builtin_constant_p (c) 30# define is_constant(c) __builtin_constant_p (c)
30# define expect(expr,value) __builtin_expect ((expr),(value)) 31# define expect(expr,value) __builtin_expect ((expr),(value))
31# define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) 32# define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
65void *g_slice_alloc0 (unsigned long size); 66void *g_slice_alloc0 (unsigned long size);
66void g_slice_free1 (unsigned long size, void *ptr); 67void g_slice_free1 (unsigned long size, void *ptr);
67#elif PREFER_MALLOC 68#elif PREFER_MALLOC
68# define g_slice_alloc0(s) calloc (1, (s)) 69# define g_slice_alloc0(s) calloc (1, (s))
69# define g_slice_alloc(s) malloc ((s)) 70# define g_slice_alloc(s) malloc ((s))
70# define g_slice_free1(s,p) free ((s)) 71# define g_slice_free1(s,p) free ((p))
71#endif 72#endif
72 73
73// use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) 74// use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever)
74#define auto(var,expr) decltype(expr) var = (expr) 75#define auto(var,expr) decltype(expr) var = (expr)
75 76
217inline void sfree (T *ptr, int n = 1) throw () 218inline void sfree (T *ptr, int n = 1) throw ()
218{ 219{
219 if (expect_true (ptr)) 220 if (expect_true (ptr))
220 { 221 {
221 slice_alloc -= n * sizeof (T); 222 slice_alloc -= n * sizeof (T);
223 if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T));
222 g_slice_free1 (n * sizeof (T), (void *)ptr); 224 g_slice_free1 (n * sizeof (T), (void *)ptr);
223 assert (slice_alloc >= 0);//D 225 assert (slice_alloc >= 0);//D
224 } 226 }
225} 227}
226 228
229// nulls the pointer
230template<typename T>
231inline void sfree0 (T *&ptr, int n = 1) throw ()
232{
233 sfree<T> (ptr, n);
234 ptr = 0;
235}
236
227// makes dynamically allocated objects zero-initialised 237// makes dynamically allocated objects zero-initialised
228struct zero_initialised 238struct zero_initialised
229{ 239{
230 void *operator new (size_t s, void *p) 240 void *operator new (size_t s, void *p)
231 { 241 {
239 } 249 }
240 250
241 void *operator new[] (size_t s) 251 void *operator new[] (size_t s)
242 { 252 {
243 return salloc0<char> (s); 253 return salloc0<char> (s);
254 }
255
256 void operator delete (void *p, size_t s)
257 {
258 sfree ((char *)p, s);
259 }
260
261 void operator delete[] (void *p, size_t s)
262 {
263 sfree ((char *)p, s);
264 }
265};
266
267// makes dynamically allocated objects zero-initialised
268struct slice_allocated
269{
270 void *operator new (size_t s, void *p)
271 {
272 return p;
273 }
274
275 void *operator new (size_t s)
276 {
277 return salloc<char> (s);
278 }
279
280 void *operator new[] (size_t s)
281 {
282 return salloc<char> (s);
244 } 283 }
245 284
246 void operator delete (void *p, size_t s) 285 void operator delete (void *p, size_t s)
247 { 286 {
248 sfree ((char *)p, s); 287 sfree ((char *)p, s);
354 int get_range (int r_min, int r_max); 393 int get_range (int r_min, int r_max);
355}; 394};
356 395
357typedef tausworthe_random_generator rand_gen; 396typedef tausworthe_random_generator rand_gen;
358 397
359extern rand_gen rndm; 398extern rand_gen rndm, rmg_rndm;
360 399
361INTERFACE_CLASS (attachable) 400INTERFACE_CLASS (attachable)
362struct refcnt_base 401struct refcnt_base
363{ 402{
364 typedef int refcnt_t; 403 typedef int refcnt_t;
606#else 645#else
607 #define SMUTEX_INITIALISER PTHREAD_MUTEX_INITIALIZER 646 #define SMUTEX_INITIALISER PTHREAD_MUTEX_INITIALIZER
608#endif 647#endif
609 648
610#define SMUTEX(name) smutex name = SMUTEX_INITIALISER 649#define SMUTEX(name) smutex name = SMUTEX_INITIALISER
611#define SMUTEX_LOCK(name) pthread_mutex_lock (&(name)) 650#define SMUTEX_LOCK(name) pthread_mutex_lock (&(name))
612#define SMUTEX_UNLOCK(name) pthread_mutex_unlock (&(name)) 651#define SMUTEX_UNLOCK(name) pthread_mutex_unlock (&(name))
613 652
653typedef pthread_cond_t scond;
654
655#define SCOND(name) scond name = PTHREAD_COND_INITIALIZER
656#define SCOND_SIGNAL(name) pthread_cond_signal (&(name))
657#define SCOND_BROADCAST(name) pthread_cond_broadcast (&(name))
658#define SCOND_WAIT(name,mutex) pthread_cond_wait (&(name), &(mutex))
659
614#endif 660#endif
615 661

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines