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.54 by root, Mon Aug 6 10:54:12 2007 UTC vs.
Revision 1.60 by root, Tue Jan 22 15:53:01 2008 UTC

1/* 1/*
2 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team 4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * 5 *
6 * Crossfire TRT is free software: you can redistribute it and/or modify 6 * Deliantra is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or 8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version. 9 * (at your option) any later version.
10 * 10 *
11 * This program is distributed in the hope that it will be useful, 11 * This program is distributed in the hope that it will be useful,
14 * GNU General Public License for more details. 14 * GNU General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * 18 *
19 * The authors can be reached via e-mail to <crossfire@schmorp.de> 19 * The authors can be reached via e-mail to <support@deliantra.net>
20 */ 20 */
21 21
22#ifndef UTIL_H__ 22#ifndef UTIL_H__
23#define UTIL_H__ 23#define UTIL_H__
24 24
25//#define PREFER_MALLOC 25//#define PREFER_MALLOC
26#define DEBUG_SALLOC
26 27
27#if __GNUC__ >= 3 28#if __GNUC__ >= 3
28# define is_constant(c) __builtin_constant_p (c) 29# define is_constant(c) __builtin_constant_p (c)
29# define expect(expr,value) __builtin_expect ((expr),(value)) 30# define expect(expr,value) __builtin_expect ((expr),(value))
30# define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) 31# define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
52#include <glib.h> 53#include <glib.h>
53 54
54#include <shstr.h> 55#include <shstr.h>
55#include <traits.h> 56#include <traits.h>
56 57
58#ifdef DEBUG_SALLOC
59# define g_slice_alloc0(s) debug_slice_alloc0(s)
60# define g_slice_alloc(s) debug_slice_alloc(s)
61# define g_slice_free1(s,p) debug_slice_free1(s,p)
62void *g_slice_alloc (unsigned long size);
63void *g_slice_alloc0 (unsigned long size);
64void g_slice_free1 (unsigned long size, void *ptr);
65#endif
66
57// use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) 67// use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever)
58#define auto(var,expr) decltype(expr) var = (expr) 68#define auto(var,expr) decltype(expr) var = (expr)
59 69
60// very ugly macro that basicaly declares and initialises a variable 70// very ugly macro that basicaly declares and initialises a variable
61// that is in scope for the next statement only 71// that is in scope for the next statement only
172absdir (int d) 182absdir (int d)
173{ 183{
174 return ((d - 1) & 7) + 1; 184 return ((d - 1) & 7) + 1;
175} 185}
176 186
187extern size_t slice_alloc; // statistics
188
177// makes dynamically allocated objects zero-initialised 189// makes dynamically allocated objects zero-initialised
178struct zero_initialised 190struct zero_initialised
179{ 191{
180 void *operator new (size_t s, void *p) 192 void *operator new (size_t s, void *p)
181 { 193 {
183 return p; 195 return p;
184 } 196 }
185 197
186 void *operator new (size_t s) 198 void *operator new (size_t s)
187 { 199 {
200 slice_alloc += s;
188 return g_slice_alloc0 (s); 201 return g_slice_alloc0 (s);
189 } 202 }
190 203
191 void *operator new[] (size_t s) 204 void *operator new[] (size_t s)
192 { 205 {
206 slice_alloc += s;
193 return g_slice_alloc0 (s); 207 return g_slice_alloc0 (s);
194 } 208 }
195 209
196 void operator delete (void *p, size_t s) 210 void operator delete (void *p, size_t s)
197 { 211 {
212 slice_alloc -= s;
198 g_slice_free1 (s, p); 213 g_slice_free1 (s, p);
199 } 214 }
200 215
201 void operator delete[] (void *p, size_t s) 216 void operator delete[] (void *p, size_t s)
202 { 217 {
218 slice_alloc -= s;
203 g_slice_free1 (s, p); 219 g_slice_free1 (s, p);
204 } 220 }
205}; 221};
206 222
207void *salloc_ (int n) throw (std::bad_alloc); 223void *salloc_ (int n) throw (std::bad_alloc);
225inline void sfree (T *ptr, int n = 1) throw () 241inline void sfree (T *ptr, int n = 1) throw ()
226{ 242{
227#ifdef PREFER_MALLOC 243#ifdef PREFER_MALLOC
228 free (ptr); 244 free (ptr);
229#else 245#else
246 slice_alloc -= n * sizeof (T);
230 g_slice_free1 (n * sizeof (T), (void *)ptr); 247 g_slice_free1 (n * sizeof (T), (void *)ptr);
231#endif 248#endif
232} 249}
233 250
234// a STL-compatible allocator that uses g_slice 251// a STL-compatible allocator that uses g_slice
345 MTH void refcnt_dec () const { --refcnt; } 362 MTH void refcnt_dec () const { --refcnt; }
346 363
347 refcnt_base () : refcnt (0) { } 364 refcnt_base () : refcnt (0) { }
348}; 365};
349 366
367// to avoid branches with more advanced compilers
350extern refcnt_base::refcnt_t refcnt_dummy; 368extern refcnt_base::refcnt_t refcnt_dummy;
351 369
352template<class T> 370template<class T>
353struct refptr 371struct refptr
354{ 372{
539 assign ((char *)&dst, src, N); 557 assign ((char *)&dst, src, N);
540} 558}
541 559
542typedef double tstamp; 560typedef double tstamp;
543 561
544// return current time as timestampe 562// return current time as timestamp
545tstamp now (); 563tstamp now ();
546 564
547int similar_direction (int a, int b); 565int similar_direction (int a, int b);
548 566
549// like printf, but returns a std::string 567// like sprintf, but returns a "static" buffer
550const std::string format (const char *format, ...); 568const char *format (const char *format, ...);
551 569
552#endif 570#endif
553 571

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines