libcdsb/src/array/memory.c

35 lines
978 B
C

/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "include.h"
#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;
assert(!is_null(p));
do {
switch (x->type) {
default:
#ifndef NDEBUG
abort();
#endif
case VTYPE_STRING: string_free(p); break;
case VTYPE_ARRAY: array_free(p); break;
case VTYPE_LIST: list_free(p); break;
case VTYPE_MAP: map_free(p); break;
case VTYPE_SET: vset_free(p); break;
case VTYPE_DICT: dict_free(p); break;
}
p += vtype_size(x->type);
} while (--x->size);
}
free(x->mem);
memset(x, 0, sizeof(*x));
}