Skip to content

Commit

Permalink
Merge branch 'refactor/freertos_port_malloc_macros' into 'master'
Browse files Browse the repository at this point in the history
FreeRTOS(IDF): Refactor pvPortMalloc()/vPortFree(), Implement IDF heap wrapper for FreeRTOS.

Closes IDF-3997

See merge request espressif/esp-idf!22565
  • Loading branch information
Dazza0 committed Mar 7, 2023
2 parents 56dc272 + 4069a62 commit daf4150
Show file tree
Hide file tree
Showing 17 changed files with 266 additions and 232 deletions.
1 change: 1 addition & 0 deletions components/freertos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(arch "linux")
endif()

set(srcs
"heap_idf.c"
"${kernel_dir}/list.c"
"${kernel_dir}/queue.c"
"${kernel_dir}/tasks.c"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -286,15 +286,32 @@ static inline bool IRAM_ATTR xPortCanYield(void)

void vPortSetStackWatchpoint(void *pxStackStart);

#define portVALID_TCB_MEM(ptr) (esp_ptr_internal(ptr) && esp_ptr_byte_accessible(ptr))
#ifdef CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
#define portVALID_STACK_MEM(ptr) (esp_ptr_byte_accessible(ptr))
#else
#define portVALID_STACK_MEM(ptr) (esp_ptr_internal(ptr) && esp_ptr_byte_accessible(ptr))
#endif
// -------------------- Heap Related -----------------------

/**
* @brief Checks if a given piece of memory can be used to store a task's TCB
*
* - Defined in heap_idf.c
*
* @param ptr Pointer to memory
* @return true Memory can be used to store a TCB
* @return false Otherwise
*/
bool xPortCheckValidTCBMem(const void *ptr);

/**
* @brief Checks if a given piece of memory can be used to store a task's stack
*
* - Defined in heap_idf.c
*
* @param ptr Pointer to memory
* @return true Memory can be used to store a task stack
* @return false Otherwise
*/
bool xPortcheckValidStackMem(const void *ptr);

#define portTcbMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
#define portStackMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
#define portVALID_TCB_MEM(ptr) xPortCheckValidTCBMem(ptr)
#define portVALID_STACK_MEM(ptr) xPortcheckValidStackMem(ptr)

/* ------------------------------------------------------ Misc ---------------------------------------------------------
* - Miscellaneous porting macros
Expand Down
36 changes: 0 additions & 36 deletions components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,42 +349,6 @@ void vPortEndScheduler(void)
abort();
}

// ----------------------- Memory --------------------------

#define FREERTOS_SMP_MALLOC_CAPS (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)

void *pvPortMalloc( size_t xSize )
{
return heap_caps_malloc(xSize, FREERTOS_SMP_MALLOC_CAPS);
}

void vPortFree( void *pv )
{
heap_caps_free(pv);
}

void vPortInitialiseBlocks( void )
{
; //Does nothing, heap is initialized separately in ESP-IDF
}

size_t xPortGetFreeHeapSize( void )
{
return esp_get_free_heap_size();
}

#if( configSTACK_ALLOCATION_FROM_SEPARATE_HEAP == 1 )
void *pvPortMallocStack( size_t xSize )
{
return NULL;
}

void vPortFreeStack( void *pv )
{

}
#endif

// ------------------------ Stack --------------------------

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -352,15 +352,32 @@ static inline bool IRAM_ATTR xPortCanYield(void)

void vPortSetStackWatchpoint(void *pxStackStart);

#define portVALID_TCB_MEM(ptr) (esp_ptr_internal(ptr) && esp_ptr_byte_accessible(ptr))
#ifdef CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
#define portVALID_STACK_MEM(ptr) (esp_ptr_byte_accessible(ptr))
#else
#define portVALID_STACK_MEM(ptr) (esp_ptr_internal(ptr) && esp_ptr_byte_accessible(ptr))
#endif
// -------------------- Heap Related -----------------------

/**
* @brief Checks if a given piece of memory can be used to store a task's TCB
*
* - Defined in heap_idf.c
*
* @param ptr Pointer to memory
* @return true Memory can be used to store a TCB
* @return false Otherwise
*/
bool xPortCheckValidTCBMem(const void *ptr);

/**
* @brief Checks if a given piece of memory can be used to store a task's stack
*
* - Defined in heap_idf.c
*
* @param ptr Pointer to memory
* @return true Memory can be used to store a task stack
* @return false Otherwise
*/
bool xPortcheckValidStackMem(const void *ptr);

#define portTcbMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
#define portStackMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
#define portVALID_TCB_MEM(ptr) xPortCheckValidTCBMem(ptr)
#define portVALID_STACK_MEM(ptr) xPortcheckValidStackMem(ptr)

// --------------- Compatibility Includes ------------------
/*
Expand Down
36 changes: 0 additions & 36 deletions components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,42 +386,6 @@ void vPortEndScheduler( void )
;
}

// ----------------------- Memory --------------------------

#define FREERTOS_SMP_MALLOC_CAPS (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)

void *pvPortMalloc( size_t xSize )
{
return heap_caps_malloc(xSize, FREERTOS_SMP_MALLOC_CAPS);
}

void vPortFree( void *pv )
{
heap_caps_free(pv);
}

void vPortInitialiseBlocks( void )
{
; //Does nothing, heap is initialized separately in ESP-IDF
}

size_t xPortGetFreeHeapSize( void )
{
return esp_get_free_heap_size();
}

#if( configSTACK_ALLOCATION_FROM_SEPARATE_HEAP == 1 )
void *pvPortMallocStack( size_t xSize )
{
return NULL;
}

void vPortFreeStack( void *pv )
{

}
#endif

// ------------------------ Stack --------------------------

// User exception dispatcher when exiting
Expand Down
25 changes: 0 additions & 25 deletions components/freertos/FreeRTOS-Kernel/include/freertos/portable.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@
#endif
#endif /* if ( portUSING_MPU_WRAPPERS == 1 ) */

#ifdef configUSE_FREERTOS_PROVIDED_HEAP

/* Used by heap_5.c to define the start address and size of each memory region
* that together comprise the total FreeRTOS heap space. */
typedef struct HeapRegion
Expand Down Expand Up @@ -189,29 +187,6 @@ void vPortInitialiseBlocks( void ) PRIVILEGED_FUNCTION;
size_t xPortGetFreeHeapSize( void ) PRIVILEGED_FUNCTION;
size_t xPortGetMinimumEverFreeHeapSize( void ) PRIVILEGED_FUNCTION;

#if( configSTACK_ALLOCATION_FROM_SEPARATE_HEAP == 1 )
void *pvPortMallocStack( size_t xSize ) PRIVILEGED_FUNCTION;
void vPortFreeStack( void *pv ) PRIVILEGED_FUNCTION;
#else
#define pvPortMallocStack pvPortMalloc
#define vPortFreeStack vPortFree
#endif
#else // configUSE_FREERTOS_PROVIDED_HEAP

/*
* Map to the memory management routines required for the port.
*
* Note that libc standard malloc/free are also available for
* non-FreeRTOS-specific code, and behave the same as
* pvPortMalloc()/vPortFree().
*/
#define pvPortMalloc malloc
#define vPortFree free
#define xPortGetFreeHeapSize esp_get_free_heap_size
#define xPortGetMinimumEverFreeHeapSize esp_get_minimum_free_heap_size

#endif

/*
* Setup the hardware ready for the scheduler to take control. This generally
* sets up a tick interrupt and sets timers for the correct tick frequency.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,30 @@ static inline BaseType_t IRAM_ATTR xPortGetCoreID(void)
return (BaseType_t) 0;
}

static inline bool portVALID_TCB_MEM(const void *ptr)
{
return true;
}
/**
* @brief Checks if a given piece of memory can be used to store a task's TCB
*
* - Defined in heap_idf.c
*
* @param ptr Pointer to memory
* @return true Memory can be used to store a TCB
* @return false Otherwise
*/
bool xPortCheckValidTCBMem(const void *ptr);

static inline bool portVALID_STACK_MEM(const void *ptr)
{
return true;
}
/**
* @brief Checks if a given piece of memory can be used to store a task's stack
*
* - Defined in heap_idf.c
*
* @param ptr Pointer to memory
* @return true Memory can be used to store a task stack
* @return false Otherwise
*/
bool xPortcheckValidStackMem(const void *ptr);

#define pvPortMallocTcbMem(size) pvPortMalloc(size)
#define pvPortMallocStackMem(size) pvPortMalloc(size)
#define portVALID_TCB_MEM(ptr) xPortCheckValidTCBMem(ptr)
#define portVALID_STACK_MEM(ptr) xPortcheckValidStackMem(ptr)

BaseType_t xPortCheckIfInISR(void);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,20 +303,6 @@ FORCE_INLINE_ATTR BaseType_t xPortGetCoreID(void)
* - Maps to forward declared functions
* ------------------------------------------------------------------------------------------------------------------ */

// ----------------------- Memory --------------------------

/**
* @brief Task memory allocation macros
*
* @note Because the ROM routines don't necessarily handle a stack in external RAM correctly, we force the stack
* memory to always be internal.
* @note [refactor-todo] Update portable.h to match v10.4.3 to use new malloc prototypes
*/
#define portTcbMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
#define portStackMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
#define pvPortMallocTcbMem(size) pvPortMalloc(size)
#define pvPortMallocStackMem(size) pvPortMalloc(size)

// --------------------- Interrupts ------------------------

#define portDISABLE_INTERRUPTS() portSET_INTERRUPT_MASK_FROM_ISR()
Expand Down Expand Up @@ -446,7 +432,7 @@ FORCE_INLINE_ATTR bool xPortCanYield(void)
/**
* @brief Checks if a given piece of memory can be used to store a task's TCB
*
* - Defined in port_common.c
* - Defined in heap_idf.c
*
* @param ptr Pointer to memory
* @return true Memory can be used to store a TCB
Expand All @@ -457,16 +443,16 @@ bool xPortCheckValidTCBMem(const void *ptr);
/**
* @brief Checks if a given piece of memory can be used to store a task's stack
*
* - Defined in port_common.c
* - Defined in heap_idf.c
*
* @param ptr Pointer to memory
* @return true Memory can be used to store a task stack
* @return false Otherwise
*/
bool xPortcheckValidStackMem(const void *ptr);

#define portVALID_TCB_MEM(ptr) xPortCheckValidTCBMem(ptr)
#define portVALID_STACK_MEM(ptr) xPortcheckValidStackMem(ptr)
#define portVALID_TCB_MEM(ptr) xPortCheckValidTCBMem(ptr)
#define portVALID_STACK_MEM(ptr) xPortcheckValidStackMem(ptr)

// --------------------- App-Trace -------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,20 +414,6 @@ FORCE_INLINE_ATTR BaseType_t xPortGetCoreID(void);
* - Maps to forward declared functions
* ------------------------------------------------------------------------------------------------------------------ */

// ----------------------- Memory --------------------------

/**
* @brief Task memory allocation macros
*
* @note Because the ROM routines don't necessarily handle a stack in external RAM correctly, we force the stack
* memory to always be internal.
* @note [refactor-todo] Update portable.h to match v10.4.3 to use new malloc prototypes
*/
#define portTcbMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
#define portStackMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
#define pvPortMallocTcbMem(size) heap_caps_malloc(size, portTcbMemoryCaps)
#define pvPortMallocStackMem(size) heap_caps_malloc(size, portStackMemoryCaps)

// --------------------- Interrupts ------------------------

/**
Expand Down Expand Up @@ -656,7 +642,7 @@ void vPortCleanUpCoprocArea(void *pvTCB);
/**
* @brief Checks if a given piece of memory can be used to store a task's TCB
*
* - Defined in port_common.c
* - Defined in heap_idf.c
*
* @param ptr Pointer to memory
* @return true Memory can be used to store a TCB
Expand All @@ -667,16 +653,16 @@ bool xPortCheckValidTCBMem(const void *ptr);
/**
* @brief Checks if a given piece of memory can be used to store a task's stack
*
* - Defined in port_common.c
* - Defined in heap_idf.c
*
* @param ptr Pointer to memory
* @return true Memory can be used to store a task stack
* @return false Otherwise
*/
bool xPortcheckValidStackMem(const void *ptr);

#define portVALID_TCB_MEM(ptr) xPortCheckValidTCBMem(ptr)
#define portVALID_STACK_MEM(ptr) xPortcheckValidStackMem(ptr)
#define portVALID_TCB_MEM(ptr) xPortCheckValidTCBMem(ptr)
#define portVALID_STACK_MEM(ptr) xPortcheckValidStackMem(ptr)

// --------------------- App-Trace -------------------------

Expand Down
Loading

0 comments on commit daf4150

Please sign in to comment.