aboutsummaryrefslogtreecommitdiff
path: root/teensy/teensy_mem.c
blob: 735cf5537f6555c0adfbf504aeff1368de573a2e (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#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;

#ifdef TEENSY_DEBUG

#include <execinfo.h>

#define BACKTRACE_SIZE 16

struct alloc {
    void *ptr;
    size_t size;
    int backtrace_len;
    char **backtrace;
};

int allocations_cap;
int allocations_len;
struct alloc *allocations;

#endif // TODO TEENSY_DEBUG

void ty_init_mem(void)
{
#ifdef TEENSY_DEBUG
    allocations_len = 0;
    allocations_cap = 8;
    allocations = malloc(sizeof(struct alloc) * 8);
    if (!allocations)
        ty_log_fatal(TY_ERR_MEM, "(%s) (track) ran out of memory", __func__);
#endif // TEENSY_DEBUG
}

void ty_deinit_mem(void)
{
#ifdef TEENSY_DEBUG
    fprintf(stderr, "%d allocations leaked\n", allocations_len);
    for (int i = 0; i < allocations_len; i++) {
        struct alloc *alloc = &allocations[i];
        fprintf(
            stderr,
            "leaked %zu bytes at %p\n",
            alloc->size,
            alloc->ptr
        );
        for (int j = 0; j < alloc->backtrace_len; j++) {
            fprintf(stderr, "\t%s\n", alloc->backtrace[j]);
        }
        putc('\n', stderr);

        free(alloc->backtrace);
    }
    free(allocations);

    if (allocations_len > 0)
        exit(TY_ERR_MEM_LEAK);
#endif // TEENSY_DEBUG
}

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)
{
    void *ptr = malloc(size);
    if (!ptr)
        ty_log_fatal(TY_ERR_MEM, "(%s) ran out of memory", __func__);

#ifdef TEENSY_DEBUG
    // Log allocation
    struct alloc alloc;
    void *bt[BACKTRACE_SIZE];
    int bt_len = backtrace(bt, BACKTRACE_SIZE);
    char **symbols = backtrace_symbols(bt, bt_len);

    alloc.ptr = ptr;
    alloc.size = size;
    alloc.backtrace_len = bt_len;
    alloc.backtrace = symbols;

    if (allocations_len + 1 > allocations_cap) {
        allocations_cap *= 2;
        allocations = realloc(
            allocations,
            sizeof(struct alloc) * allocations_cap
        );
        if (!allocations)
            ty_log_fatal(
                TY_ERR_MEM,
                "(%s) (track) ran out of memory",
                __func__
            );
    }

    allocations[allocations_len++] = alloc;
#endif // TEENSY_DEBUG

    return ptr;
}

void *ty_realloc(void *ptr, size_t new_size)
{
    if (new_size == 0) {
        ty_free(ptr);
        return NULL;
    }

    void *new_ptr = realloc(ptr, new_size);
    if (!new_ptr)
        ty_log_fatal(TY_ERR_MEM, "(%s) ran out of memory", __func__);

#ifdef TEENSY_DEBUG
    for (int i = 0; i < allocations_len; i++) {
        struct alloc *alloc = &allocations[i];
        if (alloc->ptr == ptr) {
            alloc->ptr = new_ptr;
            alloc->size = new_size;
            break;
        }
    }
#endif // TEENSY_DEBUG

    return new_ptr;
}

void ty_free(void *ptr)
{
    free(ptr);

#ifdef TEENSY_DEBUG
    for (int i = 0; i < allocations_len; i++) {
        struct alloc *alloc = &allocations[i];
        if (alloc->ptr == ptr) {
            free(alloc->backtrace);
            *alloc = allocations[allocations_len - 1];
            allocations_len--;
            break;
        }
    }
#endif // TEENSY_DEBUG
}

int ty_alloc_count(void)
{
#ifdef TEENSY_DEBUG
    return allocations_len;
#else
    return -1;
#endif
}