… | |
… | |
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 |
334 | template<typename T> |
333 | template<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 |
|
|
455 | struct 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 | |
|
|
482 | protected: |
|
|
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 | |
455 | INTERFACE_CLASS (attachable) |
511 | INTERFACE_CLASS (attachable) |
456 | struct refcnt_base |
512 | struct refcnt_base |