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.73 by root, Sat May 3 11:14:50 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)
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// 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
237// makes dynamically allocated objects zero-initialised
238struct zero_initialised
239{
240 void *operator new (size_t s, void *p)
241 {
242 memset (p, 0, s);
243 return p;
244 }
245
246 void *operator new (size_t s)
247 {
248 return salloc0<char> (s);
249 }
250
251 void *operator new[] (size_t s)
252 {
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);
283 }
284
285 void operator delete (void *p, size_t s)
286 {
287 sfree ((char *)p, s);
288 }
289
290 void operator delete[] (void *p, size_t s)
291 {
292 sfree ((char *)p, s);
293 }
294};
256 295
257// a STL-compatible allocator that uses g_slice 296// a STL-compatible allocator that uses g_slice
258// boy, this is verbose 297// boy, this is verbose
259template<typename Tp> 298template<typename Tp>
260struct slice_allocator 299struct slice_allocator
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