From b4a7dfe1f651e6a8befa7fa754196b9173b1d2f6 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Fri, 22 Apr 2022 21:43:19 +0200 Subject: [PATCH] Move allocation macros from internal.h to alloc.h --- src/alloc.h | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ src/internal.h | 75 -------------------------------------------------- 2 files changed, 74 insertions(+), 75 deletions(-) diff --git a/src/alloc.h b/src/alloc.h index 6032b5b..9297e4c 100644 --- a/src/alloc.h +++ b/src/alloc.h @@ -21,6 +21,80 @@ extern "C" { #define ALLOC_OBJ(a, T) ALLOC_LIST(a, T, 1) #define FREE_OBJ(a, ptr) FREE_LIST(a, ptr, 1) +#define DEINIT_CAP_LIST(allocator, items, len, cap, item_deinit) \ +do { \ + if ((items) == NULL) { \ + break; \ + } \ + for (size_t i = 0; i < (len); i++) { \ + item_deinit(allocator, &((items)[i])); \ + } \ + FREE_LIST(allocator, items, cap); \ + len = 0; \ + cap = 0; \ + (items) = NULL; \ +} while(0) + +#define DEINIT_LIST(allocator, items, len, item_deinit) \ + do { \ + if ((items) == NULL) { \ + break; \ + } \ + for (size_t i = 0; i < (len); i++) { \ + item_deinit(allocator, &((items)[i])); \ + } \ + FREE_LIST(allocator, items, len); \ + len = 0; \ + (items) = NULL; \ + } while(0) + +#define DEINIT_LIST_WITH_ARGS(allocator, items, len, item_deinit, ...) \ + do { \ + if ((items) == NULL) { \ + break; \ + } \ + for (size_t i = 0; i < (len); i++) { \ + item_deinit(__VA_ARGS__, &((items)[i])); \ + } \ + FREE_LIST(allocator, items, len); \ + len = 0; \ + (items) = NULL; \ + } while(0) + +#define DEINIT_CAP_LIST_WITH_ARGS(allocator, items, len, cap, item_deinit, ...) \ + do { \ + if ((items) == NULL) { \ + break; \ + } \ + for (size_t i = 0; i < (len); i++) { \ + item_deinit(__VA_ARGS__, &((items)[i])); \ + } \ + FREE_LIST(allocator, items, cap); \ + len = 0; \ + cap = 0; \ + (items) = NULL; \ + } while(0) + +#define MOVEPTR(out, in) \ + do { \ + out = in; \ + in = NULL; \ + } while(0) + +// DESTROY destroys a dynamically allocated value. +// It will first deinit the value using deiniter, +// free the memory and then set the variable to NULL. +// It is always allowed to destroy an already destroyed +// or deinited value. +#define DESTROY(allocator, var, deiniter) \ + do { \ + if ((var) == NULL) { \ + break; \ + } \ + deiniter(allocator, var); \ + FREE_OBJ(allocator, var); \ + (var) = NULL; \ + } while(0) #ifdef __cplusplus } diff --git a/src/internal.h b/src/internal.h index 869eb60..dbdd2eb 100644 --- a/src/internal.h +++ b/src/internal.h @@ -10,81 +10,6 @@ extern "C" { #include "gc.h" -#define DEINIT_CAP_LIST(allocator, items, len, cap, item_deinit) \ -do { \ - if ((items) == NULL) { \ - break; \ - } \ - for (size_t i = 0; i < (len); i++) { \ - item_deinit(allocator, &((items)[i])); \ - } \ - FREE_LIST(allocator, items, cap); \ - len = 0; \ - cap = 0; \ - (items) = NULL; \ -} while(0) - -#define DEINIT_LIST(allocator, items, len, item_deinit) \ - do { \ - if ((items) == NULL) { \ - break; \ - } \ - for (size_t i = 0; i < (len); i++) { \ - item_deinit(allocator, &((items)[i])); \ - } \ - FREE_LIST(allocator, items, len); \ - len = 0; \ - (items) = NULL; \ - } while(0) - -#define DEINIT_LIST_WITH_ARGS(allocator, items, len, item_deinit, ...) \ - do { \ - if ((items) == NULL) { \ - break; \ - } \ - for (size_t i = 0; i < (len); i++) { \ - item_deinit(__VA_ARGS__, &((items)[i])); \ - } \ - FREE_LIST(allocator, items, len); \ - len = 0; \ - (items) = NULL; \ - } while(0) - -#define DEINIT_CAP_LIST_WITH_ARGS(allocator, items, len, cap, item_deinit, ...) \ - do { \ - if ((items) == NULL) { \ - break; \ - } \ - for (size_t i = 0; i < (len); i++) { \ - item_deinit(__VA_ARGS__, &((items)[i])); \ - } \ - FREE_LIST(allocator, items, cap); \ - len = 0; \ - cap = 0; \ - (items) = NULL; \ - } while(0) - -#define MOVEPTR(out, in) \ - do { \ - out = in; \ - in = NULL; \ - } while(0) - -// DESTROY destroys a dynamically allocated value. -// It will first deinit the value using deiniter, -// free the memory and then set the variable to NULL. -// It is always allowed to destroy an already destroyed -// or deinited value. -#define DESTROY(allocator, var, deiniter) \ - do { \ - if ((var) == NULL) { \ - break; \ - } \ - deiniter(allocator, var); \ - FREE_OBJ(allocator, var); \ - (var) = NULL; \ - } while(0) - // Internal use only functions void apfl_print_indented(unsigned indent, FILE *, const char* fmt, ...);