#include "teensy_mem.h" #include "teensy_common.h" #include "teensy_list.h" #define TEMP_ALLOC_ARENA_SIZE (1024*1024) uint8_t temp_arena[TEMP_ALLOC_ARENA_SIZE]; uint8_t *next_temp_alloc = temp_arena; void *ty_talloc(size_t size) { if (next_temp_alloc > temp_arena + TEMP_ALLOC_ARENA_SIZE) ty_log_fatal(TY_ERR_MEM, "bump up the temp alloc arena size"); void *ptr = next_temp_alloc; next_temp_alloc += size; return ptr; } void ty_free_temp_allocs(void) { next_temp_alloc = temp_arena; } void *ty_alloc(size_t size) { if (size == 0) ty_log_fatal(TY_ERR_MEM, "(%s) tried to allocate 0 bytes", __func__); void *ptr = malloc(size); if (!ptr) ty_log_fatal(TY_ERR_MEM, "(%s) ran out of memory", __func__); return ptr; } void *ty_realloc(void *ptr, size_t new_size) { if (new_size == 0) { ty_free(ptr); return NULL; } // The pointer will not be registered in the tracker by realloc if it does // not yet exist. if (ptr == NULL) return ty_alloc(new_size); void *new_ptr = realloc(ptr, new_size); if (!new_ptr) ty_log_fatal(TY_ERR_MEM, "(%s) ran out of memory", __func__); return new_ptr; } void ty_free(void *ptr) { free(ptr); }