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.116 by root, Sat Dec 31 06:18:01 2011 UTC vs.
Revision 1.119 by root, Thu Jan 26 19:01:22 2012 UTC

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.
324 if (expect_true (ptr)) 324 if (expect_true (ptr))
325 { 325 {
326 slice_alloc -= n * sizeof (T); 326 slice_alloc -= n * sizeof (T);
327 if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); 327 if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T));
328 g_slice_free1 (n * sizeof (T), (void *)ptr); 328 g_slice_free1 (n * sizeof (T), (void *)ptr);
329 assert (slice_alloc >= 0);//D
330 } 329 }
331} 330}
332 331
333// nulls the pointer 332// nulls the pointer
334template<typename T> 333template<typename T>
447 } 446 }
448 447
449 void destroy (pointer p) 448 void destroy (pointer p)
450 { 449 {
451 p->~Tp (); 450 p->~Tp ();
451 }
452};
453
454// basically a memory area, but refcounted
455struct refcnt_buf
456{
457 char *data;
458
459 refcnt_buf (size_t size = 0);
460 refcnt_buf (void *data, size_t size);
461
462 refcnt_buf (const refcnt_buf &src)
463 {
464 data = src.data;
465 ++_refcnt ();
466 }
467
468 ~refcnt_buf ();
469
470 refcnt_buf &operator =(const refcnt_buf &src);
471
472 operator char *()
473 {
474 return data;
475 }
476
477 size_t size () const
478 {
479 return _size ();
480 }
481
482protected:
483 enum {
484 overhead = sizeof (unsigned int) * 2
485 };
486
487 unsigned int &_size () const
488 {
489 return ((unsigned int *)data)[-2];
490 }
491
492 unsigned int &_refcnt () const
493 {
494 return ((unsigned int *)data)[-1];
495 }
496
497 void _alloc (unsigned int size)
498 {
499 data = ((char *)salloc<char> (size + overhead)) + overhead;
500 _size () = size;
501 _refcnt () = 1;
502 }
503
504 void dec ()
505 {
506 if (!--_refcnt ())
507 sfree<char> (data - overhead, size () + overhead);
452 } 508 }
453}; 509};
454 510
455INTERFACE_CLASS (attachable) 511INTERFACE_CLASS (attachable)
456struct refcnt_base 512struct refcnt_base
598 } 654 }
599}; 655};
600 656
601// This container blends advantages of linked lists 657// This container blends advantages of linked lists
602// (efficiency) with vectors (random access) by 658// (efficiency) with vectors (random access) by
603// by using an unordered vector and storing the vector 659// using an unordered vector and storing the vector
604// index inside the object. 660// index inside the object.
605// 661//
606// + memory-efficient on most 64 bit archs 662// + memory-efficient on most 64 bit archs
607// + O(1) insert/remove 663// + O(1) insert/remove
608// + free unique (but varying) id for inserted objects 664// + free unique (but varying) id for inserted objects
645 insert (&obj); 701 insert (&obj);
646 } 702 }
647 703
648 void erase (T *obj) 704 void erase (T *obj)
649 { 705 {
650 unsigned int pos = obj->*indexmember; 706 object_vector_index pos = obj->*indexmember;
651 obj->*indexmember = 0; 707 obj->*indexmember = 0;
652 708
653 if (pos < this->size ()) 709 if (pos < this->size ())
654 { 710 {
655 (*this)[pos - 1] = (*this)[this->size () - 1]; 711 (*this)[pos - 1] = (*this)[this->size () - 1];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines