Move allocation macros from internal.h to alloc.h
This commit is contained in:
parent
29d9c75c4e
commit
b4a7dfe1f6
2 changed files with 74 additions and 75 deletions
74
src/alloc.h
74
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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, ...);
|
||||
|
|
|
|||
Loading…
Reference in a new issue