diff --git a/cpu/msp430_common/include/cpu_conf.h b/cpu/msp430_common/include/cpu_conf.h index 09e0aa9ba4..d4fbbaeea5 100644 --- a/cpu/msp430_common/include/cpu_conf.h +++ b/cpu/msp430_common/include/cpu_conf.h @@ -77,6 +77,11 @@ extern "C" { #endif /** @} */ +/** + * @brief Declare the heap_stats function as available + */ +#define HAVE_HEAP_STATS + #ifdef __cplusplus } #endif diff --git a/cpu/msp430_common/malloc.c b/cpu/msp430_common/malloc.c index 354d94ec02..bae6ce9db4 100644 --- a/cpu/msp430_common/malloc.c +++ b/cpu/msp430_common/malloc.c @@ -224,6 +224,10 @@ free(void *p) state = irq_disable(); + if (__brkval == NULL) { + __brkval = __malloc_heap_start; + } + /* ISO C says free(NULL) must be a no-op */ if (p == NULL) { irq_restore(state); @@ -457,4 +461,26 @@ calloc(size_t nele, size_t size) return p; } +void heap_stats(void) +{ + if (__brkval == NULL) { + __brkval = __malloc_heap_start; + } + + long int heap_size = __malloc_heap_end - __malloc_heap_start; + long int free = __malloc_heap_end - __brkval; + struct __freelist *fp; + for (fp = __flp; fp; fp = fp->nx) { + free += fp->sz; + } + printf("heap: %ld (used %ld, free %ld) [bytes]\n", + heap_size, heap_size - free, free); +} + +#else + +void heap_stats(void) { + puts("heap statistics are not supported"); +} + #endif /* MODULE_MSP430_MALLOC */