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.46 by root, Mon May 28 21:15:56 2007 UTC vs.
Revision 1.62 by root, Fri Jan 25 18:13:57 2008 UTC

1/* 1/*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. 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 it 6 * Deliantra is free software: you can redistribute it and/or modify
7 * under the terms of the GNU General Public License as published by the Free 7 * it under the terms of the GNU General Public License as published by
8 * Software Foundation; either version 2 of the License, or (at your option) 8 * the Free Software Foundation, either version 3 of the License, or
9 * 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, but 11 * This program is distributed in the hope that it will be useful,
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * 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 along 16 * You should have received a copy of the GNU General Public License
17 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * 18 *
20 * 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>
21 */ 20 */
22 21
23#ifndef UTIL_H__ 22#ifndef UTIL_H__
24#define UTIL_H__ 23#define UTIL_H__
25 24
26//#define PREFER_MALLOC 25//#define PREFER_MALLOC
26#define DEBUG_SALLOC
27 27
28#if __GNUC__ >= 3 28#if __GNUC__ >= 3
29# define is_constant(c) __builtin_constant_p (c) 29# define is_constant(c) __builtin_constant_p (c)
30# define expect(expr,value) __builtin_expect ((expr),(value)) 30# define expect(expr,value) __builtin_expect ((expr),(value))
31# define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) 31# define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
33# define is_constant(c) 0 33# define is_constant(c) 0
34# define expect(expr,value) (expr) 34# define expect(expr,value) (expr)
35# define prefetch(addr,rw,locality) 35# define prefetch(addr,rw,locality)
36#endif 36#endif
37 37
38#if __GNUC__ < 4 || (__GNUC__ == 4 || __GNUC_MINOR__ < 4)
39# define decltype(x) typeof(x)
40#endif
41
38// put into ifs if you are very sure that the expression 42// put into ifs if you are very sure that the expression
39// is mostly true or mosty false. note that these return 43// is mostly true or mosty false. note that these return
40// booleans, not the expression. 44// booleans, not the expression.
41#define expect_false(expr) expect ((expr) != 0, 0) 45#define expect_false(expr) expect ((expr) != 0, 0)
42#define expect_true(expr) expect ((expr) != 0, 1) 46#define expect_true(expr) expect ((expr) != 0, 1)
49#include <glib.h> 53#include <glib.h>
50 54
51#include <shstr.h> 55#include <shstr.h>
52#include <traits.h> 56#include <traits.h>
53 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
54// use a gcc extension for auto declarations until ISO C++ sanctifies them 67// use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever)
55#define auto(var,expr) typeof(expr) var = (expr) 68#define auto(var,expr) decltype(expr) var = (expr)
56 69
57// very ugly macro that basicaly declares and initialises a variable 70// very ugly macro that basicaly declares and initialises a variable
58// that is in scope for the next statement only 71// that is in scope for the next statement only
59// works only for stuff that can be assigned 0 and converts to false 72// works only for stuff that can be assigned 0 and converts to false
60// (note: works great for pointers) 73// (note: works great for pointers)
61// most ugly macro I ever wrote 74// most ugly macro I ever wrote
62#define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) 75#define statementvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1)
63 76
64// in range including end 77// in range including end
65#define IN_RANGE_INC(val,beg,end) \ 78#define IN_RANGE_INC(val,beg,end) \
66 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) 79 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
67 80
169absdir (int d) 182absdir (int d)
170{ 183{
171 return ((d - 1) & 7) + 1; 184 return ((d - 1) & 7) + 1;
172} 185}
173 186
187extern size_t slice_alloc; // statistics
188
174// makes dynamically allocated objects zero-initialised 189// makes dynamically allocated objects zero-initialised
175struct zero_initialised 190struct zero_initialised
176{ 191{
177 void *operator new (size_t s, void *p) 192 void *operator new (size_t s, void *p)
178 { 193 {
180 return p; 195 return p;
181 } 196 }
182 197
183 void *operator new (size_t s) 198 void *operator new (size_t s)
184 { 199 {
200 slice_alloc += s;
185 return g_slice_alloc0 (s); 201 return g_slice_alloc0 (s);
186 } 202 }
187 203
188 void *operator new[] (size_t s) 204 void *operator new[] (size_t s)
189 { 205 {
206 slice_alloc += s;
190 return g_slice_alloc0 (s); 207 return g_slice_alloc0 (s);
191 } 208 }
192 209
193 void operator delete (void *p, size_t s) 210 void operator delete (void *p, size_t s)
194 { 211 {
212 slice_alloc -= s;
195 g_slice_free1 (s, p); 213 g_slice_free1 (s, p);
196 } 214 }
197 215
198 void operator delete[] (void *p, size_t s) 216 void operator delete[] (void *p, size_t s)
199 { 217 {
218 slice_alloc -= s;
200 g_slice_free1 (s, p); 219 g_slice_free1 (s, p);
201 } 220 }
202}; 221};
203 222
204void *salloc_ (int n) throw (std::bad_alloc); 223void *salloc_ (int n) throw (std::bad_alloc);
222inline void sfree (T *ptr, int n = 1) throw () 241inline void sfree (T *ptr, int n = 1) throw ()
223{ 242{
224#ifdef PREFER_MALLOC 243#ifdef PREFER_MALLOC
225 free (ptr); 244 free (ptr);
226#else 245#else
246 slice_alloc -= n * sizeof (T);
227 g_slice_free1 (n * sizeof (T), (void *)ptr); 247 g_slice_free1 (n * sizeof (T), (void *)ptr);
228#endif 248#endif
229} 249}
230 250
231// a STL-compatible allocator that uses g_slice 251// a STL-compatible allocator that uses g_slice
330 350
331typedef tausworthe_random_generator rand_gen; 351typedef tausworthe_random_generator rand_gen;
332 352
333extern rand_gen rndm; 353extern rand_gen rndm;
334 354
355INTERFACE_CLASS (attachable)
356struct refcnt_base
357{
358 typedef int refcnt_t;
359 mutable refcnt_t ACC (RW, refcnt);
360
361 MTH void refcnt_inc () const { ++refcnt; }
362 MTH void refcnt_dec () const { --refcnt; }
363
364 refcnt_base () : refcnt (0) { }
365};
366
367// to avoid branches with more advanced compilers
368extern refcnt_base::refcnt_t refcnt_dummy;
369
335template<class T> 370template<class T>
336struct refptr 371struct refptr
337{ 372{
373 // p if not null
374 refcnt_base::refcnt_t *refcnt_ref () { return p ? &p->refcnt : &refcnt_dummy; }
375
376 void refcnt_dec ()
377 {
378 if (!is_constant (p))
379 --*refcnt_ref ();
380 else if (p)
381 --p->refcnt;
382 }
383
384 void refcnt_inc ()
385 {
386 if (!is_constant (p))
387 ++*refcnt_ref ();
388 else if (p)
389 ++p->refcnt;
390 }
391
338 T *p; 392 T *p;
339 393
340 refptr () : p(0) { } 394 refptr () : p(0) { }
341 refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); } 395 refptr (const refptr<T> &p) : p(p.p) { refcnt_inc (); }
342 refptr (T *p) : p(p) { if (p) p->refcnt_inc (); } 396 refptr (T *p) : p(p) { refcnt_inc (); }
343 ~refptr () { if (p) p->refcnt_dec (); } 397 ~refptr () { refcnt_dec (); }
344 398
345 const refptr<T> &operator =(T *o) 399 const refptr<T> &operator =(T *o)
346 { 400 {
401 // if decrementing ever destroys we need to reverse the order here
347 if (p) p->refcnt_dec (); 402 refcnt_dec ();
348 p = o; 403 p = o;
349 if (p) p->refcnt_inc (); 404 refcnt_inc ();
350
351 return *this; 405 return *this;
352 } 406 }
353 407
354 const refptr<T> &operator =(const refptr<T> o) 408 const refptr<T> &operator =(const refptr<T> &o)
355 { 409 {
356 *this = o.p; 410 *this = o.p;
357 return *this; 411 return *this;
358 } 412 }
359 413
360 T &operator * () const { return *p; } 414 T &operator * () const { return *p; }
361 T *operator ->() const { return p; } 415 T *operator ->() const { return p; }
362 416
363 operator T *() const { return p; } 417 operator T *() const { return p; }
364}; 418};
365 419
366typedef refptr<maptile> maptile_ptr; 420typedef refptr<maptile> maptile_ptr;
401 { 455 {
402 return !strcmp (a, b); 456 return !strcmp (a, b);
403 } 457 }
404}; 458};
405 459
460// Mostly the same as std::vector, but insert/erase can reorder
461// the elements, making append(=insert)/remove O(1) instead of O(n).
462//
463// NOTE: only some forms of erase are available
406template<class T> 464template<class T>
407struct unordered_vector : std::vector<T, slice_allocator<T> > 465struct unordered_vector : std::vector<T, slice_allocator<T> >
408{ 466{
409 typedef typename unordered_vector::iterator iterator; 467 typedef typename unordered_vector::iterator iterator;
410 468
420 { 478 {
421 erase ((unsigned int )(i - this->begin ())); 479 erase ((unsigned int )(i - this->begin ()));
422 } 480 }
423}; 481};
424 482
425template<class T, int T::* index> 483// This container blends advantages of linked lists
484// (efficiency) with vectors (random access) by
485// by using an unordered vector and storing the vector
486// index inside the object.
487//
488// + memory-efficient on most 64 bit archs
489// + O(1) insert/remove
490// + free unique (but varying) id for inserted objects
491// + cache-friendly iteration
492// - only works for pointers to structs
493//
494// NOTE: only some forms of erase/insert are available
495typedef int object_vector_index;
496
497template<class T, object_vector_index T::*indexmember>
426struct object_vector : std::vector<T *, slice_allocator<T *> > 498struct object_vector : std::vector<T *, slice_allocator<T *> >
427{ 499{
500 typedef typename object_vector::iterator iterator;
501
502 bool contains (const T *obj) const
503 {
504 return obj->*indexmember;
505 }
506
507 iterator find (const T *obj)
508 {
509 return obj->*indexmember
510 ? this->begin () + obj->*indexmember - 1
511 : this->end ();
512 }
513
514 void push_back (T *obj)
515 {
516 std::vector<T *, slice_allocator<T *> >::push_back (obj);
517 obj->*indexmember = this->size ();
518 }
519
428 void insert (T *obj) 520 void insert (T *obj)
429 { 521 {
430 assert (!(obj->*index));
431 push_back (obj); 522 push_back (obj);
432 obj->*index = this->size ();
433 } 523 }
434 524
435 void insert (T &obj) 525 void insert (T &obj)
436 { 526 {
437 insert (&obj); 527 insert (&obj);
438 } 528 }
439 529
440 void erase (T *obj) 530 void erase (T *obj)
441 { 531 {
442 assert (obj->*index);
443 unsigned int pos = obj->*index; 532 unsigned int pos = obj->*indexmember;
444 obj->*index = 0; 533 obj->*indexmember = 0;
445 534
446 if (pos < this->size ()) 535 if (pos < this->size ())
447 { 536 {
448 (*this)[pos - 1] = (*this)[this->size () - 1]; 537 (*this)[pos - 1] = (*this)[this->size () - 1];
449 (*this)[pos - 1]->*index = pos; 538 (*this)[pos - 1]->*indexmember = pos;
450 } 539 }
451 540
452 this->pop_back (); 541 this->pop_back ();
453 } 542 }
454 543
455 void erase (T &obj) 544 void erase (T &obj)
456 { 545 {
457 errase (&obj); 546 erase (&obj);
458 } 547 }
459}; 548};
460 549
461// basically does what strncpy should do, but appends "..." to strings exceeding length 550// basically does what strncpy should do, but appends "..." to strings exceeding length
462void assign (char *dst, const char *src, int maxlen); 551void assign (char *dst, const char *src, int maxlen);
468 assign ((char *)&dst, src, N); 557 assign ((char *)&dst, src, N);
469} 558}
470 559
471typedef double tstamp; 560typedef double tstamp;
472 561
473// return current time as timestampe 562// return current time as timestamp
474tstamp now (); 563tstamp now ();
475 564
476int similar_direction (int a, int b); 565int similar_direction (int a, int b);
477 566
478// like printf, but returns a std::string 567// like sprintf, but returns a "static" buffer
479const std::string format (const char *format, ...); 568const char *format (const char *format, ...);
480 569
481#endif 570#endif
482 571

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines