Skip to content

Commit 33a6f3f

Browse files
committed
Log lr from bmalloc to assist in memory tracking.
Signed-off-by: Katharine Berry <[email protected]>
1 parent 9fe8413 commit 33a6f3f

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

app/src/c/util/memory/malloc.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@
2121
#include <pebble.h>
2222

2323
void *bmalloc(size_t size) {
24+
register uintptr_t lr __asm("lr");
25+
const uintptr_t saved_lr = lr;
2426
int heap_size = heap_bytes_free();
2527
BOBBY_LOG(APP_LOG_LEVEL_DEBUG, "malloc request: %d; free: %d", size, heap_size);
2628
while (true) {
2729
heap_size = heap_bytes_free();
2830
if (heap_bytes_free() > 750) {
2931
void *ptr = malloc(size);
3032
if (ptr) {
33+
BOBBY_LOG(APP_LOG_LEVEL_DEBUG_VERBOSE, "malloc returned %p for caller %p", ptr, saved_lr);
3134
return ptr;
3235
}
3336
BOBBY_LOG(APP_LOG_LEVEL_WARNING, "Out of memory! Need to allocate %d bytes; %d bytes free.", size, heap_size);
@@ -36,7 +39,11 @@ void *bmalloc(size_t size) {
3639
}
3740
if (!memory_pressure_try_free()) {
3841
BOBBY_LOG(APP_LOG_LEVEL_ERROR, "Failed to allocate memory: couldn't free enough heap.");
39-
return malloc(size);
42+
void *tried = malloc(size);
43+
if (tried) {
44+
BOBBY_LOG(APP_LOG_LEVEL_DEBUG, "malloc returned %p for caller %p", tried, saved_lr);
45+
}
46+
return tried;
4047
}
4148
int new_heap_size = heap_bytes_free();
4249
BOBBY_LOG(APP_LOG_LEVEL_INFO, "Freed %d bytes, heap size is now %d. Retrying allocation of %d bytes.", heap_size - new_heap_size, new_heap_size, size);

0 commit comments

Comments
 (0)