111 lines
2.4 KiB
C
111 lines
2.4 KiB
C
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
enum { TEST_MAX_ALLOCATIONS = 64, TEST_MAX_FREES = 64 };
|
|
|
|
typedef struct TestAllocation {
|
|
void *raw;
|
|
void *pointer;
|
|
size_t size;
|
|
void *context;
|
|
int tag;
|
|
const char *source;
|
|
int line;
|
|
} TestAllocation;
|
|
|
|
static TestAllocation allocations[TEST_MAX_ALLOCATIONS];
|
|
static void *frees[TEST_MAX_FREES];
|
|
static size_t allocation_count;
|
|
static size_t free_count;
|
|
static int fail_next_allocation;
|
|
|
|
void arena_test_reset(void) {
|
|
for (size_t index = 0; index < allocation_count; ++index) {
|
|
free(allocations[index].raw);
|
|
}
|
|
memset(allocations, 0, sizeof(allocations));
|
|
memset(frees, 0, sizeof(frees));
|
|
allocation_count = 0;
|
|
free_count = 0;
|
|
fail_next_allocation = 0;
|
|
}
|
|
|
|
void arena_test_fail_next_allocation(void) {
|
|
fail_next_allocation = 1;
|
|
}
|
|
|
|
size_t arena_test_allocation_count(void) {
|
|
return allocation_count;
|
|
}
|
|
|
|
size_t arena_test_allocation_size(size_t index) {
|
|
return allocations[index].size;
|
|
}
|
|
|
|
void *arena_test_allocation_pointer(size_t index) {
|
|
return allocations[index].pointer;
|
|
}
|
|
|
|
void *arena_test_allocation_context(size_t index) {
|
|
return allocations[index].context;
|
|
}
|
|
|
|
int arena_test_allocation_tag(size_t index) {
|
|
return allocations[index].tag;
|
|
}
|
|
|
|
const char *arena_test_allocation_source(size_t index) {
|
|
return allocations[index].source;
|
|
}
|
|
|
|
int arena_test_allocation_line(size_t index) {
|
|
return allocations[index].line;
|
|
}
|
|
|
|
size_t arena_test_free_count(void) {
|
|
return free_count;
|
|
}
|
|
|
|
void *arena_test_free_pointer(size_t index) {
|
|
return frees[index];
|
|
}
|
|
|
|
void *iggy_stbm_alloc(
|
|
void *context,
|
|
size_t size,
|
|
int tag,
|
|
const char *source,
|
|
int line) {
|
|
if (fail_next_allocation != 0) {
|
|
fail_next_allocation = 0;
|
|
return NULL;
|
|
}
|
|
if (allocation_count == TEST_MAX_ALLOCATIONS) {
|
|
return NULL;
|
|
}
|
|
|
|
void *const raw = malloc(size + 63U);
|
|
if (raw == NULL) {
|
|
return NULL;
|
|
}
|
|
const uintptr_t aligned = ((uintptr_t)raw + 63U) & ~(uintptr_t)63U;
|
|
TestAllocation *const allocation = &allocations[allocation_count++];
|
|
allocation->raw = raw;
|
|
allocation->pointer = (void *)aligned;
|
|
allocation->size = size;
|
|
allocation->context = context;
|
|
allocation->tag = tag;
|
|
allocation->source = source;
|
|
allocation->line = line;
|
|
memset(allocation->pointer, 0xa5, size);
|
|
return allocation->pointer;
|
|
}
|
|
|
|
void iggy_stbm_free(void *context, void *allocation) {
|
|
(void)context;
|
|
if (free_count < TEST_MAX_FREES) {
|
|
frees[free_count++] = allocation;
|
|
}
|
|
}
|