#ifndef TEENSY_MEM_H_ #define TEENSY_MEM_H_ #include #include #define ty_new(T) (ty_alloc(sizeof(T))) // A specific section of memory allocated for an arena. typedef struct Arena_Hunk Arena_Hunk; struct Arena_Hunk { Arena_Hunk *next; uint8_t *start; uint8_t *end; uint8_t *head; }; // Memory arena. It allows the user of this API to allocate lots of memory and // free it all at once. Arenas do not support reallocation. typedef struct { Arena_Hunk *hunks; size_t hunk_size; size_t alloced_size; } Arena; // Makes an allocation with the speicified arena. void *ty_arena_alloc(Arena *arena, size_t size); // Frees all allocations made with this arena. void ty_arena_clear(Arena *arena); // Frees the arena itself. Do NOT call to free allocations made with the arena. // Use `ty_arena_clear()` for that. void ty_arena_free(Arena *arena); void ty_clear_temp_allocs(void); // Temp allocation. Freed at the end of every frame. Do NOT realloc. void *ty_talloc(size_t size); // These mem_* functions handle the case of a bad allocation. No need to check // for NULL after allocating with these. In debug builds, they will also track // allocations and report any memory still in use when the program exits. void *ty_alloc(size_t size); void *ty_realloc(void *ptr, size_t new_size); void ty_free(void *ptr); #endif // TEENSY_MEM_H_