apfl/src/internal.h

56 lines
1.5 KiB
C
Raw Normal View History

2021-12-10 20:22:16 +00:00
#ifndef APFL_INTERNAL_H
#define APFL_INTERNAL_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
2021-12-10 20:22:16 +00:00
#include <stdlib.h>
#include "common.h"
#define DEINIT_LIST(items, len, item_deinit) \
do { \
2021-12-15 20:26:13 +00:00
if ((items) == NULL) { \
break; \
} \
2021-12-10 20:22:16 +00:00
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, ...);
struct apfl_list *apfl_list_new(void);
void apfl_list_deinit(struct apfl_list *);
2021-12-10 20:22:16 +00:00
#ifdef __cplusplus
}
#endif
#endif