From cdb854ffc60de71f95ca681c7f02a5dbd9df7c02 Mon Sep 17 00:00:00 2001 From: jasonpcarroll <23126711+jasonpcarroll@users.noreply.github.com> Date: Wed, 30 Nov 2022 14:19:14 -0800 Subject: [PATCH 1/4] Add option to not include heap in CMakeLists.txt --- CMakeLists.txt | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d4a7799f7c..ec60888d608 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -224,16 +224,25 @@ endif() add_subdirectory(portable) -add_library(freertos_kernel STATIC +list(APPEND FREERTOS_KERNEL_SOURCES event_groups.c list.c queue.c stream_buffer.c tasks.c timers.c +) - # If FREERTOS_HEAP is digit between 1 .. 5 - it is heap number, otherwise - it is path to custom heap source file - $>,${FREERTOS_HEAP},portable/MemMang/heap_${FREERTOS_HEAP}.c> +# Check if user requested to not inlude a heap implementation +if(NOT DEFINED FREERTOS_DO_NOT_INCLUDE_HEAP) + list(APPEND FREERTOS_KERNEL_SOURCES + # If FREERTOS_HEAP is digit between 1 .. 5 - it is heap number, otherwise - it is path to custom heap source file + $>,${FREERTOS_HEAP},portable/MemMang/heap_${FREERTOS_HEAP}.c> + ) +endif() + +add_library(freertos_kernel STATIC + ${FREERTOS_KERNEL_SOURCES} ) target_include_directories(freertos_kernel From f22169dc23e7300497258fbd81a6061c65cb882a Mon Sep 17 00:00:00 2001 From: Soren Ptak Date: Wed, 20 Sep 2023 13:15:40 -0700 Subject: [PATCH 2/4] Fix a spelling mistake in CMakeLists.txt --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1005843e6db..44da9b0a0ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -286,7 +286,7 @@ target_sources(freertos_kernel PRIVATE timers.c ) -# Check if user requested to not inlude a heap implementation +# Check if user requested to not include a heap implementation if(NOT DEFINED FREERTOS_DO_NOT_INCLUDE_HEAP) list(APPEND FREERTOS_KERNEL_SOURCES # If FREERTOS_HEAP is digit between 1 .. 5 - it is heap number, otherwise - it is path to custom heap source file From 8c9b0325d2f0c1ee0bcd7b6b738625f0cd7a081f Mon Sep 17 00:00:00 2001 From: Soren Ptak Date: Wed, 20 Sep 2023 17:17:19 -0700 Subject: [PATCH 3/4] Wrap the heap based parts of portable.h in if dynamic allocation is supported. Update the CMakeLists.txt to account for this --- CMakeLists.txt | 12 ++--- include/portable.h | 130 +++++++++++++++++++++++---------------------- 2 files changed, 70 insertions(+), 72 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 44da9b0a0ba..f9eeb2a1ee7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -284,18 +284,12 @@ target_sources(freertos_kernel PRIVATE stream_buffer.c tasks.c timers.c -) -# Check if user requested to not include a heap implementation -if(NOT DEFINED FREERTOS_DO_NOT_INCLUDE_HEAP) - list(APPEND FREERTOS_KERNEL_SOURCES + # Check if user requested to not include a heap implementation + if(NOT DEFINED FREERTOS_DO_NOT_INCLUDE_HEAP) # If FREERTOS_HEAP is digit between 1 .. 5 - it is heap number, otherwise - it is path to custom heap source file $>,${FREERTOS_HEAP},portable/MemMang/heap_${FREERTOS_HEAP}.c> - ) -endif() - -add_library(freertos_kernel STATIC - ${FREERTOS_KERNEL_SOURCES} + endif() ) diff --git a/include/portable.h b/include/portable.h index 615d77d5acf..c63530f8a82 100644 --- a/include/portable.h +++ b/include/portable.h @@ -132,76 +132,80 @@ #endif #endif /* if ( portUSING_MPU_WRAPPERS == 1 ) */ -/* 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 -{ - uint8_t * pucStartAddress; - size_t xSizeInBytes; -} HeapRegion_t; +/* Only include heap related functions and structs if using dynamic allocation */ +#if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) -/* Used to pass information about the heap out of vPortGetHeapStats(). */ -typedef struct xHeapStats -{ - size_t xAvailableHeapSpaceInBytes; /* The total heap size currently available - this is the sum of all the free blocks, not the largest block that can be allocated. */ - size_t xSizeOfLargestFreeBlockInBytes; /* The maximum size, in bytes, of all the free blocks within the heap at the time vPortGetHeapStats() is called. */ - size_t xSizeOfSmallestFreeBlockInBytes; /* The minimum size, in bytes, of all the free blocks within the heap at the time vPortGetHeapStats() is called. */ - size_t xNumberOfFreeBlocks; /* The number of free memory blocks within the heap at the time vPortGetHeapStats() is called. */ - size_t xMinimumEverFreeBytesRemaining; /* The minimum amount of total free memory (sum of all free blocks) there has been in the heap since the system booted. */ - size_t xNumberOfSuccessfulAllocations; /* The number of calls to pvPortMalloc() that have returned a valid memory block. */ - size_t xNumberOfSuccessfulFrees; /* The number of calls to vPortFree() that has successfully freed a block of memory. */ -} HeapStats_t; + /* 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 + { + uint8_t * pucStartAddress; + size_t xSizeInBytes; + } HeapRegion_t; -/* - * Used to define multiple heap regions for use by heap_5.c. This function - * must be called before any calls to pvPortMalloc() - not creating a task, - * queue, semaphore, mutex, software timer, event group, etc. will result in - * pvPortMalloc being called. - * - * pxHeapRegions passes in an array of HeapRegion_t structures - each of which - * defines a region of memory that can be used as the heap. The array is - * terminated by a HeapRegions_t structure that has a size of 0. The region - * with the lowest start address must appear first in the array. - */ -void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions ) PRIVILEGED_FUNCTION; + /* Used to pass information about the heap out of vPortGetHeapStats(). */ + typedef struct xHeapStats + { + size_t xAvailableHeapSpaceInBytes; /* The total heap size currently available - this is the sum of all the free blocks, not the largest block that can be allocated. */ + size_t xSizeOfLargestFreeBlockInBytes; /* The maximum size, in bytes, of all the free blocks within the heap at the time vPortGetHeapStats() is called. */ + size_t xSizeOfSmallestFreeBlockInBytes; /* The minimum size, in bytes, of all the free blocks within the heap at the time vPortGetHeapStats() is called. */ + size_t xNumberOfFreeBlocks; /* The number of free memory blocks within the heap at the time vPortGetHeapStats() is called. */ + size_t xMinimumEverFreeBytesRemaining; /* The minimum amount of total free memory (sum of all free blocks) there has been in the heap since the system booted. */ + size_t xNumberOfSuccessfulAllocations; /* The number of calls to pvPortMalloc() that have returned a valid memory block. */ + size_t xNumberOfSuccessfulFrees; /* The number of calls to vPortFree() that has successfully freed a block of memory. */ + } HeapStats_t; -/* - * Returns a HeapStats_t structure filled with information about the current - * heap state. - */ -void vPortGetHeapStats( HeapStats_t * pxHeapStats ); + /* + * Used to define multiple heap regions for use by heap_5.c. This function + * must be called before any calls to pvPortMalloc() - not creating a task, + * queue, semaphore, mutex, software timer, event group, etc. will result in + * pvPortMalloc being called. + * + * pxHeapRegions passes in an array of HeapRegion_t structures - each of which + * defines a region of memory that can be used as the heap. The array is + * terminated by a HeapRegions_t structure that has a size of 0. The region + * with the lowest start address must appear first in the array. + */ + void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions ) PRIVILEGED_FUNCTION; -/* - * Map to the memory management routines required for the port. - */ -void * pvPortMalloc( size_t xSize ) PRIVILEGED_FUNCTION; -void * pvPortCalloc( size_t xNum, - size_t xSize ) PRIVILEGED_FUNCTION; -void vPortFree( void * pv ) PRIVILEGED_FUNCTION; -void vPortInitialiseBlocks( void ) PRIVILEGED_FUNCTION; -size_t xPortGetFreeHeapSize( void ) PRIVILEGED_FUNCTION; -size_t xPortGetMinimumEverFreeHeapSize( void ) PRIVILEGED_FUNCTION; + /* + * Returns a HeapStats_t structure filled with information about the current + * heap state. + */ + void vPortGetHeapStats( HeapStats_t * pxHeapStats ); -#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 + /* + * Map to the memory management routines required for the port. + */ + void * pvPortMalloc( size_t xSize ) PRIVILEGED_FUNCTION; + void * pvPortCalloc( size_t xNum, + size_t xSize ) PRIVILEGED_FUNCTION; + void vPortFree( void * pv ) PRIVILEGED_FUNCTION; + void vPortInitialiseBlocks( void ) PRIVILEGED_FUNCTION; + size_t xPortGetFreeHeapSize( void ) PRIVILEGED_FUNCTION; + size_t xPortGetMinimumEverFreeHeapSize( void ) PRIVILEGED_FUNCTION; -#if ( configUSE_MALLOC_FAILED_HOOK == 1 ) + #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 -/** - * task.h - * @code{c} - * void vApplicationMallocFailedHook( void ) - * @endcode - * - * This hook function is called when allocation failed. - */ - void vApplicationMallocFailedHook( void ); /*lint !e526 Symbol not defined as it is an application callback. */ -#endif + #if ( configUSE_MALLOC_FAILED_HOOK == 1 ) + /** + * task.h + * @code{c} + * void vApplicationMallocFailedHook( void ) + * @endcode + * + * This hook function is called when allocation failed. + */ + void vApplicationMallocFailedHook( void ); /*lint !e526 Symbol not defined as it is an application callback. */ + #endif /* ( configUSE_MALLOC_FAILED_HOOK == 1 ) */ + +#endif /* if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) */ /* * Setup the hardware ready for the scheduler to take control. This generally From 2aff1a1c452719a43fa22ef0fed1d81d85b984cc Mon Sep 17 00:00:00 2001 From: Soren Ptak Date: Thu, 21 Sep 2023 10:41:40 -0700 Subject: [PATCH 4/4] Work on making it so this cmake file can be included from anywhere, not just if it's used from this directoy --- CMakeLists.txt | 31 +++++++++++---------- include/CMakeLists.txt | 2 +- include/portable.h | 63 +++++++++++++++++++++--------------------- 3 files changed, 49 insertions(+), 47 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f9eeb2a1ee7..c092da6c381 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -273,25 +273,26 @@ target_compile_options(freertos_kernel PRIVATE ######################################################################## -add_subdirectory(include) -add_subdirectory(portable) +include(${CMAKE_CURRENT_LIST_DIR}/include/CMakeLists.txt) +include(${CMAKE_CURRENT_LIST_DIR}/portable/CMakeLists.txt) target_sources(freertos_kernel PRIVATE - croutine.c - event_groups.c - list.c - queue.c - stream_buffer.c - tasks.c - timers.c - - # Check if user requested to not include a heap implementation - if(NOT DEFINED FREERTOS_DO_NOT_INCLUDE_HEAP) - # If FREERTOS_HEAP is digit between 1 .. 5 - it is heap number, otherwise - it is path to custom heap source file - $>,${FREERTOS_HEAP},portable/MemMang/heap_${FREERTOS_HEAP}.c> - endif() + ${CMAKE_CURRENT_LIST_DIR}/croutine.c + ${CMAKE_CURRENT_LIST_DIR}/event_groups.c + ${CMAKE_CURRENT_LIST_DIR}/list.c + ${CMAKE_CURRENT_LIST_DIR}/queue.c + ${CMAKE_CURRENT_LIST_DIR}/stream_buffer.c + ${CMAKE_CURRENT_LIST_DIR}/tasks.c + ${CMAKE_CURRENT_LIST_DIR}/timers.c ) +# Check if user requested to not include a heap implementation +if(NOT DEFINED FREERTOS_DO_NOT_INCLUDE_HEAP) + target_sources(freertos_kernel PRIVATE + # If FREERTOS_HEAP is digit between 1 .. 5 - it is heap number, otherwise - it is path to custom heap source file + $>,${FREERTOS_HEAP},portable/MemMang/heap_${FREERTOS_HEAP}.c> + ) +endif() target_link_libraries(freertos_kernel PUBLIC diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 46a1c3e7f5f..7b28df06655 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -4,7 +4,7 @@ add_library(freertos_kernel_include INTERFACE) target_include_directories(freertos_kernel_include INTERFACE - . + ${CMAKE_CURRENT_LIST_DIR}/. # Note: DEPRECATED but still supported, may be removed in a future release. $<$>:${FREERTOS_CONFIG_FILE_DIRECTORY}> ) diff --git a/include/portable.h b/include/portable.h index c63530f8a82..46bd98be9a4 100644 --- a/include/portable.h +++ b/include/portable.h @@ -135,15 +135,15 @@ /* Only include heap related functions and structs if using dynamic allocation */ #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) - /* Used by heap_5.c to define the start address and size of each memory region - * that together comprise the total FreeRTOS heap space. */ +/* 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 { uint8_t * pucStartAddress; size_t xSizeInBytes; } HeapRegion_t; - /* Used to pass information about the heap out of vPortGetHeapStats(). */ +/* Used to pass information about the heap out of vPortGetHeapStats(). */ typedef struct xHeapStats { size_t xAvailableHeapSpaceInBytes; /* The total heap size currently available - this is the sum of all the free blocks, not the largest block that can be allocated. */ @@ -155,31 +155,31 @@ size_t xNumberOfSuccessfulFrees; /* The number of calls to vPortFree() that has successfully freed a block of memory. */ } HeapStats_t; - /* - * Used to define multiple heap regions for use by heap_5.c. This function - * must be called before any calls to pvPortMalloc() - not creating a task, - * queue, semaphore, mutex, software timer, event group, etc. will result in - * pvPortMalloc being called. - * - * pxHeapRegions passes in an array of HeapRegion_t structures - each of which - * defines a region of memory that can be used as the heap. The array is - * terminated by a HeapRegions_t structure that has a size of 0. The region - * with the lowest start address must appear first in the array. - */ +/* + * Used to define multiple heap regions for use by heap_5.c. This function + * must be called before any calls to pvPortMalloc() - not creating a task, + * queue, semaphore, mutex, software timer, event group, etc. will result in + * pvPortMalloc being called. + * + * pxHeapRegions passes in an array of HeapRegion_t structures - each of which + * defines a region of memory that can be used as the heap. The array is + * terminated by a HeapRegions_t structure that has a size of 0. The region + * with the lowest start address must appear first in the array. + */ void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions ) PRIVILEGED_FUNCTION; - /* - * Returns a HeapStats_t structure filled with information about the current - * heap state. - */ +/* + * Returns a HeapStats_t structure filled with information about the current + * heap state. + */ void vPortGetHeapStats( HeapStats_t * pxHeapStats ); - /* - * Map to the memory management routines required for the port. - */ +/* + * Map to the memory management routines required for the port. + */ void * pvPortMalloc( size_t xSize ) PRIVILEGED_FUNCTION; void * pvPortCalloc( size_t xNum, - size_t xSize ) PRIVILEGED_FUNCTION; + size_t xSize ) PRIVILEGED_FUNCTION; void vPortFree( void * pv ) PRIVILEGED_FUNCTION; void vPortInitialiseBlocks( void ) PRIVILEGED_FUNCTION; size_t xPortGetFreeHeapSize( void ) PRIVILEGED_FUNCTION; @@ -194,15 +194,16 @@ #endif #if ( configUSE_MALLOC_FAILED_HOOK == 1 ) - /** - * task.h - * @code{c} - * void vApplicationMallocFailedHook( void ) - * @endcode - * - * This hook function is called when allocation failed. - */ - void vApplicationMallocFailedHook( void ); /*lint !e526 Symbol not defined as it is an application callback. */ + +/** + * task.h + * @code{c} + * void vApplicationMallocFailedHook( void ) + * @endcode + * + * This hook function is called when allocation failed. + */ + void vApplicationMallocFailedHook( void ); /*lint !e526 Symbol not defined as it is an application callback. */ #endif /* ( configUSE_MALLOC_FAILED_HOOK == 1 ) */ #endif /* if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) */