apfl/src/internal.h
Laria Carolin Chabowski 0384875ef3 values: Split lists into read-only lists and editable lists
This is analogous to dictionaries and ensures that no circular references
can be created when using the exported API in apfl.h.

This also changes apfl_value_copy into apfl_value_incref to better reflect
what it does and to reflect that it is no longer an operation that can
fail.
2022-01-04 23:11:38 +01:00

57 lines
1.6 KiB
C

#ifndef APFL_INTERNAL_H
#define APFL_INTERNAL_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
#define DEINIT_LIST(items, len, item_deinit) \
do { \
if ((items) == NULL) { \
break; \
} \
for (size_t i = 0; i < (len); i++) { \
item_deinit(&((items)[i])); \
} \
len = 0; \
free(items); \
(items) = NULL; \
} while(0)
#define MOVEPTR(out, in) \
do { \
out = in; \
in = NULL; \
} while(0)
// ALLOC_LIST allocates memory for a list of n values of type T.
// n == 0 will always result in NULL (not guaranteed by calloc()) and the
// result will be cast into a pointer to T (this way the compiler can warn us,
// if we try to allocate memory for a wrong type). Also if we always use
// calloc(), the allocated memory is in a defined state.
#define ALLOC_LIST(T, n) (T *)((n) == 0 ? NULL : calloc((n), sizeof(T)))
#define ALLOC(T) ALLOC_LIST(T, 1)
// Aliases to commonly used functions / macros
#define DESTROY APFL_DESTROY
// Internal use only functions
void apfl_print_indented(unsigned indent, FILE *, const char* fmt, ...);
/* Decrease a refererence count. Returns true, if the object should be freed.
*/
bool apfl_refcount_dec(unsigned *);
#ifdef __cplusplus
}
#endif
#endif