From 1b236acc86c33bd6dc8fb6739f2eb8af290ae42f Mon Sep 17 00:00:00 2001 From: d0k3 Date: Thu, 1 Feb 2018 02:22:13 +0100 Subject: [PATCH] Setup heap testing --- arm9/source/common/common.h | 6 ++++++ arm9/source/godmode.c | 10 ++++++++++ arm9/source/system/mymalloc.c | 31 +++++++++++++++++++++++++++++++ arm9/source/system/mymalloc.h | 8 ++++++++ 4 files changed, 55 insertions(+) create mode 100644 arm9/source/system/mymalloc.c create mode 100644 arm9/source/system/mymalloc.h diff --git a/arm9/source/common/common.h b/arm9/source/common/common.h index bb8defe..94bc1c3 100644 --- a/arm9/source/common/common.h +++ b/arm9/source/common/common.h @@ -10,6 +10,12 @@ #include #include +#ifdef MONITOR_HEAP +#include "mymalloc.h" +#define malloc my_malloc +#define free my_free +#endif + #define max(a,b) \ (((a) > (b)) ? (a) : (b)) #define min(a,b) \ diff --git a/arm9/source/godmode.c b/arm9/source/godmode.c index 9bd4801..5073d2c 100644 --- a/arm9/source/godmode.c +++ b/arm9/source/godmode.c @@ -175,6 +175,16 @@ void DrawTopBar(const char* curr_path) { } #endif + #ifdef MONITOR_HEAP + if (true) { // allocated mem + const u32 bartxt_rx = SCREEN_WIDTH_TOP - (9*FONT_WIDTH_EXT) - bartxt_x; + char bytestr[32]; + FormatBytes(bytestr, mem_allocated()); + DrawStringF(TOP_SCREEN, bartxt_rx, bartxt_start, COLOR_STD_BG, COLOR_TOP_BAR, "%9.9s", bytestr); + show_time = false; + } + #endif + if (show_time) { // clock & battery const u32 battery_width = 16; const u32 battery_height = 9; diff --git a/arm9/source/system/mymalloc.c b/arm9/source/system/mymalloc.c new file mode 100644 index 0000000..09d3ffe --- /dev/null +++ b/arm9/source/system/mymalloc.c @@ -0,0 +1,31 @@ +#include "mymalloc.h" +#include + +static size_t total_allocated = 0; + +void* my_malloc(size_t size) { + void* ptr = (void*) malloc(sizeof(size_t) + size); + if (ptr) total_allocated += size; + if (ptr) (*(size_t*) ptr) = size; + return ptr ? (((char*) ptr) + sizeof(size_t)) : NULL; +} + +void my_free(void* ptr) { + void* ptr_fix = (char*) ptr - sizeof(size_t); + total_allocated -= *(size_t*) ptr_fix; + free(ptr_fix); +} + +size_t mem_allocated(void) { + return total_allocated; +} + +size_t my_malloc_test(void) { + size_t add = 1024 * 1024; + for (size_t s = add;; s += add) { + void* ptr = (void*) malloc(s); + if (!ptr) return s; + free(ptr); + } + return 0; // unreachable +} diff --git a/arm9/source/system/mymalloc.h b/arm9/source/system/mymalloc.h new file mode 100644 index 0000000..f5266f2 --- /dev/null +++ b/arm9/source/system/mymalloc.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +void* my_malloc(size_t size); +void my_free(void* ptr); +size_t mem_allocated(void); +size_t my_malloc_test(void);