Skip to content

Commit

Permalink
list: use safe_free macro
Browse files Browse the repository at this point in the history
  • Loading branch information
HiGarfield committed Sep 1, 2024
1 parent 9d2eb6e commit 5c18f2f
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@

#include "list.h"

#define safe_free(p) \
do \
{ \
if ((p) != NULL) \
{ \
free((p)); \
(p) = NULL; \
} \
} while (0)

#define EMPTYLIST NULL

void init_list(struct list *l, int keysize)
Expand Down Expand Up @@ -78,13 +88,12 @@ void delete_node(struct list *l, struct list_node *node)
node->next->previous = node->previous;
}
l->count--;
free(node);
safe_free(node);
}

void destroy_node(struct list *l, struct list_node *node)
{
free(node->data);
node->data = NULL;
safe_free(node->data);
delete_node(l, node);
}

Expand Down Expand Up @@ -154,8 +163,7 @@ void clear_list(struct list *l)
struct list_node *tmp;
tmp = l->first;
l->first = l->first->next;
free(tmp);
tmp = NULL;
safe_free(tmp);
}
l->last = EMPTYLIST;
l->count = 0;
Expand All @@ -168,10 +176,8 @@ void destroy_list(struct list *l)
struct list_node *tmp;
tmp = l->first;
l->first = l->first->next;
free(tmp->data);
tmp->data = NULL;
safe_free(tmp->data);
free(tmp);
tmp = NULL;
}
l->last = EMPTYLIST;
l->count = 0;
Expand Down

0 comments on commit 5c18f2f

Please sign in to comment.