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 | |
… | |
… | |
172 | absdir (int d) |
172 | absdir (int d) |
173 | { |
173 | { |
174 | return ((d - 1) & 7) + 1; |
174 | return ((d - 1) & 7) + 1; |
175 | } |
175 | } |
176 | |
176 | |
|
|
177 | extern size_t slice_alloc; // statistics |
|
|
178 | |
177 | // makes dynamically allocated objects zero-initialised |
179 | // makes dynamically allocated objects zero-initialised |
178 | struct zero_initialised |
180 | struct 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 | |
207 | void *salloc_ (int n) throw (std::bad_alloc); |
213 | void *salloc_ (int n) throw (std::bad_alloc); |
… | |
… | |
225 | inline void sfree (T *ptr, int n = 1) throw () |
231 | inline 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 |
… | |
… | |
333 | |
340 | |
334 | typedef tausworthe_random_generator rand_gen; |
341 | typedef tausworthe_random_generator rand_gen; |
335 | |
342 | |
336 | extern rand_gen rndm; |
343 | extern rand_gen rndm; |
337 | |
344 | |
|
|
345 | INTERFACE_CLASS (attachable) |
|
|
346 | struct 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 |
|
|
358 | extern refcnt_base::refcnt_t refcnt_dummy; |
|
|
359 | |
338 | template<class T> |
360 | template<class T> |
339 | struct refptr |
361 | struct refptr |
340 | { |
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 | |
341 | T *p; |
382 | T *p; |
342 | |
383 | |
343 | refptr () : p(0) { } |
384 | refptr () : p(0) { } |
344 | refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); } |
385 | refptr (const refptr<T> &p) : p(p.p) { refcnt_inc (); } |
345 | refptr (T *p) : p(p) { if (p) p->refcnt_inc (); } |
386 | refptr (T *p) : p(p) { refcnt_inc (); } |
346 | ~refptr () { if (p) p->refcnt_dec (); } |
387 | ~refptr () { refcnt_dec (); } |
347 | |
388 | |
348 | const refptr<T> &operator =(T *o) |
389 | const refptr<T> &operator =(T *o) |
349 | { |
390 | { |
|
|
391 | // if decrementing ever destroys we need to reverse the order here |
350 | if (p) p->refcnt_dec (); |
392 | refcnt_dec (); |
351 | p = o; |
393 | p = o; |
352 | if (p) p->refcnt_inc (); |
394 | refcnt_inc (); |
353 | |
|
|
354 | return *this; |
395 | return *this; |
355 | } |
396 | } |
356 | |
397 | |
357 | const refptr<T> &operator =(const refptr<T> o) |
398 | const refptr<T> &operator =(const refptr<T> &o) |
358 | { |
399 | { |
359 | *this = o.p; |
400 | *this = o.p; |
360 | return *this; |
401 | return *this; |
361 | } |
402 | } |
362 | |
403 | |
363 | T &operator * () const { return *p; } |
404 | T &operator * () const { return *p; } |
364 | T *operator ->() const { return p; } |
405 | T *operator ->() const { return p; } |
365 | |
406 | |
366 | operator T *() const { return p; } |
407 | operator T *() const { return p; } |
367 | }; |
408 | }; |
368 | |
409 | |
369 | typedef refptr<maptile> maptile_ptr; |
410 | typedef refptr<maptile> maptile_ptr; |
… | |
… | |
405 | return !strcmp (a, b); |
446 | return !strcmp (a, b); |
406 | } |
447 | } |
407 | }; |
448 | }; |
408 | |
449 | |
409 | // Mostly the same as std::vector, but insert/erase can reorder |
450 | // Mostly the same as std::vector, but insert/erase can reorder |
410 | // the elements, making insret/remove O(1) instead of O(n). |
451 | // the elements, making append(=insert)/remove O(1) instead of O(n). |
411 | // |
452 | // |
412 | // NOTE: only some forms of erase/insert are available |
453 | // NOTE: only some forms of erase are available |
413 | template<class T> |
454 | template<class T> |
414 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
455 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
415 | { |
456 | { |
416 | typedef typename unordered_vector::iterator iterator; |
457 | typedef typename unordered_vector::iterator iterator; |
417 | |
458 | |
… | |
… | |
458 | return obj->*indexmember |
499 | return obj->*indexmember |
459 | ? this->begin () + obj->*indexmember - 1 |
500 | ? this->begin () + obj->*indexmember - 1 |
460 | : this->end (); |
501 | : this->end (); |
461 | } |
502 | } |
462 | |
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 | |
463 | void insert (T *obj) |
510 | void insert (T *obj) |
464 | { |
511 | { |
465 | push_back (obj); |
512 | push_back (obj); |
466 | obj->*indexmember = this->size (); |
|
|
467 | } |
513 | } |
468 | |
514 | |
469 | void insert (T &obj) |
515 | void insert (T &obj) |
470 | { |
516 | { |
471 | insert (&obj); |
517 | insert (&obj); |
… | |
… | |
501 | assign ((char *)&dst, src, N); |
547 | assign ((char *)&dst, src, N); |
502 | } |
548 | } |
503 | |
549 | |
504 | typedef double tstamp; |
550 | typedef double tstamp; |
505 | |
551 | |
506 | // return current time as timestampe |
552 | // return current time as timestamp |
507 | tstamp now (); |
553 | tstamp now (); |
508 | |
554 | |
509 | int similar_direction (int a, int b); |
555 | int similar_direction (int a, int b); |
510 | |
556 | |
511 | // like printf, but returns a std::string |
557 | // like sprintf, but returns a "static" buffer |
512 | const std::string format (const char *format, ...); |
558 | const char *format (const char *format, ...); |
513 | |
559 | |
514 | #endif |
560 | #endif |
515 | |
561 | |