Compare commits

...

2 Commits

Author SHA1 Message Date
Juhani Krekelä 7345815424 Don't leak memory 2018-08-11 19:45:41 +03:00
Juhani Krekelä 1ff08c0189 Allocate smaller buffer by default 2018-08-11 13:02:15 +03:00
2 changed files with 5 additions and 3 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) {\

View File

@ -50,7 +50,7 @@ MAYBE(intmax_t_array) convert_to_numbers(char *line) {
struct intmax_t_array numbers; struct intmax_t_array numbers;
numbers.length = 0; numbers.length = 0;
numbers.alloc_size = 64 * sizeof(intmax_t); numbers.alloc_size = 8 * sizeof(intmax_t);
numbers.data = TRY_MALLOC(numbers.alloc_size * sizeof(intmax_t)); numbers.data = TRY_MALLOC(numbers.alloc_size * sizeof(intmax_t));
char *strtok_saveptr = NULL; char *strtok_saveptr = NULL;