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.117 by root, Tue Jan 3 11:23:41 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines