29 lines
781 B
C
29 lines
781 B
C
|
#include "dbg_malloc.h"
|
||
|
|
||
|
size_t dbg_malloc_mem_usage = 0;
|
||
|
size_t dbg_malloc_peak_usage = 0;
|
||
|
|
||
|
#include <malloc.h>
|
||
|
void* dbg_malloc(size_t size) {
|
||
|
void* ptr = malloc(size);
|
||
|
dbg_malloc_mem_usage += malloc_usable_size(ptr);
|
||
|
if (dbg_malloc_mem_usage > dbg_malloc_peak_usage)
|
||
|
dbg_malloc_peak_usage = dbg_malloc_mem_usage;
|
||
|
#ifdef DBG_MALLOC_PRINT
|
||
|
fprintf(stderr, " malloc(%3li) -> %4liB used\n", size, dbg_malloc_mem_usage);
|
||
|
#endif
|
||
|
return ptr;
|
||
|
}
|
||
|
void dbg_free(void* ptr) {
|
||
|
dbg_malloc_mem_usage -= malloc_usable_size(ptr);
|
||
|
free(ptr);
|
||
|
#ifdef DBG_MALLOC_PRINT
|
||
|
fprintf(stderr, " free() -> %4liB used\n", dbg_malloc_mem_usage);
|
||
|
#endif
|
||
|
}
|
||
|
size_t dbg_reset_peak() {
|
||
|
size_t old_peak = dbg_malloc_peak_usage;
|
||
|
dbg_malloc_peak_usage = dbg_malloc_mem_usage;
|
||
|
return old_peak;
|
||
|
}
|