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