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.50 by root, Sun Jun 24 00:33:54 2007 UTC vs.
Revision 1.57 by root, Tue Oct 16 05:34:24 2007 UTC

1/* 1/*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. 2 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG.
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 Crossfire TRT team
5 * 5 *
6 * Crossfire TRT is free software; you can redistribute it and/or modify it 6 * Crossfire TRT 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 <crossfire@schmorp.de>
21 */ 20 */
22 21
23#ifndef UTIL_H__ 22#ifndef UTIL_H__
173absdir (int d) 172absdir (int d)
174{ 173{
175 return ((d - 1) & 7) + 1; 174 return ((d - 1) & 7) + 1;
176} 175}
177 176
177extern size_t slice_alloc; // statistics
178
178// makes dynamically allocated objects zero-initialised 179// makes dynamically allocated objects zero-initialised
179struct zero_initialised 180struct zero_initialised
180{ 181{
181 void *operator new (size_t s, void *p) 182 void *operator new (size_t s, void *p)
182 { 183 {
184 return p; 185 return p;
185 } 186 }
186 187
187 void *operator new (size_t s) 188 void *operator new (size_t s)
188 { 189 {
190 slice_alloc += s;
189 return g_slice_alloc0 (s); 191 return g_slice_alloc0 (s);
190 } 192 }
191 193
192 void *operator new[] (size_t s) 194 void *operator new[] (size_t s)
193 { 195 {
196 slice_alloc += s;
194 return g_slice_alloc0 (s); 197 return g_slice_alloc0 (s);
195 } 198 }
196 199
197 void operator delete (void *p, size_t s) 200 void operator delete (void *p, size_t s)
198 { 201 {
202 slice_alloc -= s;
199 g_slice_free1 (s, p); 203 g_slice_free1 (s, p);
200 } 204 }
201 205
202 void operator delete[] (void *p, size_t s) 206 void operator delete[] (void *p, size_t s)
203 { 207 {
208 slice_alloc -= s;
204 g_slice_free1 (s, p); 209 g_slice_free1 (s, p);
205 } 210 }
206}; 211};
207 212
208void *salloc_ (int n) throw (std::bad_alloc); 213void *salloc_ (int n) throw (std::bad_alloc);
226inline void sfree (T *ptr, int n = 1) throw () 231inline void sfree (T *ptr, int n = 1) throw ()
227{ 232{
228#ifdef PREFER_MALLOC 233#ifdef PREFER_MALLOC
229 free (ptr); 234 free (ptr);
230#else 235#else
236 slice_alloc -= n * sizeof (T);
231 g_slice_free1 (n * sizeof (T), (void *)ptr); 237 g_slice_free1 (n * sizeof (T), (void *)ptr);
232#endif 238#endif
233} 239}
234 240
235// a STL-compatible allocator that uses g_slice 241// a STL-compatible allocator that uses g_slice
334 340
335typedef tausworthe_random_generator rand_gen; 341typedef tausworthe_random_generator rand_gen;
336 342
337extern rand_gen rndm; 343extern rand_gen rndm;
338 344
345INTERFACE_CLASS (attachable)
346struct refcnt_base
347{
348 typedef int refcnt_t;
349 mutable refcnt_t ACC (RW, refcnt);
350
351 MTH void refcnt_inc () const { ++refcnt; }
352 MTH void refcnt_dec () const { --refcnt; }
353
354 refcnt_base () : refcnt (0) { }
355};
356
357// to avoid branches with more advanced compilers
358extern refcnt_base::refcnt_t refcnt_dummy;
359
339template<class T> 360template<class T>
340struct refptr 361struct refptr
341{ 362{
363 // p if not null
364 refcnt_base::refcnt_t *refcnt_ref () { return p ? &p->refcnt : &refcnt_dummy; }
365
366 void refcnt_dec ()
367 {
368 if (!is_constant (p))
369 --*refcnt_ref ();
370 else if (p)
371 --p->refcnt;
372 }
373
374 void refcnt_inc ()
375 {
376 if (!is_constant (p))
377 ++*refcnt_ref ();
378 else if (p)
379 ++p->refcnt;
380 }
381
342 T *p; 382 T *p;
343 383
344 refptr () : p(0) { } 384 refptr () : p(0) { }
345 refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); } 385 refptr (const refptr<T> &p) : p(p.p) { refcnt_inc (); }
346 refptr (T *p) : p(p) { if (p) p->refcnt_inc (); } 386 refptr (T *p) : p(p) { refcnt_inc (); }
347 ~refptr () { if (p) p->refcnt_dec (); } 387 ~refptr () { refcnt_dec (); }
348 388
349 const refptr<T> &operator =(T *o) 389 const refptr<T> &operator =(T *o)
350 { 390 {
391 // if decrementing ever destroys we need to reverse the order here
351 if (p) p->refcnt_dec (); 392 refcnt_dec ();
352 p = o; 393 p = o;
353 if (p) p->refcnt_inc (); 394 refcnt_inc ();
354
355 return *this; 395 return *this;
356 } 396 }
357 397
358 const refptr<T> &operator =(const refptr<T> o) 398 const refptr<T> &operator =(const refptr<T> &o)
359 { 399 {
360 *this = o.p; 400 *this = o.p;
361 return *this; 401 return *this;
362 } 402 }
363 403
364 T &operator * () const { return *p; } 404 T &operator * () const { return *p; }
365 T *operator ->() const { return p; } 405 T *operator ->() const { return p; }
366 406
367 operator T *() const { return p; } 407 operator T *() const { return p; }
368}; 408};
369 409
370typedef refptr<maptile> maptile_ptr; 410typedef refptr<maptile> maptile_ptr;
406 return !strcmp (a, b); 446 return !strcmp (a, b);
407 } 447 }
408}; 448};
409 449
410// Mostly the same as std::vector, but insert/erase can reorder 450// Mostly the same as std::vector, but insert/erase can reorder
411// the elements, making insret/remove O(1) instead of O(n). 451// the elements, making append(=insert)/remove O(1) instead of O(n).
412// 452//
413// NOTE: only some forms of erase/insert are available 453// NOTE: only some forms of erase are available
414template<class T> 454template<class T>
415struct unordered_vector : std::vector<T, slice_allocator<T> > 455struct unordered_vector : std::vector<T, slice_allocator<T> >
416{ 456{
417 typedef typename unordered_vector::iterator iterator; 457 typedef typename unordered_vector::iterator iterator;
418 458
459 return obj->*indexmember 499 return obj->*indexmember
460 ? this->begin () + obj->*indexmember - 1 500 ? this->begin () + obj->*indexmember - 1
461 : this->end (); 501 : this->end ();
462 } 502 }
463 503
504 void push_back (T *obj)
505 {
506 std::vector<T *, slice_allocator<T *> >::push_back (obj);
507 obj->*indexmember = this->size ();
508 }
509
464 void insert (T *obj) 510 void insert (T *obj)
465 { 511 {
466 push_back (obj); 512 push_back (obj);
467 obj->*indexmember = this->size ();
468 } 513 }
469 514
470 void insert (T &obj) 515 void insert (T &obj)
471 { 516 {
472 insert (&obj); 517 insert (&obj);
507// return current time as timestampe 552// return current time as timestampe
508tstamp now (); 553tstamp now ();
509 554
510int similar_direction (int a, int b); 555int similar_direction (int a, int b);
511 556
512// like printf, but returns a std::string 557// like sprintf, but returns a "static" buffer
513const std::string format (const char *format, ...); 558const char *format (const char *format, ...);
514 559
515#endif 560#endif
516 561

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines