From 4dfe971282d6bb4813582f561a49a0d378a0d3a2 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 26 Aug 2022 12:46:59 +0300 Subject: [PATCH] Update container free methods (#58) --- include/vtype.h | 2 +- src/array/memory.c | 2 ++ src/dict/memory.c | 1 + src/list/memory.c | 2 ++ src/map/memory.c | 2 ++ src/set/memory.c | 2 ++ 6 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/vtype.h b/include/vtype.h index e47855c..c9195ab 100644 --- a/include/vtype.h +++ b/include/vtype.h @@ -144,7 +144,7 @@ inline size_t array_size (const vtype_array* x) { return x->size; } inline size_t dict_size (const vtype_dict* x) { return x->size; } inline size_t dict_capacity(const vtype_dict* x) { return x->capacity; } inline size_t string_nmemb (const vtype_string* x) { return (x->buffer) ? libcdsb_strlen(x->buffer) : 0; } -inline void string_free ( vtype_string* x) { libcdsb_free(x->buffer); x->buffer = 0; } +inline void string_free ( vtype_string* x) { if (x) { libcdsb_free(x->buffer); x->buffer = 0; } } inline vtype_string string_copy(const vtype_string* s) { vtype_string x = { .buffer = libcdsb_strdup(s->buffer) }; diff --git a/src/array/memory.c b/src/array/memory.c index 503bc98..b96ce78 100644 --- a/src/array/memory.c +++ b/src/array/memory.c @@ -5,6 +5,8 @@ #include "../__internal/assert.h" void array_free(arr_t* x) { + if (is_null(x)) return; + if (x->size && x->type >= VTYPE_STRING) { void* p = x->mem; diff --git a/src/dict/memory.c b/src/dict/memory.c index 59234ea..37ae354 100644 --- a/src/dict/memory.c +++ b/src/dict/memory.c @@ -4,6 +4,7 @@ #include "include.h" void dict_free(dict_t* x) { + if (is_null(x)) return; while (x->capacity--) { while (!is_null(x->nodes[x->capacity])) { diff --git a/src/list/memory.c b/src/list/memory.c index b215538..da0944f 100644 --- a/src/list/memory.c +++ b/src/list/memory.c @@ -7,6 +7,8 @@ void list_free(list_t* x) { lnode_t* c; lnode_t* next; + if (is_null(x)) return; + c = x->first; while (!is_null(c)) { diff --git a/src/map/memory.c b/src/map/memory.c index 0d8c25a..784c7da 100644 --- a/src/map/memory.c +++ b/src/map/memory.c @@ -12,6 +12,8 @@ void map_init(map_t* x, vtype t) { void map_free(map_t* x) { mnode_t *t, *c; + if (is_null(x)) return; + c = x->root; while (!mnode_is_empty(x->root)) { diff --git a/src/set/memory.c b/src/set/memory.c index e0e3c2b..e71e6ca 100644 --- a/src/set/memory.c +++ b/src/set/memory.c @@ -12,6 +12,8 @@ void vset_init(set_t* x, vtype t) { void vset_free(set_t* x) { rbnode_t *t, *c; + if (is_null(x)) return; + c = x->root; while (!rbnode_is_empty(x->root)) {