aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build.env2
-rw-r--r--platform/desktop/desktop.c6
-rw-r--r--teensy/teensy_context.c3
-rw-r--r--teensy/teensy_mem.c144
-rw-r--r--teensy/teensy_mem.h8
5 files changed, 8 insertions, 155 deletions
diff --git a/build.env b/build.env
index 7689a7f..4aa7b5c 100644
--- a/build.env
+++ b/build.env
@@ -1,4 +1,4 @@
CC=cc
PLATFORM=desktop
OUT=demonchime
-DBFLAGS='-g -ggdb -rdynamic -DTEENSY_DEBUG'
+DBFLAGS='-g -ggdb -rdynamic -DTEENSY_DEBUG -fsanitize=address'
diff --git a/platform/desktop/desktop.c b/platform/desktop/desktop.c
index 8de6d8d..56c8dee 100644
--- a/platform/desktop/desktop.c
+++ b/platform/desktop/desktop.c
@@ -186,7 +186,13 @@ void ty_platform_deinit(void)
ty_log_info("deinitializing GLFW...");
glfwDestroyWindow(p.win);
+
+ // If asan is used, it will give a ton of false positives here, but won't
+ // spit out any errors if memory is actually leaked here, so I just leak it
+ // to avoid the errors.
+#ifndef __SANITIZE_ADDRESS__
glfwTerminate();
+#endif
}
void ty_platform_frame(ty_Image img)
diff --git a/teensy/teensy_context.c b/teensy/teensy_context.c
index 2eb1df3..f401ede 100644
--- a/teensy/teensy_context.c
+++ b/teensy/teensy_context.c
@@ -21,8 +21,6 @@ bool is_init(void)
void ty_init(ty_Hints hints)
{
- ty_init_mem();
-
ctx.hints = hints;
ctx.ticrate = 1.0 / hints.ticrate;
ctx.prev_tic_ts = 0;
@@ -36,7 +34,6 @@ void ty_deinit(void)
assert(is_init());
ty_platform_deinit();
ty_deinit_renderer();
- ty_deinit_mem();
}
bool ty_button_down(ty_Button btn)
diff --git a/teensy/teensy_mem.c b/teensy/teensy_mem.c
index 65b7486..d32cabf 100644
--- a/teensy/teensy_mem.c
+++ b/teensy/teensy_mem.c
@@ -8,67 +8,11 @@
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
-
-typedef struct {
- void *ptr;
- size_t size;
- int backtrace_len;
- char **backtrace;
-} Alloc;
-
-int allocations_cap;
-int allocations_len;
-Alloc *allocations;
-
-#endif // TODO TEENSY_DEBUG
-
-void ty_init_mem(void)
-{
-#ifdef TEENSY_DEBUG
- allocations_len = 0;
- allocations_cap = 8;
- allocations = malloc(sizeof(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++) {
- 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;
@@ -86,36 +30,6 @@ 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
- 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(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;
}
@@ -135,66 +49,10 @@ void *ty_realloc(void *ptr, size_t 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++) {
- 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)
{
-#ifdef TEENSY_DEBUG
- bool double_free = true;
-
- for (int i = 0; i < allocations_len; i++) {
- Alloc *alloc = &allocations[i];
- if (alloc->ptr == ptr) {
- free(alloc->backtrace);
- *alloc = allocations[allocations_len - 1];
- allocations_len--;
- double_free = false;
- break;
- }
- }
-
- if (double_free) {
- ty_log_fatal(
- TY_ERR_MEM,
- "Double free or freed unowned memory (%p)",
- ptr
- );
- }
-#endif // TEENSY_DEBUG
free(ptr);
}
-
-int ty_alloc_count(void)
-{
-#ifdef TEENSY_DEBUG
- return allocations_len;
-#else
- return -1;
-#endif
-}
-
-size_t ty_alloc_size(void)
-{
-#ifdef TEENSY_DEBUG
- size_t total = 0;
- for (int i = 0; i < allocations_len; i++) {
- Alloc *alloc = &allocations[i];
- total += alloc->size;
- }
- return total;
-#else
- return 0;
-#endif
-}
diff --git a/teensy/teensy_mem.h b/teensy/teensy_mem.h
index 8639479..133869f 100644
--- a/teensy/teensy_mem.h
+++ b/teensy/teensy_mem.h
@@ -5,9 +5,6 @@
#define ty_new(T) (ty_alloc(sizeof(T)))
-// NOTE: Overrides GLFW allocator
-void ty_init_mem(void);
-void ty_deinit_mem(void);
void ty_free_temp_allocs(void);
// Temp allocation. Freed at the end of every frame. Do NOT realloc.
void *ty_talloc(size_t size);
@@ -20,9 +17,4 @@ void *ty_alloc(size_t size);
void *ty_realloc(void *ptr, size_t new_size);
void ty_free(void *ptr);
-// Returns -1 in release builds.
-int ty_alloc_count(void);
-// Returns 0 in release builds.
-size_t ty_alloc_size(void);
-
#endif // TEENSY_MEM_H_