blob: b4cdc626b656509c6a707aef35b547e62c4b9d4f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
#ifndef TEENSY_MEM_H_
#define TEENSY_MEM_H_
#include <stdint.h>
#include <stdlib.h>
#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_
|