55
66#include  < stdlib.h> 
77#include  " umm_malloc/umm_malloc.h" 
8+ 
9+ //  Need FORCE_ALWAYS_INLINE to put HeapSelect class constructor/deconstructor in IRAM
10+ #define  FORCE_ALWAYS_INLINE_HEAP_SELECT 
11+ #include  " umm_malloc/umm_heap_select.h" 
12+ 
813#include  < c_types.h> 
914#include  < sys/reent.h> 
1015#include  < user_interface.h> 
@@ -16,6 +21,7 @@ extern "C" {
1621#define  UMM_CALLOC (n,s )         umm_poison_calloc(n,s)
1722#define  UMM_REALLOC_FL (p,s,f,l ) umm_poison_realloc_fl(p,s,f,l)
1823#define  UMM_FREE_FL (p,f,l )      umm_poison_free_fl(p,f,l)
24+ #define  STATIC_ALWAYS_INLINE 
1925
2026#undef  realloc
2127#undef  free
@@ -25,6 +31,7 @@ extern "C" {
2531#define  UMM_CALLOC (n,s )         umm_calloc(n,s)
2632#define  UMM_REALLOC_FL (p,s,f,l ) umm_realloc(p,s)
2733#define  UMM_FREE_FL (p,f,l )      umm_free(p)
34+ #define  STATIC_ALWAYS_INLINE 
2835
2936#undef  realloc
3037#undef  free
@@ -34,6 +41,10 @@ extern "C" {
3441#define  UMM_CALLOC (n,s )         calloc(n,s)
3542#define  UMM_REALLOC_FL (p,s,f,l ) realloc(p,s)
3643#define  UMM_FREE_FL (p,f,l )      free(p)
44+ 
45+ //  STATIC_ALWAYS_INLINE only applys to the non-debug build path,
46+ //  it must not be enabled on the debug build path.
47+ #define  STATIC_ALWAYS_INLINE  static  ALWAYS_INLINE
3748#endif 
3849
3950
@@ -259,8 +270,8 @@ void ICACHE_RAM_ATTR free(void* p)
259270}
260271#endif 
261272
262- 
263- void * ICACHE_RAM_ATTR pvPortMalloc (size_t  size, const  char * file, int  line)
273+ STATIC_ALWAYS_INLINE 
274+ void * ICACHE_RAM_ATTR heap_pvPortMalloc (size_t  size, const  char * file, int  line)
264275{
265276    INTEGRITY_CHECK__PANIC_FL (file, line);
266277    POISON_CHECK__PANIC_FL (file, line);
@@ -270,7 +281,8 @@ void* ICACHE_RAM_ATTR pvPortMalloc(size_t size, const char* file, int line)
270281    return  ret;
271282}
272283
273- void * ICACHE_RAM_ATTR pvPortCalloc (size_t  count, size_t  size, const  char * file, int  line)
284+ STATIC_ALWAYS_INLINE
285+ void * ICACHE_RAM_ATTR heap_pvPortCalloc (size_t  count, size_t  size, const  char * file, int  line)
274286{
275287    INTEGRITY_CHECK__PANIC_FL (file, line);
276288    POISON_CHECK__PANIC_FL (file, line);
@@ -280,7 +292,8 @@ void* ICACHE_RAM_ATTR pvPortCalloc(size_t count, size_t size, const char* file,
280292    return  ret;
281293}
282294
283- void * ICACHE_RAM_ATTR pvPortRealloc (void  *ptr, size_t  size, const  char * file, int  line)
295+ STATIC_ALWAYS_INLINE
296+ void * ICACHE_RAM_ATTR heap_pvPortRealloc (void  *ptr, size_t  size, const  char * file, int  line)
284297{
285298    INTEGRITY_CHECK__PANIC_FL (file, line);
286299    void * ret = UMM_REALLOC_FL (ptr, size, file, line);
@@ -290,7 +303,8 @@ void* ICACHE_RAM_ATTR pvPortRealloc(void *ptr, size_t size, const char* file, in
290303    return  ret;
291304}
292305
293- void * ICACHE_RAM_ATTR pvPortZalloc (size_t  size, const  char * file, int  line)
306+ STATIC_ALWAYS_INLINE
307+ void * ICACHE_RAM_ATTR heap_pvPortZalloc (size_t  size, const  char * file, int  line)
294308{
295309    INTEGRITY_CHECK__PANIC_FL (file, line);
296310    POISON_CHECK__PANIC_FL (file, line);
@@ -300,7 +314,8 @@ void* ICACHE_RAM_ATTR pvPortZalloc(size_t size, const char* file, int line)
300314    return  ret;
301315}
302316
303- void  ICACHE_RAM_ATTR vPortFree (void  *ptr, const  char * file, int  line)
317+ STATIC_ALWAYS_INLINE
318+ void  ICACHE_RAM_ATTR heap_vPortFree (void  *ptr, const  char * file, int  line)
304319{
305320    INTEGRITY_CHECK__PANIC_FL (file, line);
306321    UMM_FREE_FL (ptr, file, line);
@@ -314,7 +329,47 @@ size_t ICACHE_RAM_ATTR xPortWantedSizeAlign(size_t size)
314329
315330void  system_show_malloc (void )
316331{
332+     HeapSelectDram ephemeral;
317333    umm_info (NULL , true );
318334}
319335
336+ /* 
337+   NONOS SDK and lwIP do not handle IRAM heap well. Since they also use portable 
338+   malloc calls pvPortMalloc, ... we can leverage that for this solution. 
339+   Force pvPortMalloc, ... APIs to serve DRAM only. 
340+ */ 
341+ void * ICACHE_RAM_ATTR pvPortMalloc (size_t  size, const  char * file, int  line)
342+ {
343+     HeapSelectDram ephemeral;
344+     return  heap_pvPortMalloc (size,  file, line);;
345+ }
346+ 
347+ void * ICACHE_RAM_ATTR pvPortCalloc (size_t  count, size_t  size, const  char * file, int  line)
348+ {
349+     HeapSelectDram ephemeral;
350+     return  heap_pvPortCalloc (count, size,  file, line);
351+ }
352+ 
353+ void * ICACHE_RAM_ATTR pvPortRealloc (void  *ptr, size_t  size, const  char * file, int  line)
354+ {
355+     HeapSelectDram ephemeral;
356+     return  heap_pvPortRealloc (ptr, size,  file, line);
357+ }
358+ 
359+ void * ICACHE_RAM_ATTR pvPortZalloc (size_t  size, const  char * file, int  line)
360+ {
361+     HeapSelectDram ephemeral;
362+     return  heap_pvPortZalloc (size,  file, line);
363+ }
364+ 
365+ void  ICACHE_RAM_ATTR vPortFree (void  *ptr, const  char * file, int  line)
366+ {
367+ #if  defined(DEBUG_ESP_OOM) || defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE) || defined(UMM_INTEGRITY_CHECK)
368+     //  This is only needed for debug checks to ensure they are performed in
369+     //  correct context. umm_malloc free internally determines the correct heap.
370+     HeapSelectDram ephemeral;
371+ #endif 
372+     return  heap_vPortFree (ptr,  file, line);
373+ }
374+ 
320375};
0 commit comments