1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
/*
* Written by Poul-Henning Kamp <phk@phk.freebsd.dk>
*
* This file is in the public domain.
*
*/
#define ZERO_OBJ(to, sz) \
do { \
void *(*volatile z_obj)(void *, int, size_t) = memset; \
(void)z_obj(to, 0, sz); \
} while (0)
#define INIT_OBJ(to, type_magic) \
do { \
(void)memset(to, 0, sizeof *(to)); \
(to)->magic = (type_magic); \
} while (0)
#define FINI_OBJ(to) \
do { \
ZERO_OBJ(&(to)->magic, sizeof (to)->magic); \
to = NULL; \
} while (0)
#define ALLOC_OBJ(to, type_magic) \
do { \
(to) = calloc(1, sizeof *(to)); \
if ((to) != NULL) \
(to)->magic = (type_magic); \
} while (0)
#define ALLOC_OBJ_EXTRA(to, extra_size, type_magic) \
do { \
(to) = calloc(1, sizeof(*(to)) + (extra_size)); \
if ((to) != NULL) \
(to)->magic = (type_magic); \
} while (0)
#define SIZEOF_FLEX_OBJ(to, fld, len) \
(offsetof(typeof(*to), fld) + sizeof *(to)->fld * (len))
#define ALLOC_FLEX_OBJ(to, fld, len, type_magic) \
do { \
(to) = calloc(1, SIZEOF_FLEX_OBJ(to, fld, len)); \
if ((to) != NULL) \
(to)->magic = (type_magic); \
} while (0)
#define FREE_OBJ(to) \
do { \
ZERO_OBJ(&(to)->magic, sizeof (to)->magic); \
free(to); \
to = NULL; \
} while (0)
#define VALID_OBJ(ptr, type_magic) \
((ptr) != NULL && (ptr)->magic == (type_magic))
#define CHECK_OBJ(ptr, type_magic) \
do { \
assert((ptr)->magic == type_magic); \
} while (0)
#define CHECK_OBJ_NOTNULL(ptr, type_magic) \
do { \
assert((ptr) != NULL); \
assert((ptr)->magic == type_magic); \
} while (0)
#define CHECK_OBJ_ORNULL(ptr, type_magic) \
do { \
if ((ptr) != NULL) \
assert((ptr)->magic == type_magic); \
} while (0)
#define CAST_OBJ(to, from, type_magic) \
do { \
(to) = (from); \
if ((to) != NULL) \
CHECK_OBJ((to), (type_magic)); \
} while (0)
#define CAST_OBJ_NOTNULL(to, from, type_magic) \
do { \
(to) = (from); \
AN((to)); \
CHECK_OBJ((to), (type_magic)); \
} while (0)
#define TAKE_OBJ_NOTNULL(to, pfrom, type_magic) \
do { \
AN((pfrom)); \
(to) = *(pfrom); \
*(pfrom) = NULL; \
CHECK_OBJ_NOTNULL((to), (type_magic)); \
} while (0)
#define REPLACE(ptr, val) \
do { \
const char *_vreplace = (val); \
free(ptr); \
if (_vreplace != NULL) { \
ptr = strdup(_vreplace); \
AN((ptr)); \
} else { \
ptr = NULL; \
} \
} while (0)
|