#define TINY_ALLOCATOR_IMPLEMENTATION #include "base/allocator.h" #include #include static size_t fail_index = SIZE_MAX; static size_t calls; static size_t outstanding; static int should_fail(void) { return calls++ == fail_index; } void * tiny_alloc(size_t size) { void *result; if (should_fail()) { return NULL; } result = malloc(size); if (result != NULL) { outstanding++; } return result; } void * tiny_calloc(size_t count, size_t size) { void *result; if ((size != 0 && count > SIZE_MAX / size) || should_fail()) { return NULL; } result = calloc(count, size); if (result != NULL) { outstanding++; } return result; } void * tiny_realloc(void *memory, size_t size) { void *result; if (size == 0) { tiny_free(memory); return NULL; } if (should_fail()) { return NULL; } result = realloc(memory, size); if (result != NULL && memory == NULL) { outstanding++; } return result; } void tiny_free(void *memory) { if (memory != NULL) { free(memory); if (outstanding > 0) { outstanding--; } } } void tiny_allocator_reset(void) { fail_index = SIZE_MAX; calls = 0; } void tiny_allocator_fail_after(size_t allocation_index) { fail_index = allocation_index; calls = 0; } size_t tiny_allocator_call_count(void) { return calls; } size_t tiny_allocator_outstanding(void) { return outstanding; }