1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
5 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * the terms of the Affero GNU General Public License as published by the |
7 | * the terms of the Affero GNU General Public License as published by the |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * option) any later version. |
9 | * 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, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
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 Affero GNU General Public License |
16 | * You should have received a copy of the Affero GNU General Public License |
17 | * and the GNU General Public License along with this program. If not, see |
17 | * and the GNU General Public License along with this program. If not, see |
18 | * <http://www.gnu.org/licenses/>. |
18 | * <http://www.gnu.org/licenses/>. |
19 | * |
19 | * |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | */ |
21 | */ |
22 | |
22 | |
23 | #ifndef UTIL_H__ |
23 | #ifndef UTIL_H__ |
24 | #define UTIL_H__ |
24 | #define UTIL_H__ |
… | |
… | |
460 | refcnt_buf (void *data, size_t size); |
460 | refcnt_buf (void *data, size_t size); |
461 | |
461 | |
462 | refcnt_buf (const refcnt_buf &src) |
462 | refcnt_buf (const refcnt_buf &src) |
463 | { |
463 | { |
464 | data = src.data; |
464 | data = src.data; |
465 | ++_refcnt (); |
465 | inc (); |
466 | } |
466 | } |
467 | |
467 | |
468 | ~refcnt_buf (); |
468 | ~refcnt_buf (); |
469 | |
469 | |
470 | refcnt_buf &operator =(const refcnt_buf &src); |
470 | refcnt_buf &operator =(const refcnt_buf &src); |
… | |
… | |
479 | return _size (); |
479 | return _size (); |
480 | } |
480 | } |
481 | |
481 | |
482 | protected: |
482 | protected: |
483 | enum { |
483 | enum { |
484 | overhead = sizeof (unsigned int) * 2 |
484 | overhead = sizeof (uint32_t) * 2 |
485 | }; |
485 | }; |
486 | |
486 | |
487 | unsigned int &_size () const |
487 | uint32_t &_size () const |
488 | { |
488 | { |
489 | return ((unsigned int *)data)[-2]; |
489 | return ((unsigned int *)data)[-2]; |
490 | } |
490 | } |
491 | |
491 | |
492 | unsigned int &_refcnt () const |
492 | uint32_t &_refcnt () const |
493 | { |
493 | { |
494 | return ((unsigned int *)data)[-1]; |
494 | return ((unsigned int *)data)[-1]; |
495 | } |
495 | } |
496 | |
496 | |
497 | void _alloc (unsigned int size) |
497 | void _alloc (uint32_t size) |
498 | { |
498 | { |
499 | data = ((char *)salloc<char> (size + overhead)) + overhead; |
499 | data = ((char *)salloc<char> (size + overhead)) + overhead; |
500 | _size () = size; |
500 | _size () = size; |
501 | _refcnt () = 1; |
501 | _refcnt () = 1; |
502 | } |
502 | } |
503 | |
503 | |
|
|
504 | void _dealloc (); |
|
|
505 | |
|
|
506 | void inc () |
|
|
507 | { |
|
|
508 | ++_refcnt (); |
|
|
509 | } |
|
|
510 | |
504 | void dec () |
511 | void dec () |
505 | { |
512 | { |
506 | if (!--_refcnt ()) |
513 | if (!--_refcnt ()) |
507 | sfree<char> (data - overhead, size () + overhead); |
514 | _dealloc (); |
508 | } |
515 | } |
509 | }; |
516 | }; |
510 | |
517 | |
511 | INTERFACE_CLASS (attachable) |
518 | INTERFACE_CLASS (attachable) |
512 | struct refcnt_base |
519 | struct refcnt_base |
… | |
… | |
654 | } |
661 | } |
655 | }; |
662 | }; |
656 | |
663 | |
657 | // This container blends advantages of linked lists |
664 | // This container blends advantages of linked lists |
658 | // (efficiency) with vectors (random access) by |
665 | // (efficiency) with vectors (random access) by |
659 | // by using an unordered vector and storing the vector |
666 | // using an unordered vector and storing the vector |
660 | // index inside the object. |
667 | // index inside the object. |
661 | // |
668 | // |
662 | // + memory-efficient on most 64 bit archs |
669 | // + memory-efficient on most 64 bit archs |
663 | // + O(1) insert/remove |
670 | // + O(1) insert/remove |
664 | // + free unique (but varying) id for inserted objects |
671 | // + free unique (but varying) id for inserted objects |
… | |
… | |
701 | insert (&obj); |
708 | insert (&obj); |
702 | } |
709 | } |
703 | |
710 | |
704 | void erase (T *obj) |
711 | void erase (T *obj) |
705 | { |
712 | { |
706 | unsigned int pos = obj->*indexmember; |
713 | object_vector_index pos = obj->*indexmember; |
707 | obj->*indexmember = 0; |
714 | obj->*indexmember = 0; |
708 | |
715 | |
709 | if (pos < this->size ()) |
716 | if (pos < this->size ()) |
710 | { |
717 | { |
711 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
718 | (*this)[pos - 1] = (*this)[this->size () - 1]; |