This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
commit 516c553b9744fa38d1eed42c272c0b2c87f0070d Author: chenwen <chen...@espressif.com> AuthorDate: Mon Feb 8 10:18:23 2021 +0800 esp32/esp32_wifi_adapter.c: Fix the issue of WiFi internal malloc from PSRAM --- arch/xtensa/include/arch.h | 98 +++++++++++++++++++++++++++++- arch/xtensa/src/esp32/esp32_imm.c | 40 ++++++++++++ arch/xtensa/src/esp32/esp32_wifi_adapter.c | 62 +++++++++++++++++-- 3 files changed, 195 insertions(+), 5 deletions(-) diff --git a/arch/xtensa/include/arch.h b/arch/xtensa/include/arch.h index b3a66c3..b070978 100644 --- a/arch/xtensa/include/arch.h +++ b/arch/xtensa/include/arch.h @@ -94,11 +94,107 @@ extern "C" #ifdef CONFIG_XTENSA_USE_SEPARATE_IMEM struct mallinfo; /* Forward reference, see malloc.h */ -void xtensa_imm_initialize(void); +/**************************************************************************** + * Name: xtensa_imm_initialize + * + * Description: + * Initialize the internal heap. + * + ****************************************************************************/ + +void xtensa_imm_initialize(void); + +/**************************************************************************** + * Name: xtensa_imm_malloc + * + * Description: + * Allocate memory from the internal heap. + * + ****************************************************************************/ + FAR void *xtensa_imm_malloc(size_t size); + +/**************************************************************************** + * Name: xtensa_imm_calloc + * + * Description: + * Calculates the size of the allocation and + * allocate memory the internal heap. + * + ****************************************************************************/ + +FAR void *xtensa_imm_calloc(size_t n, size_t elem_size); + +/**************************************************************************** + * Name: xtensa_imm_realloc + * + * Description: + * Reallocate memory from the internal heap. + * + ****************************************************************************/ + +FAR void *xtensa_imm_realloc(void *ptr, size_t size); + +/**************************************************************************** + * Name: xtensa_imm_zalloc + * + * Description: + * Allocate and zero memory from the internal heap. + * + ****************************************************************************/ + +FAR void *xtensa_imm_zalloc(size_t size); + +/**************************************************************************** + * Name: xtensa_imm_free + * + * Description: + * Free memory from the internal heap. + * + ****************************************************************************/ + void xtensa_imm_free(FAR void *mem); + +/**************************************************************************** + * Name: xtensa_imm_memalign + * + * Description: + * memalign requests more than enough space from malloc, finds a region + * within that chunk that meets the alignment request and then frees any + * leading or trailing space. + * + * The alignment argument must be a power of two (not checked). 8-byte + * alignment is guaranteed by normal malloc calls. + * + ****************************************************************************/ + FAR void *xtensa_imm_memalign(size_t alignment, size_t size); + +/**************************************************************************** + * Name: xtensa_imm_heapmember + * + * Description: + * Check if an address lies in the internal heap. + * + * Parameters: + * mem - The address to check + * + * Return Value: + * true if the address is a member of the internal heap. false if not + * + ****************************************************************************/ + bool xtensa_imm_heapmember(FAR void *mem); + +/**************************************************************************** + * Name: xtensa_imm_mallinfo + * + * Description: + * mallinfo returns a copy of updated current heap information for the + * user heap. + * + ****************************************************************************/ + int xtensa_imm_mallinfo(FAR struct mallinfo *info); #endif diff --git a/arch/xtensa/src/esp32/esp32_imm.c b/arch/xtensa/src/esp32/esp32_imm.c index 03617a2..7bfb703 100644 --- a/arch/xtensa/src/esp32/esp32_imm.c +++ b/arch/xtensa/src/esp32/esp32_imm.c @@ -84,6 +84,46 @@ FAR void *xtensa_imm_malloc(size_t size) } /**************************************************************************** + * Name: xtensa_imm_calloc + * + * Description: + * Calculates the size of the allocation and + * allocate memory the internal heap. + * + ****************************************************************************/ + +FAR void *xtensa_imm_calloc(size_t n, size_t elem_size) +{ + return mm_calloc(&g_iheap, n, elem_size); +} + +/**************************************************************************** + * Name: xtensa_imm_realloc + * + * Description: + * Reallocate memory from the internal heap. + * + ****************************************************************************/ + +FAR void *xtensa_imm_realloc(void *ptr, size_t size) +{ + return mm_realloc(&g_iheap, ptr, size); +} + +/**************************************************************************** + * Name: xtensa_imm_zalloc + * + * Description: + * Allocate and zero memory from the internal heap. + * + ****************************************************************************/ + +FAR void *xtensa_imm_zalloc(size_t size) +{ + return mm_zalloc(&g_iheap, size); +} + +/**************************************************************************** * Name: xtensa_imm_free * * Description: diff --git a/arch/xtensa/src/esp32/esp32_wifi_adapter.c b/arch/xtensa/src/esp32/esp32_wifi_adapter.c index a8da8b7..b0d3a30 100644 --- a/arch/xtensa/src/esp32/esp32_wifi_adapter.c +++ b/arch/xtensa/src/esp32/esp32_wifi_adapter.c @@ -45,11 +45,13 @@ #include <nuttx/wqueue.h> #include <nuttx/sched.h> #include <nuttx/signal.h> +#include <nuttx/arch.h> #include "xtensa.h" #include "xtensa_attr.h" #include "hardware/esp32_dport.h" #include "hardware/esp32_emac.h" +#include "hardware/esp32_soc.h" #include "esp32_cpuint.h" #include "esp32_wifi_adapter.h" #include "esp32_rt_timer.h" @@ -1860,7 +1862,16 @@ static void *esp_malloc(uint32_t size) static void esp_free(void *ptr) { - kmm_free(ptr); +#ifdef CONFIG_XTENSA_USE_SEPARATE_IMEM + if (xtensa_imm_heapmember(ptr)) + { + xtensa_imm_free(ptr); + } + else +#endif + { + kmm_free(ptr); + } } /**************************************************************************** @@ -3427,7 +3438,18 @@ uint32_t esp_log_timestamp(void) static void *esp_malloc_internal(size_t size) { - return kmm_malloc(size); +#ifdef CONFIG_XTENSA_USE_SEPARATE_IMEM + return xtensa_imm_malloc(size); +#else + void *ptr = kmm_malloc(size); + if (esp32_ptr_extram(ptr)) + { + kmm_free(ptr); + return NULL; + } + + return ptr; +#endif } /**************************************************************************** @@ -3447,7 +3469,17 @@ static void *esp_malloc_internal(size_t size) static void *esp_realloc_internal(void *ptr, size_t size) { +#ifdef CONFIG_XTENSA_USE_SEPARATE_IMEM + return xtensa_imm_realloc(ptr, size); +#else + if (size == 0 || esp32_ptr_extram(ptr)) + { + esp_free(ptr); + return NULL; + } + return kmm_realloc(ptr, size); +#endif } /**************************************************************************** @@ -3467,7 +3499,18 @@ static void *esp_realloc_internal(void *ptr, size_t size) static void *esp_calloc_internal(size_t n, size_t size) { - return kmm_calloc(n, size); +#ifdef CONFIG_XTENSA_USE_SEPARATE_IMEM + return xtensa_imm_calloc(n, size); +#else + void *ptr = kmm_calloc(n, size); + if (esp32_ptr_extram(ptr)) + { + kmm_free(ptr); + return NULL; + } + + return ptr; +#endif } /**************************************************************************** @@ -3486,7 +3529,18 @@ static void *esp_calloc_internal(size_t n, size_t size) static void *esp_zalloc_internal(size_t size) { - return kmm_zalloc(size); +#ifdef CONFIG_XTENSA_USE_SEPARATE_IMEM + return xtensa_imm_zalloc(size); +#else + void *ptr = kmm_zalloc(size); + if (esp32_ptr_extram(ptr)) + { + kmm_free(ptr); + return NULL; + } + + return ptr; +#endif } /****************************************************************************