Skip to content

Commit daf4150

Browse files
committed
Merge branch 'refactor/freertos_port_malloc_macros' into 'master'
FreeRTOS(IDF): Refactor pvPortMalloc()/vPortFree(), Implement IDF heap wrapper for FreeRTOS. Closes IDF-3997 See merge request espressif/esp-idf!22565
2 parents 56dc272 + 4069a62 commit daf4150

File tree

17 files changed

+266
-232
lines changed

17 files changed

+266
-232
lines changed

components/freertos/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ set(arch "linux")
2121
endif()
2222

2323
set(srcs
24+
"heap_idf.c"
2425
"${kernel_dir}/list.c"
2526
"${kernel_dir}/queue.c"
2627
"${kernel_dir}/tasks.c"

components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/include/freertos/portmacro.h

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -286,15 +286,32 @@ static inline bool IRAM_ATTR xPortCanYield(void)
286286

287287
void vPortSetStackWatchpoint(void *pxStackStart);
288288

289-
#define portVALID_TCB_MEM(ptr) (esp_ptr_internal(ptr) && esp_ptr_byte_accessible(ptr))
290-
#ifdef CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
291-
#define portVALID_STACK_MEM(ptr) (esp_ptr_byte_accessible(ptr))
292-
#else
293-
#define portVALID_STACK_MEM(ptr) (esp_ptr_internal(ptr) && esp_ptr_byte_accessible(ptr))
294-
#endif
289+
// -------------------- Heap Related -----------------------
290+
291+
/**
292+
* @brief Checks if a given piece of memory can be used to store a task's TCB
293+
*
294+
* - Defined in heap_idf.c
295+
*
296+
* @param ptr Pointer to memory
297+
* @return true Memory can be used to store a TCB
298+
* @return false Otherwise
299+
*/
300+
bool xPortCheckValidTCBMem(const void *ptr);
301+
302+
/**
303+
* @brief Checks if a given piece of memory can be used to store a task's stack
304+
*
305+
* - Defined in heap_idf.c
306+
*
307+
* @param ptr Pointer to memory
308+
* @return true Memory can be used to store a task stack
309+
* @return false Otherwise
310+
*/
311+
bool xPortcheckValidStackMem(const void *ptr);
295312

296-
#define portTcbMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
297-
#define portStackMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
313+
#define portVALID_TCB_MEM(ptr) xPortCheckValidTCBMem(ptr)
314+
#define portVALID_STACK_MEM(ptr) xPortcheckValidStackMem(ptr)
298315

299316
/* ------------------------------------------------------ Misc ---------------------------------------------------------
300317
* - Miscellaneous porting macros

components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -349,42 +349,6 @@ void vPortEndScheduler(void)
349349
abort();
350350
}
351351

352-
// ----------------------- Memory --------------------------
353-
354-
#define FREERTOS_SMP_MALLOC_CAPS (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
355-
356-
void *pvPortMalloc( size_t xSize )
357-
{
358-
return heap_caps_malloc(xSize, FREERTOS_SMP_MALLOC_CAPS);
359-
}
360-
361-
void vPortFree( void *pv )
362-
{
363-
heap_caps_free(pv);
364-
}
365-
366-
void vPortInitialiseBlocks( void )
367-
{
368-
; //Does nothing, heap is initialized separately in ESP-IDF
369-
}
370-
371-
size_t xPortGetFreeHeapSize( void )
372-
{
373-
return esp_get_free_heap_size();
374-
}
375-
376-
#if( configSTACK_ALLOCATION_FROM_SEPARATE_HEAP == 1 )
377-
void *pvPortMallocStack( size_t xSize )
378-
{
379-
return NULL;
380-
}
381-
382-
void vPortFreeStack( void *pv )
383-
{
384-
385-
}
386-
#endif
387-
388352
// ------------------------ Stack --------------------------
389353

390354
/**

components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/include/freertos/portmacro.h

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -352,15 +352,32 @@ static inline bool IRAM_ATTR xPortCanYield(void)
352352

353353
void vPortSetStackWatchpoint(void *pxStackStart);
354354

355-
#define portVALID_TCB_MEM(ptr) (esp_ptr_internal(ptr) && esp_ptr_byte_accessible(ptr))
356-
#ifdef CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
357-
#define portVALID_STACK_MEM(ptr) (esp_ptr_byte_accessible(ptr))
358-
#else
359-
#define portVALID_STACK_MEM(ptr) (esp_ptr_internal(ptr) && esp_ptr_byte_accessible(ptr))
360-
#endif
355+
// -------------------- Heap Related -----------------------
356+
357+
/**
358+
* @brief Checks if a given piece of memory can be used to store a task's TCB
359+
*
360+
* - Defined in heap_idf.c
361+
*
362+
* @param ptr Pointer to memory
363+
* @return true Memory can be used to store a TCB
364+
* @return false Otherwise
365+
*/
366+
bool xPortCheckValidTCBMem(const void *ptr);
367+
368+
/**
369+
* @brief Checks if a given piece of memory can be used to store a task's stack
370+
*
371+
* - Defined in heap_idf.c
372+
*
373+
* @param ptr Pointer to memory
374+
* @return true Memory can be used to store a task stack
375+
* @return false Otherwise
376+
*/
377+
bool xPortcheckValidStackMem(const void *ptr);
361378

362-
#define portTcbMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
363-
#define portStackMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
379+
#define portVALID_TCB_MEM(ptr) xPortCheckValidTCBMem(ptr)
380+
#define portVALID_STACK_MEM(ptr) xPortcheckValidStackMem(ptr)
364381

365382
// --------------- Compatibility Includes ------------------
366383
/*

components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/port.c

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -386,42 +386,6 @@ void vPortEndScheduler( void )
386386
;
387387
}
388388

389-
// ----------------------- Memory --------------------------
390-
391-
#define FREERTOS_SMP_MALLOC_CAPS (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
392-
393-
void *pvPortMalloc( size_t xSize )
394-
{
395-
return heap_caps_malloc(xSize, FREERTOS_SMP_MALLOC_CAPS);
396-
}
397-
398-
void vPortFree( void *pv )
399-
{
400-
heap_caps_free(pv);
401-
}
402-
403-
void vPortInitialiseBlocks( void )
404-
{
405-
; //Does nothing, heap is initialized separately in ESP-IDF
406-
}
407-
408-
size_t xPortGetFreeHeapSize( void )
409-
{
410-
return esp_get_free_heap_size();
411-
}
412-
413-
#if( configSTACK_ALLOCATION_FROM_SEPARATE_HEAP == 1 )
414-
void *pvPortMallocStack( size_t xSize )
415-
{
416-
return NULL;
417-
}
418-
419-
void vPortFreeStack( void *pv )
420-
{
421-
422-
}
423-
#endif
424-
425389
// ------------------------ Stack --------------------------
426390

427391
// User exception dispatcher when exiting

components/freertos/FreeRTOS-Kernel/include/freertos/portable.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@
139139
#endif
140140
#endif /* if ( portUSING_MPU_WRAPPERS == 1 ) */
141141

142-
#ifdef configUSE_FREERTOS_PROVIDED_HEAP
143-
144142
/* Used by heap_5.c to define the start address and size of each memory region
145143
* that together comprise the total FreeRTOS heap space. */
146144
typedef struct HeapRegion
@@ -189,29 +187,6 @@ void vPortInitialiseBlocks( void ) PRIVILEGED_FUNCTION;
189187
size_t xPortGetFreeHeapSize( void ) PRIVILEGED_FUNCTION;
190188
size_t xPortGetMinimumEverFreeHeapSize( void ) PRIVILEGED_FUNCTION;
191189

192-
#if( configSTACK_ALLOCATION_FROM_SEPARATE_HEAP == 1 )
193-
void *pvPortMallocStack( size_t xSize ) PRIVILEGED_FUNCTION;
194-
void vPortFreeStack( void *pv ) PRIVILEGED_FUNCTION;
195-
#else
196-
#define pvPortMallocStack pvPortMalloc
197-
#define vPortFreeStack vPortFree
198-
#endif
199-
#else // configUSE_FREERTOS_PROVIDED_HEAP
200-
201-
/*
202-
* Map to the memory management routines required for the port.
203-
*
204-
* Note that libc standard malloc/free are also available for
205-
* non-FreeRTOS-specific code, and behave the same as
206-
* pvPortMalloc()/vPortFree().
207-
*/
208-
#define pvPortMalloc malloc
209-
#define vPortFree free
210-
#define xPortGetFreeHeapSize esp_get_free_heap_size
211-
#define xPortGetMinimumEverFreeHeapSize esp_get_minimum_free_heap_size
212-
213-
#endif
214-
215190
/*
216191
* Setup the hardware ready for the scheduler to take control. This generally
217192
* sets up a tick interrupt and sets timers for the correct tick frequency.

components/freertos/FreeRTOS-Kernel/portable/linux/include/freertos/portmacro_idf.h

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,30 @@ static inline BaseType_t IRAM_ATTR xPortGetCoreID(void)
5151
return (BaseType_t) 0;
5252
}
5353

54-
static inline bool portVALID_TCB_MEM(const void *ptr)
55-
{
56-
return true;
57-
}
54+
/**
55+
* @brief Checks if a given piece of memory can be used to store a task's TCB
56+
*
57+
* - Defined in heap_idf.c
58+
*
59+
* @param ptr Pointer to memory
60+
* @return true Memory can be used to store a TCB
61+
* @return false Otherwise
62+
*/
63+
bool xPortCheckValidTCBMem(const void *ptr);
5864

59-
static inline bool portVALID_STACK_MEM(const void *ptr)
60-
{
61-
return true;
62-
}
65+
/**
66+
* @brief Checks if a given piece of memory can be used to store a task's stack
67+
*
68+
* - Defined in heap_idf.c
69+
*
70+
* @param ptr Pointer to memory
71+
* @return true Memory can be used to store a task stack
72+
* @return false Otherwise
73+
*/
74+
bool xPortcheckValidStackMem(const void *ptr);
6375

64-
#define pvPortMallocTcbMem(size) pvPortMalloc(size)
65-
#define pvPortMallocStackMem(size) pvPortMalloc(size)
76+
#define portVALID_TCB_MEM(ptr) xPortCheckValidTCBMem(ptr)
77+
#define portVALID_STACK_MEM(ptr) xPortcheckValidStackMem(ptr)
6678

6779
BaseType_t xPortCheckIfInISR(void);
6880

components/freertos/FreeRTOS-Kernel/portable/riscv/include/freertos/portmacro.h

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -303,20 +303,6 @@ FORCE_INLINE_ATTR BaseType_t xPortGetCoreID(void)
303303
* - Maps to forward declared functions
304304
* ------------------------------------------------------------------------------------------------------------------ */
305305

306-
// ----------------------- Memory --------------------------
307-
308-
/**
309-
* @brief Task memory allocation macros
310-
*
311-
* @note Because the ROM routines don't necessarily handle a stack in external RAM correctly, we force the stack
312-
* memory to always be internal.
313-
* @note [refactor-todo] Update portable.h to match v10.4.3 to use new malloc prototypes
314-
*/
315-
#define portTcbMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
316-
#define portStackMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
317-
#define pvPortMallocTcbMem(size) pvPortMalloc(size)
318-
#define pvPortMallocStackMem(size) pvPortMalloc(size)
319-
320306
// --------------------- Interrupts ------------------------
321307

322308
#define portDISABLE_INTERRUPTS() portSET_INTERRUPT_MASK_FROM_ISR()
@@ -446,7 +432,7 @@ FORCE_INLINE_ATTR bool xPortCanYield(void)
446432
/**
447433
* @brief Checks if a given piece of memory can be used to store a task's TCB
448434
*
449-
* - Defined in port_common.c
435+
* - Defined in heap_idf.c
450436
*
451437
* @param ptr Pointer to memory
452438
* @return true Memory can be used to store a TCB
@@ -457,16 +443,16 @@ bool xPortCheckValidTCBMem(const void *ptr);
457443
/**
458444
* @brief Checks if a given piece of memory can be used to store a task's stack
459445
*
460-
* - Defined in port_common.c
446+
* - Defined in heap_idf.c
461447
*
462448
* @param ptr Pointer to memory
463449
* @return true Memory can be used to store a task stack
464450
* @return false Otherwise
465451
*/
466452
bool xPortcheckValidStackMem(const void *ptr);
467453

468-
#define portVALID_TCB_MEM(ptr) xPortCheckValidTCBMem(ptr)
469-
#define portVALID_STACK_MEM(ptr) xPortcheckValidStackMem(ptr)
454+
#define portVALID_TCB_MEM(ptr) xPortCheckValidTCBMem(ptr)
455+
#define portVALID_STACK_MEM(ptr) xPortcheckValidStackMem(ptr)
470456

471457
// --------------------- App-Trace -------------------------
472458

components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -414,20 +414,6 @@ FORCE_INLINE_ATTR BaseType_t xPortGetCoreID(void);
414414
* - Maps to forward declared functions
415415
* ------------------------------------------------------------------------------------------------------------------ */
416416

417-
// ----------------------- Memory --------------------------
418-
419-
/**
420-
* @brief Task memory allocation macros
421-
*
422-
* @note Because the ROM routines don't necessarily handle a stack in external RAM correctly, we force the stack
423-
* memory to always be internal.
424-
* @note [refactor-todo] Update portable.h to match v10.4.3 to use new malloc prototypes
425-
*/
426-
#define portTcbMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
427-
#define portStackMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
428-
#define pvPortMallocTcbMem(size) heap_caps_malloc(size, portTcbMemoryCaps)
429-
#define pvPortMallocStackMem(size) heap_caps_malloc(size, portStackMemoryCaps)
430-
431417
// --------------------- Interrupts ------------------------
432418

433419
/**
@@ -656,7 +642,7 @@ void vPortCleanUpCoprocArea(void *pvTCB);
656642
/**
657643
* @brief Checks if a given piece of memory can be used to store a task's TCB
658644
*
659-
* - Defined in port_common.c
645+
* - Defined in heap_idf.c
660646
*
661647
* @param ptr Pointer to memory
662648
* @return true Memory can be used to store a TCB
@@ -667,16 +653,16 @@ bool xPortCheckValidTCBMem(const void *ptr);
667653
/**
668654
* @brief Checks if a given piece of memory can be used to store a task's stack
669655
*
670-
* - Defined in port_common.c
656+
* - Defined in heap_idf.c
671657
*
672658
* @param ptr Pointer to memory
673659
* @return true Memory can be used to store a task stack
674660
* @return false Otherwise
675661
*/
676662
bool xPortcheckValidStackMem(const void *ptr);
677663

678-
#define portVALID_TCB_MEM(ptr) xPortCheckValidTCBMem(ptr)
679-
#define portVALID_STACK_MEM(ptr) xPortcheckValidStackMem(ptr)
664+
#define portVALID_TCB_MEM(ptr) xPortCheckValidTCBMem(ptr)
665+
#define portVALID_STACK_MEM(ptr) xPortcheckValidStackMem(ptr)
680666

681667
// --------------------- App-Trace -------------------------
682668

0 commit comments

Comments
 (0)