Setup heap testing

This commit is contained in:
d0k3 2018-02-01 02:22:13 +01:00
parent a0d2d27b06
commit 1b236acc86
4 changed files with 55 additions and 0 deletions

View File

@ -10,6 +10,12 @@
#include <types.h> #include <types.h>
#include <stdalign.h> #include <stdalign.h>
#ifdef MONITOR_HEAP
#include "mymalloc.h"
#define malloc my_malloc
#define free my_free
#endif
#define max(a,b) \ #define max(a,b) \
(((a) > (b)) ? (a) : (b)) (((a) > (b)) ? (a) : (b))
#define min(a,b) \ #define min(a,b) \

View File

@ -175,6 +175,16 @@ void DrawTopBar(const char* curr_path) {
} }
#endif #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 if (show_time) { // clock & battery
const u32 battery_width = 16; const u32 battery_width = 16;
const u32 battery_height = 9; const u32 battery_height = 9;

View File

@ -0,0 +1,31 @@
#include "mymalloc.h"
#include <stdlib.h>
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
}

View File

@ -0,0 +1,8 @@
#pragma once
#include <stddef.h>
void* my_malloc(size_t size);
void my_free(void* ptr);
size_t mem_allocated(void);
size_t my_malloc_test(void);