#ifndef CMETAL_ALLOC_H
#define CMETAL_ALLOC_H
#include <stdlib.h>
#ifdef TEST
static int cmetal_allocs_until_fail = -1;
static inline void cmetal_fail_next_alloc(void) {
cmetal_allocs_until_fail = 0;
}
static inline void cmetal_alloc_reset(void) {
cmetal_allocs_until_fail = -1;
}
static inline int cmetal_alloc_should_fail(void) {
if (cmetal_allocs_until_fail == 0) {
cmetal_allocs_until_fail = -1;
return 1;
}
if (cmetal_allocs_until_fail > 0) {
cmetal_allocs_until_fail--;
}
return 0;
}
static inline void *cmetal_test_malloc(size_t size) {
return cmetal_alloc_should_fail() ? NULL : malloc(size);
}
static inline void *cmetal_test_realloc(void *ptr, size_t size) {
return cmetal_alloc_should_fail() ? NULL : realloc(ptr, size);
}
#define CMETAL_MALLOC cmetal_test_malloc
#define CMETAL_REALLOC cmetal_test_realloc
#else
#define CMETAL_MALLOC malloc
#define CMETAL_REALLOC realloc
#endif
#endif