Skip to content

Commit 45f1c3b

Browse files
committed
Added missing DRAM fallback to pvPortCallocIram, pvPortZallocIram, and
pvPortMalloc(,,,true) case. Improved comments.
1 parent 3dd08be commit 45f1c3b

File tree

1 file changed

+58
-17
lines changed

1 file changed

+58
-17
lines changed

cores/esp8266/heap.cpp

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -678,53 +678,94 @@ void* _heap_abi_malloc(size_t size, bool unhandled, const void* caller)
678678
679679
The NON-OS SDK 3.0.x has breaking changes to pvPortMalloc. They added one more
680680
argument for selecting a heap. To avoid breaking the build, I renamed their
681-
broken version pvEsprMalloc. To be used, the LIBS need to be edited.
681+
breaking version to sdk3_pvPortMalloc. To complete the fix, the LIBS need to
682+
be edited.
682683
683-
They also added pvPortZallocIram and pvPortCallocIram, which are not a
684-
problem.
684+
Also in the release are low-level functions pvPortZallocIram and
685+
pvPortCallocIram, which are not documented in the Espressif NONOS SDK manual.
686+
No issues in providing replacements. For the non-Arduino ESP8266 applications,
687+
pvPortZallocIram and pvPortCallocIram would have been selected through the
688+
macros like os_malloc defined in `mem.h`.
689+
690+
OOM - Implementation strategy - Native v3.0 SDK
691+
* For functions `pvPortMalloc(,,,true);` and `pvPortMallocIram(,,,);` on a
692+
failed IRAM alloc, try DRAM.
693+
* For function `pvPortMalloc(,,,false);` use DRAM only - on fail, do not
694+
try IRAM.
685695
686696
WPA2 Enterprise connect crashing is fixed at v3.0.2 and up.
687697
688698
Not used for unreleased version NONOSDK3V0.
689699
*/
700+
#ifdef UMM_HEAP_IRAM
690701
void* IRAM_ATTR sdk3_pvPortMalloc(size_t size, const char* file, int line, bool iram)
691702
{
703+
void* caller = __builtin_return_address(0);
692704
if (iram) {
693705
HeapSelectIram ephemeral;
694-
return _heap_pvPortMalloc(size, file, line, __builtin_return_address(0));
695-
} else {
706+
void* ret = _heap_pvPortMalloc(size, file, line, caller);
707+
if (ret) return ret;
708+
}
709+
{
696710
HeapSelectDram ephemeral;
697-
return _heap_pvPortMalloc(size, file, line, __builtin_return_address(0));
711+
return _heap_pvPortMalloc(size, file, line, caller);
698712
}
699713
}
700714

701715
void* IRAM_ATTR pvPortCallocIram(size_t count, size_t size, const char* file, int line)
702716
{
703-
HeapSelectIram ephemeral;
704-
return _heap_pvPortCalloc(count, size, file, line, __builtin_return_address(0));
717+
void* caller = __builtin_return_address(0);
718+
{
719+
HeapSelectIram ephemeral;
720+
void* ret = _heap_pvPortCalloc(count, size, file, line, caller);
721+
if (ret) return ret;
722+
}
723+
{
724+
HeapSelectDram ephemeral;
725+
return _heap_pvPortCalloc(count, size, file, line, caller);
726+
}
705727
}
706728

707729
void* IRAM_ATTR pvPortZallocIram(size_t size, const char* file, int line)
708730
{
709-
HeapSelectIram ephemeral;
710-
return _heap_pvPortCalloc(1, size, file, line, __builtin_return_address(0));
731+
void* caller = __builtin_return_address(0);
732+
{
733+
HeapSelectIram ephemeral;
734+
void* ret = _heap_pvPortCalloc(1, size, file, line, caller);
735+
if (ret) return ret;
736+
}
737+
{
738+
HeapSelectDram ephemeral;
739+
return _heap_pvPortCalloc(1, size, file, line, caller);
740+
}
711741
}
742+
#define CONFIG_IRAM_MEMORY 1
712743

713-
/*
714-
uint32_t IRAM_ATTR user_iram_memory_is_enabled(void)
715-
{
716-
return CONFIG_ENABLE_IRAM_MEMORY;
717-
}
744+
#else
745+
// For sdk3_pvPortMalloc, the bool argument is ignored and intentionally omitted.
746+
extern "C" void* sdk3_pvPortMalloc(size_t size, const char* file, int line) __attribute__ ((alloc_size(1), malloc, alias("pvPortMalloc")));
747+
extern "C" void* pvPortCallocIram(size_t count, size_t size, const char* file, int line) __attribute__((alloc_size(1, 2), malloc, alias("pvPortCalloc")));
748+
extern "C" void* pvPortZallocIram(size_t size, const char* file, int line) __attribute__((alloc_size(1), malloc, alias("pvPortZalloc")));
749+
#define CONFIG_IRAM_MEMORY 0
750+
#endif // #ifdef UMM_HEAP_IRAM
718751

752+
/*
719753
We do not need the function user_iram_memory_is_enabled().
720754
1. It was used by mem_manager.o which was replaced with this custom heap
721-
implementation. IRAM memory selection is handled differently.
755+
implementation. IRAM memory selection is handled differently for
756+
Arduino ESP8266.
722757
2. In libmain.a, Cache_Read_Enable_New uses it for cache size. However, When
723758
using IRAM for memory or running with 48K IRAM for code, we use a
724759
replacement Cache_Read_Enable to correct the cache size ignoring
725760
Cache_Read_Enable_New's selected value.
761+
3. Create a linker conflicts in the event the sketch author tries to control
762+
IRAM heap through this method.
726763
*/
727-
#endif
764+
uint32 IRAM_ATTR user_iram_memory_is_enabled(void)
765+
{
766+
return CONFIG_IRAM_MEMORY;
767+
}
768+
#endif // #if (NONOSDK >= (0x30000))
728769
};
729770

730771
#if defined(ENABLE_THICK_DEBUG_WRAPPERS)

0 commit comments

Comments
 (0)