esp32/esp32_wifi_adapter.c: Fix the issue of WiFi internal malloc from PSRAM

This commit is contained in:
chenwen 2021-02-08 10:18:23 +08:00 committed by Xiang Xiao
parent a3f0923ad0
commit 516c553b97
3 changed files with 195 additions and 5 deletions

View File

@ -94,11 +94,107 @@ extern "C"
#ifdef CONFIG_XTENSA_USE_SEPARATE_IMEM
struct mallinfo; /* Forward reference, see malloc.h */
/****************************************************************************
* 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

View File

@ -83,6 +83,46 @@ FAR void *xtensa_imm_malloc(size_t size)
return mm_malloc(&g_iheap, 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
*

View File

@ -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"
@ -1859,9 +1861,18 @@ static void *esp_malloc(uint32_t size)
****************************************************************************/
static void esp_free(void *ptr)
{
#ifdef CONFIG_XTENSA_USE_SEPARATE_IMEM
if (xtensa_imm_heapmember(ptr))
{
xtensa_imm_free(ptr);
}
else
#endif
{
kmm_free(ptr);
}
}
/****************************************************************************
* Name: esp_event_id_map
@ -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
}
/****************************************************************************