Don't leak memory
This commit is contained in:
parent
1ff08c0189
commit
7345815424
6
cmaybe.h
6
cmaybe.h
|
@ -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 = ¤t[0]->next;
|
current = ¤t[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) {\
|
||||||
|
|
Loading…
Reference in New Issue