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.58 by root, Thu Nov 8 19:43:25 2007 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
172absdir (int d) 172absdir (int d)
173{ 173{
174 return ((d - 1) & 7) + 1; 174 return ((d - 1) & 7) + 1;
175} 175}
176 176
177extern size_t slice_alloc; // statistics
178
177// makes dynamically allocated objects zero-initialised 179// makes dynamically allocated objects zero-initialised
178struct zero_initialised 180struct zero_initialised
179{ 181{
180 void *operator new (size_t s, void *p) 182 void *operator new (size_t s, void *p)
181 { 183 {
183 return p; 185 return p;
184 } 186 }
185 187
186 void *operator new (size_t s) 188 void *operator new (size_t s)
187 { 189 {
190 slice_alloc += s;
188 return g_slice_alloc0 (s); 191 return g_slice_alloc0 (s);
189 } 192 }
190 193
191 void *operator new[] (size_t s) 194 void *operator new[] (size_t s)
192 { 195 {
196 slice_alloc += s;
193 return g_slice_alloc0 (s); 197 return g_slice_alloc0 (s);
194 } 198 }
195 199
196 void operator delete (void *p, size_t s) 200 void operator delete (void *p, size_t s)
197 { 201 {
202 slice_alloc -= s;
198 g_slice_free1 (s, p); 203 g_slice_free1 (s, p);
199 } 204 }
200 205
201 void operator delete[] (void *p, size_t s) 206 void operator delete[] (void *p, size_t s)
202 { 207 {
208 slice_alloc -= s;
203 g_slice_free1 (s, p); 209 g_slice_free1 (s, p);
204 } 210 }
205}; 211};
206 212
207void *salloc_ (int n) throw (std::bad_alloc); 213void *salloc_ (int n) throw (std::bad_alloc);
225inline void sfree (T *ptr, int n = 1) throw () 231inline void sfree (T *ptr, int n = 1) throw ()
226{ 232{
227#ifdef PREFER_MALLOC 233#ifdef PREFER_MALLOC
228 free (ptr); 234 free (ptr);
229#else 235#else
236 slice_alloc -= n * sizeof (T);
230 g_slice_free1 (n * sizeof (T), (void *)ptr); 237 g_slice_free1 (n * sizeof (T), (void *)ptr);
231#endif 238#endif
232} 239}
233 240
234// a STL-compatible allocator that uses g_slice 241// a STL-compatible allocator that uses g_slice
345 MTH void refcnt_dec () const { --refcnt; } 352 MTH void refcnt_dec () const { --refcnt; }
346 353
347 refcnt_base () : refcnt (0) { } 354 refcnt_base () : refcnt (0) { }
348}; 355};
349 356
357// to avoid branches with more advanced compilers
350extern refcnt_base::refcnt_t refcnt_dummy; 358extern refcnt_base::refcnt_t refcnt_dummy;
351 359
352template<class T> 360template<class T>
353struct refptr 361struct refptr
354{ 362{
544// return current time as timestampe 552// return current time as timestampe
545tstamp now (); 553tstamp now ();
546 554
547int similar_direction (int a, int b); 555int similar_direction (int a, int b);
548 556
549// like printf, but returns a std::string 557// like sprintf, but returns a "static" buffer
550const std::string format (const char *format, ...); 558const char *format (const char *format, ...);
551 559
552#endif 560#endif
553 561

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines