Skip to content

Commit

Permalink
mem: add mem_destructor
Browse files Browse the repository at this point in the history
This way a destruct handler can assigned after mem_alloc.
  • Loading branch information
sreimers committed Jun 20, 2022
1 parent 66c4e43 commit 4a5c0b4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/re_mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void *mem_zalloc(size_t size, mem_destroy_h *dh);
void *mem_realloc(void *data, size_t size);
void *mem_reallocarray(void *ptr, size_t nmemb,
size_t membsize, mem_destroy_h *dh);
void mem_destructor(void *data, mem_destroy_h *dh);
void *mem_ref(void *data);
void *mem_deref(void *data);
uint32_t mem_nrefs(const void *data);
Expand Down
21 changes: 21 additions & 0 deletions src/mem/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,27 @@ void *mem_reallocarray(void *ptr, size_t nmemb, size_t membsize,
}


/**
* Set or unset a destructor for a memory object
*
* @param data Memory object
* @param dh called when destroyed, NULL for remove
*/
void mem_destructor(void *data, mem_destroy_h *dh)
{
struct mem *m;

if (!data)
return;

m = ((struct mem *)data) - 1;

MAGIC_CHECK(m);

m->dh = dh;
}


/**
* Reference a reference-counted memory object
*
Expand Down

0 comments on commit 4a5c0b4

Please sign in to comment.