libcdsb/src/array/memory.c

44 lines
1.1 KiB
C
Raw Normal View History

/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "include.h"
#include "../__internal/assert.h"
typedef void (*type_free)(void*);
static inline type_free get_type_free(vtype type) {
switch (type) {
default:
#ifndef NDEBUG
abort();
#endif
case VTYPE_STRING: return (void*)string_free;
case VTYPE_ARRAY: return (void*) array_free;
case VTYPE_LIST: return (void*) list_free;
case VTYPE_MAP: return (void*) map_free;
case VTYPE_SET: return (void*) vset_free;
case VTYPE_DICT: return (void*) dict_free;
}
}
/*#####################################################################################################################*/
void array_free(arr_t* x) {
if (x->size && x->type >= VTYPE_STRING) {
void* p = x->mem;
type_free free_;
assert(!is_null(p));
free_ = get_type_free(x->type);
do {
free_(p);
p += vtype_size(x->type);
} while (--x->size);
}
free(x->mem);
memset(x, 0, sizeof(*x));
}