Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/rp2_common/pico_malloc/include/pico/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,15 @@
#define PICO_DEBUG_MALLOC_LOW_WATER 0
#endif

#endif
// PICO_CONFIG: PICO_MALLOC_TRACK_PEAK, Enable/disable tracking the peak allocated bytes by malloc, type=bool, default=0, group=pico_malloc
#ifndef PICO_MALLOC_TRACK_PEAK
#define PICO_MALLOC_TRACK_PEAK 0
#endif

#if PICO_MALLOC_TRACK_PEAK
#include <stdlib.h>
extern size_t malloc_peak_bytes;
void malloc_reset_peak();
#endif

#endif
20 changes: 20 additions & 0 deletions src/rp2_common/pico_malloc/pico_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,29 @@ static inline void check_alloc(__unused void *mem, __unused uint size) {
#endif
}

#if PICO_MALLOC_TRACK_PEAK
#include <malloc.h>
size_t malloc_peak_bytes = 0;
void malloc_reset_peak() {
malloc_peak_bytes = mallinfo().uordblks;
}
#endif

static inline void update_peak() {
#if PICO_MALLOC_TRACK_PEAK
struct mallinfo mi = mallinfo();
if (mi.uordblks > malloc_peak_bytes) {
malloc_peak_bytes = mi.uordblks;
}
#endif
}

void *WRAPPER_FUNC(malloc)(size_t size) {
#if PICO_USE_MALLOC_MUTEX
mutex_enter_blocking(&malloc_mutex);
#endif
void *rc = REAL_FUNC(malloc)(size);
update_peak();
#if PICO_USE_MALLOC_MUTEX
mutex_exit(&malloc_mutex);
#endif
Expand All @@ -50,6 +68,7 @@ void *WRAPPER_FUNC(calloc)(size_t count, size_t size) {
mutex_enter_blocking(&malloc_mutex);
#endif
void *rc = REAL_FUNC(calloc)(count, size);
update_peak();
#if PICO_USE_MALLOC_MUTEX
mutex_exit(&malloc_mutex);
#endif
Expand All @@ -67,6 +86,7 @@ void *WRAPPER_FUNC(realloc)(void *mem, size_t size) {
mutex_enter_blocking(&malloc_mutex);
#endif
void *rc = REAL_FUNC(realloc)(mem, size);
update_peak();
#if PICO_USE_MALLOC_MUTEX
mutex_exit(&malloc_mutex);
#endif
Expand Down