blob: d32cabfed2d380af55e095d7d26c9eae440496cb (
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
47
48
49
50
51
52
53
54
55
56
57
58
|
#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);
}
|