31 lines
814 B
C
Raw Normal View History

2015-10-06 16:18:30 +02:00
/* btalloc() provides a mechanism for allocating via permuted backtraces. */
void *btalloc(size_t size, unsigned bits);
2018-05-24 17:17:37 +02:00
#define btalloc_n_proto(n) \
2015-10-06 16:18:30 +02:00
void *btalloc_##n(size_t size, unsigned bits);
btalloc_n_proto(0)
btalloc_n_proto(1)
2018-05-24 17:17:37 +02:00
#define btalloc_n_gen(n) \
2015-10-06 16:18:30 +02:00
void * \
2018-05-24 17:17:37 +02:00
btalloc_##n(size_t size, unsigned bits) { \
2015-10-06 16:18:30 +02:00
void *p; \
\
2018-05-24 17:17:37 +02:00
if (bits == 0) { \
2015-10-06 16:18:30 +02:00
p = mallocx(size, 0); \
2018-05-24 17:17:37 +02:00
} else { \
2015-10-06 16:18:30 +02:00
switch (bits & 0x1U) { \
case 0: \
p = (btalloc_0(size, bits >> 1)); \
break; \
case 1: \
p = (btalloc_1(size, bits >> 1)); \
break; \
default: not_reached(); \
} \
} \
/* Intentionally sabotage tail call optimization. */ \
assert_ptr_not_null(p, "Unexpected mallocx() failure"); \
2018-05-24 17:17:37 +02:00
return p; \
2015-10-06 16:18:30 +02:00
}