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) |
|
|
62 | void *g_slice_alloc (unsigned long size); |
|
|
63 | void *g_slice_alloc0 (unsigned long size); |
|
|
64 | void 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 |
… | |
… | |
172 | absdir (int d) |
182 | absdir (int d) |
173 | { |
183 | { |
174 | return ((d - 1) & 7) + 1; |
184 | return ((d - 1) & 7) + 1; |
175 | } |
185 | } |
176 | |
186 | |
|
|
187 | extern size_t slice_alloc; // statistics |
|
|
188 | |
177 | // makes dynamically allocated objects zero-initialised |
189 | // makes dynamically allocated objects zero-initialised |
178 | struct zero_initialised |
190 | struct 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 | |
207 | void *salloc_ (int n) throw (std::bad_alloc); |
223 | void *salloc_ (int n) throw (std::bad_alloc); |
… | |
… | |
225 | inline void sfree (T *ptr, int n = 1) throw () |
241 | inline 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 |
… | |
… | |
333 | |
350 | |
334 | typedef tausworthe_random_generator rand_gen; |
351 | typedef tausworthe_random_generator rand_gen; |
335 | |
352 | |
336 | extern rand_gen rndm; |
353 | extern rand_gen rndm; |
337 | |
354 | |
|
|
355 | INTERFACE_CLASS (attachable) |
|
|
356 | struct 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 |
|
|
368 | extern refcnt_base::refcnt_t refcnt_dummy; |
|
|
369 | |
338 | template<class T> |
370 | template<class T> |
339 | struct refptr |
371 | struct refptr |
340 | { |
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 | |
341 | T *p; |
392 | T *p; |
342 | |
393 | |
343 | refptr () : p(0) { } |
394 | refptr () : p(0) { } |
344 | refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); } |
395 | refptr (const refptr<T> &p) : p(p.p) { refcnt_inc (); } |
345 | refptr (T *p) : p(p) { if (p) p->refcnt_inc (); } |
396 | refptr (T *p) : p(p) { refcnt_inc (); } |
346 | ~refptr () { if (p) p->refcnt_dec (); } |
397 | ~refptr () { refcnt_dec (); } |
347 | |
398 | |
348 | const refptr<T> &operator =(T *o) |
399 | const refptr<T> &operator =(T *o) |
349 | { |
400 | { |
|
|
401 | // if decrementing ever destroys we need to reverse the order here |
350 | if (p) p->refcnt_dec (); |
402 | refcnt_dec (); |
351 | p = o; |
403 | p = o; |
352 | if (p) p->refcnt_inc (); |
404 | refcnt_inc (); |
353 | |
|
|
354 | return *this; |
405 | return *this; |
355 | } |
406 | } |
356 | |
407 | |
357 | const refptr<T> &operator =(const refptr<T> o) |
408 | const refptr<T> &operator =(const refptr<T> &o) |
358 | { |
409 | { |
359 | *this = o.p; |
410 | *this = o.p; |
360 | return *this; |
411 | return *this; |
361 | } |
412 | } |
362 | |
413 | |
363 | T &operator * () const { return *p; } |
414 | T &operator * () const { return *p; } |
364 | T *operator ->() const { return p; } |
415 | T *operator ->() const { return p; } |
365 | |
416 | |
366 | operator T *() const { return p; } |
417 | operator T *() const { return p; } |
367 | }; |
418 | }; |
368 | |
419 | |
369 | typedef refptr<maptile> maptile_ptr; |
420 | typedef refptr<maptile> maptile_ptr; |
… | |
… | |
405 | return !strcmp (a, b); |
456 | return !strcmp (a, b); |
406 | } |
457 | } |
407 | }; |
458 | }; |
408 | |
459 | |
409 | // Mostly the same as std::vector, but insert/erase can reorder |
460 | // Mostly the same as std::vector, but insert/erase can reorder |
410 | // the elements, making insret/remove O(1) instead of O(n). |
461 | // the elements, making append(=insert)/remove O(1) instead of O(n). |
411 | // |
462 | // |
412 | // NOTE: only some forms of erase/insert are available |
463 | // NOTE: only some forms of erase are available |
413 | template<class T> |
464 | template<class T> |
414 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
465 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
415 | { |
466 | { |
416 | typedef typename unordered_vector::iterator iterator; |
467 | typedef typename unordered_vector::iterator iterator; |
417 | |
468 | |
… | |
… | |
458 | return obj->*indexmember |
509 | return obj->*indexmember |
459 | ? this->begin () + obj->*indexmember - 1 |
510 | ? this->begin () + obj->*indexmember - 1 |
460 | : this->end (); |
511 | : this->end (); |
461 | } |
512 | } |
462 | |
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 | |
463 | void insert (T *obj) |
520 | void insert (T *obj) |
464 | { |
521 | { |
465 | push_back (obj); |
522 | push_back (obj); |
466 | obj->*indexmember = this->size (); |
|
|
467 | } |
523 | } |
468 | |
524 | |
469 | void insert (T &obj) |
525 | void insert (T &obj) |
470 | { |
526 | { |
471 | insert (&obj); |
527 | insert (&obj); |
… | |
… | |
501 | assign ((char *)&dst, src, N); |
557 | assign ((char *)&dst, src, N); |
502 | } |
558 | } |
503 | |
559 | |
504 | typedef double tstamp; |
560 | typedef double tstamp; |
505 | |
561 | |
506 | // return current time as timestampe |
562 | // return current time as timestamp |
507 | tstamp now (); |
563 | tstamp now (); |
508 | |
564 | |
509 | int similar_direction (int a, int b); |
565 | int similar_direction (int a, int b); |
510 | |
566 | |
511 | // like printf, but returns a std::string |
567 | // like sprintf, but returns a "static" buffer |
512 | const std::string format (const char *format, ...); |
568 | const char *format (const char *format, ...); |
513 | |
569 | |
514 | #endif |
570 | #endif |
515 | |
571 | |