Don't leak memory

This commit is contained in:
Juhani Krekelä 2018-08-11 19:45:41 +03:00
parent 1ff08c0189
commit 7345815424
1 changed files with 4 additions and 2 deletions

View File

@ -56,11 +56,13 @@ struct __free_list {
) )
#define TRY_MALLOC(size) TRY_MEMALLOC(malloc, size) #define TRY_MALLOC(size) TRY_MEMALLOC(malloc, size)
static void __remove_free_list(struct __free_list **head, void *ptr) { static void __remove_free_list(struct __free_list **head, void *ptr, void(*free)(void*)) {
struct __free_list **current = head; struct __free_list **current = head;
while (*current != NULL) { while (*current != NULL) {
if (current[0]->ptr == ptr) { if (current[0]->ptr == ptr) {
struct __free_list *deleted = *current;
*current = current[0]->next; *current = current[0]->next;
free(deleted);
break; break;
} }
current = &current[0]->next; current = &current[0]->next;
@ -68,7 +70,7 @@ static void __remove_free_list(struct __free_list **head, void *ptr) {
} }
#define TRY_FREE(allocation) (\ #define TRY_FREE(allocation) (\
free(allocation),\ free(allocation),\
__remove_free_list(&__allocations, allocation)\ __remove_free_list(&__allocations, allocation, free)\
) )
#define TRY_FREE_ALL() \ #define TRY_FREE_ALL() \
while (__allocations != NULL) {\ while (__allocations != NULL) {\