diff --git a/components/esp8266/include/esp_libc.h b/components/esp8266/include/esp_libc.h index bd979c90..525bbb67 100644 --- a/components/esp8266/include/esp_libc.h +++ b/components/esp8266/include/esp_libc.h @@ -118,7 +118,7 @@ bool ICACHE_FLASH_ATTR check_memleak_debug_enable(void) #define os_free(s) \ do{\ static const char mem_debug_file[] ICACHE_RODATA_ATTR STORE_ATTR = __FILE__; \ - vPortFree(s, mem_debug_file, __LINE__);\ + vPortFree_trace(s, mem_debug_file, __LINE__);\ }while(0) #endif @@ -126,7 +126,7 @@ do{\ #define os_malloc(s) \ ({ \ static const char mem_debug_file[] ICACHE_RODATA_ATTR STORE_ATTR = __FILE__; \ - pvPortMalloc(s, mem_debug_file, __LINE__, false); \ + pvPortMalloc_trace(s, mem_debug_file, __LINE__, false); \ }) #endif @@ -134,7 +134,7 @@ do{\ #define os_malloc_iram(s) \ ({ \ static const char mem_debug_file[] ICACHE_RODATA_ATTR STORE_ATTR = __FILE__; \ - pvPortMalloc(s, mem_debug_file, __LINE__, true); \ + pvPortMalloc_trace(s, mem_debug_file, __LINE__, true); \ }) #endif @@ -142,7 +142,7 @@ do{\ #define os_calloc(p, s) \ ({ \ static const char mem_debug_file[] ICACHE_RODATA_ATTR STORE_ATTR = __FILE__; \ - pvPortCalloc(p, s, mem_debug_file, __LINE__); \ + pvPortCalloc_trace(p, s, mem_debug_file, __LINE__); \ }) #endif @@ -150,7 +150,7 @@ do{\ #define os_realloc(p, s) \ ({ \ static const char mem_debug_file[] ICACHE_RODATA_ATTR STORE_ATTR = __FILE__; \ - pvPortRealloc(p, s, mem_debug_file, __LINE__); \ + pvPortRealloc_trace(p, s, mem_debug_file, __LINE__); \ }) #endif @@ -158,7 +158,7 @@ do{\ #define os_zalloc(s) \ ({ \ static const char mem_debug_file[] ICACHE_RODATA_ATTR STORE_ATTR = __FILE__; \ - pvPortZalloc(s, mem_debug_file, __LINE__); \ + pvPortZalloc_trace(s, mem_debug_file, __LINE__); \ }) #endif diff --git a/components/freertos/include/freertos/private/portable.h b/components/freertos/include/freertos/private/portable.h index 4446a492..61666f01 100644 --- a/components/freertos/include/freertos/private/portable.h +++ b/components/freertos/include/freertos/private/portable.h @@ -126,8 +126,18 @@ void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions ) PRIVILEG /* * Map to the memory management routines required for the port. */ -//void *pvPortMalloc( size_t xSize ) PRIVILEGED_FUNCTION; -//void vPortFree( void *pv ) PRIVILEGED_FUNCTION; +void *pvPortMalloc( size_t xSize ) PRIVILEGED_FUNCTION; +void *pvPortZalloc( size_t xWantedSize ) PRIVILEGED_FUNCTION; +void *pvPortCalloc( size_t count, size_t size ) PRIVILEGED_FUNCTION; +void vPortFree( void *pv ) PRIVILEGED_FUNCTION; + +#ifdef MEMLEAK_DEBUG +void *pvPortMalloc_trace( size_t xWantedSize, const char * file, unsigned line, bool use_iram ) PRIVILEGED_FUNCTION; +void *pvPortZalloc_trace( size_t xWantedSize, const char * file, unsigned line ) PRIVILEGED_FUNCTION; +void *pvPortCalloc_trace( size_t count, size_t size, const char * file, unsigned line ) PRIVILEGED_FUNCTION; +void vPortFree_trace( void *pv, const char * file, unsigned line ) PRIVILEGED_FUNCTION; +#endif + void vPortInitialiseBlocks( void ) PRIVILEGED_FUNCTION; size_t xPortGetFreeHeapSize( void ) PRIVILEGED_FUNCTION; size_t xPortGetMinimumEverFreeHeapSize( void ) PRIVILEGED_FUNCTION; diff --git a/components/freertos/port/esp8266/heap_5.c b/components/freertos/port/esp8266/heap_5.c index 3eeaf298..b1d12130 100644 --- a/components/freertos/port/esp8266/heap_5.c +++ b/components/freertos/port/esp8266/heap_5.c @@ -313,10 +313,13 @@ size_t xPortWantedSizeAlign(size_t xWantedSize) return xWantedSize; } -#ifndef MEMLEAK_DEBUG void *pvPortMalloc( size_t xWantedSize ) -#else -void *pvPortMalloc( size_t xWantedSize, const char * file, unsigned line, bool use_iram) +#ifdef MEMLEAK_DEBUG +{ + return pvPortMalloc_trace( xWantedSize, NULL, 0, false ); +} + +void *pvPortMalloc_trace( size_t xWantedSize, const char * file, unsigned line, bool use_iram ) #endif { BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink; @@ -492,12 +495,16 @@ static bool is_inited = false; return pvReturn; } + /*-----------------------------------------------------------*/ -#ifndef MEMLEAK_DEBUG void vPortFree( void *pv ) -#else -void vPortFree(void *pv, const char * file, unsigned line) +#ifdef MEMLEAK_DEBUG +{ + return vPortFree_trace( pv, NULL, 0 ); +} + +void vPortFree_trace( void *pv, const char * file, unsigned line ) #endif { uint8_t *puc = ( uint8_t * ) pv; @@ -556,6 +563,7 @@ BlockLink_t *pxLink; } } } + /*-----------------------------------------------------------*/ #ifndef MEMLEAK_DEBUG @@ -615,12 +623,12 @@ void *realloc(void *ptr, size_t nbytes) __attribute__((alias("pvPortRealloc"))); #else /*-----------------------------------------------------------*/ -void *pvPortCalloc(size_t count, size_t size, const char * file, unsigned line) +void *pvPortCalloc_trace(size_t count, size_t size, const char * file, unsigned line) { void *p; //ets_printf("1,"); /* allocate 'count' objects of size 'size' */ - p = pvPortMalloc(count * size, file, line, false); + p = pvPortMalloc_trace(count * size, file, line, false); //ets_printf("2,"); if (p) { /* zero the memory */ @@ -629,59 +637,70 @@ void *pvPortCalloc(size_t count, size_t size, const char * file, unsigned line) //ets_printf("3,"); return p; } -/*-----------------------------------------------------------*/ - -void *pvPortZalloc(size_t size, const char * file, unsigned line) -{ - return pvPortCalloc(1, size, file, line); +void *pvPortCalloc(size_t count, size_t size) { + return pvPortCalloc_trace(count, size, NULL, 0); } /*-----------------------------------------------------------*/ -void *pvPortRealloc(void *mem, size_t newsize, const char *file, unsigned line) +void *pvPortZalloc_trace(size_t size, const char * file, unsigned line) +{ + return pvPortCalloc_trace(1, size, file, line); +} +void *pvPortZalloc(size_t size) { + return pvPortZalloc_trace( size, NULL, 0 ); +} + +/*-----------------------------------------------------------*/ + +void *pvPortRealloc_trace(void *mem, size_t newsize, const char *file, unsigned line) { if (newsize == 0) { - vPortFree(mem, file, line); + vPortFree_trace(mem, file, line); return NULL; } void *p; - p = pvPortMalloc(newsize, file, line, false); + p = pvPortMalloc_trace(newsize, file, line, false); if (p) { /* zero the memory */ if (mem != NULL) { memcpy(p, mem, newsize); - vPortFree(mem, file, line); + vPortFree_trace(mem, file, line); } } return p; } + +void *pvPortRealloc(void *mem, size_t newsize) { + return pvPortRealloc_trace(mem, newsize, NULL, 0 ); +} /*-----------------------------------------------------------*/ //For user void *malloc(size_t nbytes) { //ets_printf("u_m\n"); - return pvPortMalloc( nbytes, mem_debug_file, 0, false); + return pvPortMalloc_trace( nbytes, mem_debug_file, 0, false); } void free(void *ptr) { //ets_printf("u_f\n"); - vPortFree(ptr, mem_debug_file, 0); + vPortFree_trace(ptr, mem_debug_file, 0); } void *zalloc(size_t nbytes) { - return pvPortZalloc(nbytes, mem_debug_file, 0); + return pvPortZalloc_trace(nbytes, mem_debug_file, 0); } void *calloc(size_t count, size_t nbytes) { - return pvPortCalloc(count, nbytes, mem_debug_file, 0); + return pvPortCalloc_trace(count, nbytes, mem_debug_file, 0); } void *realloc(void *ptr, size_t nbytes) { - return pvPortRealloc(ptr, nbytes, mem_debug_file, 0); + return pvPortRealloc_trace(ptr, nbytes, mem_debug_file, 0); } /* diff --git a/components/lwip/port/esp8266/include/lwipopts.h b/components/lwip/port/esp8266/include/lwipopts.h index 62b12bcc..3a2af944 100644 --- a/components/lwip/port/esp8266/include/lwipopts.h +++ b/components/lwip/port/esp8266/include/lwipopts.h @@ -194,10 +194,6 @@ /** * Use DRAM instead of IRAM */ -extern void *pvPortMalloc( size_t xWantedSize, const char * file, unsigned line, bool use_iram); -extern void *pvPortZalloc( size_t xWantedSize, const char * file, unsigned line); -extern void *pvPortCalloc(size_t count, size_t size, const char * file, unsigned line); -extern void vPortFree(void *pv, const char * file, unsigned line); #define mem_clib_free os_free #define mem_clib_malloc os_malloc #define mem_clib_calloc os_calloc diff --git a/components/newlib/newlib/port/syscall.c b/components/newlib/newlib/port/syscall.c index c08989d1..75423cdb 100644 --- a/components/newlib/newlib/port/syscall.c +++ b/components/newlib/newlib/port/syscall.c @@ -21,6 +21,7 @@ #include "esp8266/ets_sys.h" #include "esp8266/eagle_soc.h" #include "esp8266/uart_register.h" +#include "FreeRTOS.h" #define PANIC_UART 0 @@ -91,7 +92,11 @@ void _sbrk_r(void *ptr, int incr) void *_malloc_r(struct _reent *r, size_t n) { +#ifndef MEMLEAK_DEBUG return pvPortMalloc(n); +#else + return pvPortMalloc_trace(n, NULL, 0, false); +#endif } void *_realloc_r(struct _reent *r, void *old_ptr, size_t n)