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);
*/
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.