From: Michael D. Lowis Date: Thu, 23 Oct 2014 02:19:31 +0000 (-0400) Subject: Added mem_swap routine and update mem_release to accept null pointers X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=6f230a518de102ec91186f35ffff401546fd4878;p=projs%2Flibcds.git Added mem_swap routine and update mem_release to accept null pointers --- diff --git a/source/mem/mem.c b/source/mem/mem.c index c926c5c..dae4729 100644 --- a/source/mem/mem.c +++ b/source/mem/mem.c @@ -69,22 +69,30 @@ void* mem_retain(void* p_obj) void mem_release(void* p_obj) { obj_t* p_hdr; - assert(NULL != p_obj); - p_hdr = (((obj_t*)p_obj)-1); - p_hdr->refcount -= 1; - if(p_hdr->refcount < 1) - { -#if (LEAK_DETECT_LEVEL > 0) - Num_Allocations--; -#endif - if(p_hdr->p_finalize) + if (NULL != p_obj) { + p_hdr = (((obj_t*)p_obj)-1); + p_hdr->refcount -= 1; + if(p_hdr->refcount < 1) { - p_hdr->p_finalize(p_obj); + #if (LEAK_DETECT_LEVEL > 0) + Num_Allocations--; + #endif + if(p_hdr->p_finalize) + { + p_hdr->p_finalize(p_obj); + } + free(p_hdr); } - free(p_hdr); } } +void mem_swap(void** loc, void* obj) +{ + void* old = *loc; + *loc = obj; + mem_release(old); +} + void* mem_box(intptr_t val) { box_t* p_box = (box_t*)mem_allocate(sizeof(box_t), NULL); diff --git a/source/mem/mem.h b/source/mem/mem.h index 3ad87dc..0e6e846 100644 --- a/source/mem/mem.h +++ b/source/mem/mem.h @@ -53,6 +53,15 @@ void* mem_retain(void* p_obj); */ void mem_release(void* p_obj); +/** + * @brief Replaces the object reference in the given location with the new + * object releasing the old one. + * + * @param loc Pointer to the location where the reference is kept. + * @param obj The new object the location will point to. + */ +void mem_swap(void** loc, void* obj); + /** * @brief Create a reference counted box holding the given value so that it can * be placed in a container.