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.66 by root, Wed Apr 2 11:13:55 2008 UTC vs.
Revision 1.70 by root, Sun Apr 20 05:24:55 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 0xaa // 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)
62# define g_slice_alloc(s) debug_slice_alloc(s) 63# define g_slice_alloc(s) debug_slice_alloc(s)
63# define g_slice_free1(s,p) debug_slice_free1(s,p) 64# define g_slice_free1(s,p) debug_slice_free1(s,p)
64void *g_slice_alloc (unsigned long size); 65void *g_slice_alloc (unsigned long size);
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);
68#elif PREFER_MALLOC
69# define g_slice_alloc0(s) calloc (1, (s))
70# define g_slice_alloc(s) malloc ((s))
71# define g_slice_free1(s,p) free ((p))
67#endif 72#endif
68 73
69// 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)
70#define auto(var,expr) decltype(expr) var = (expr) 75#define auto(var,expr) decltype(expr) var = (expr)
71 76
188absdir (int d) 193absdir (int d)
189{ 194{
190 return ((d - 1) & 7) + 1; 195 return ((d - 1) & 7) + 1;
191} 196}
192 197
193extern size_t slice_alloc; // statistics 198extern ssize_t slice_alloc; // statistics
194
195// makes dynamically allocated objects zero-initialised
196struct zero_initialised
197{
198 void *operator new (size_t s, void *p)
199 {
200 memset (p, 0, s);
201 return p;
202 }
203
204 void *operator new (size_t s)
205 {
206 slice_alloc += s;
207 return g_slice_alloc0 (s);
208 }
209
210 void *operator new[] (size_t s)
211 {
212 slice_alloc += s;
213 return g_slice_alloc0 (s);
214 }
215
216 void operator delete (void *p, size_t s)
217 {
218 slice_alloc -= s;
219 g_slice_free1 (s, p);
220 }
221
222 void operator delete[] (void *p, size_t s)
223 {
224 slice_alloc -= s;
225 g_slice_free1 (s, p);
226 }
227};
228 199
229void *salloc_ (int n) throw (std::bad_alloc); 200void *salloc_ (int n) throw (std::bad_alloc);
230void *salloc_ (int n, void *src) throw (std::bad_alloc); 201void *salloc_ (int n, void *src) throw (std::bad_alloc);
231 202
232// strictly the same as g_slice_alloc, but never returns 0 203// strictly the same as g_slice_alloc, but never returns 0
244 215
245// for symmetry 216// for symmetry
246template<typename T> 217template<typename T>
247inline void sfree (T *ptr, int n = 1) throw () 218inline void sfree (T *ptr, int n = 1) throw ()
248{ 219{
249#if PREFER_MALLOC 220 if (expect_true (ptr))
250 free (ptr); 221 {
251#else
252 slice_alloc -= n * sizeof (T); 222 slice_alloc -= n * sizeof (T);
223 if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T));
253 g_slice_free1 (n * sizeof (T), (void *)ptr); 224 g_slice_free1 (n * sizeof (T), (void *)ptr);
254#endif 225 assert (slice_alloc >= 0);//D
226 }
255} 227}
228
229// makes dynamically allocated objects zero-initialised
230struct zero_initialised
231{
232 void *operator new (size_t s, void *p)
233 {
234 memset (p, 0, s);
235 return p;
236 }
237
238 void *operator new (size_t s)
239 {
240 return salloc0<char> (s);
241 }
242
243 void *operator new[] (size_t s)
244 {
245 return salloc0<char> (s);
246 }
247
248 void operator delete (void *p, size_t s)
249 {
250 sfree ((char *)p, s);
251 }
252
253 void operator delete[] (void *p, size_t s)
254 {
255 sfree ((char *)p, s);
256 }
257};
256 258
257// a STL-compatible allocator that uses g_slice 259// a STL-compatible allocator that uses g_slice
258// boy, this is verbose 260// boy, this is verbose
259template<typename Tp> 261template<typename Tp>
260struct slice_allocator 262struct slice_allocator
606#else 608#else
607 #define SMUTEX_INITIALISER PTHREAD_MUTEX_INITIALIZER 609 #define SMUTEX_INITIALISER PTHREAD_MUTEX_INITIALIZER
608#endif 610#endif
609 611
610#define SMUTEX(name) smutex name = SMUTEX_INITIALISER 612#define SMUTEX(name) smutex name = SMUTEX_INITIALISER
611#define SMUTEX_LOCK(name) pthread_mutex_lock (&(name)) 613#define SMUTEX_LOCK(name) pthread_mutex_lock (&(name))
612#define SMUTEX_UNLOCK(name) pthread_mutex_unlock (&(name)) 614#define SMUTEX_UNLOCK(name) pthread_mutex_unlock (&(name))
613 615
616typedef pthread_cond_t scond;
617
618#define SCOND(name) scond name = PTHREAD_COND_INITIALIZER
619#define SCOND_SIGNAL(name) pthread_cond_signal (&(name))
620#define SCOND_BROADCAST(name) pthread_cond_broadcast (&(name))
621#define SCOND_WAIT(name,mutex) pthread_cond_wait (&(name), &(mutex))
622
614#endif 623#endif
615 624

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines