In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "FreeRTOS dynamic memory allocation management example analysis", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "FreeRTOS dynamic memory allocation management example analysis"!
Dynamic memory management
FreeRTOS provides five dynamic memory management strategies, respectively from heap_1 to heap_5. The source code is under FreeRTOS/Source/portable/MemMang, which essentially operates on one or more large arrays to provide memory request and release (some policies do not have) function for the system. Let's take a look at how heap_1 does it.
Heap_1.c memory heap management
Where is the large array?
/ * Allocate the memory for the heap. * / # if (configAPPLICATION_ALLOCATED_HEAP = = 1) / / in this case, the array to be managed can be assigned to external SRAM and SDRAM to extern uint8_t ucHeap [configTOTAL_HEAP_SIZE]; # else// in this case, the array to be managed is assigned to internal RAM, and the address static uint8_t ucHeap [configTOTAL_HEAP_SIZE] is determined by the compiler; # endif / * configAPPLICATION_ALLOCATED_HEAP * /
You can see that the name of the local static global large array is ucHeap and the size is configTOTAL_HEAP_SIZE. This macro is defined in FreeRTOSConfig.h.
Actual available array bytes / / since byte alignment is required, the actual number of bytes of memory that can be used should be subtracted from portBYTE_ALIGNMENT/* A few bytes might be lost to byte aligning the heap start address. * / # define configADJUSTED_HEAP_SIZE (configTOTAL_HEAP_SIZE-portBYTE_ALIGNMENT)
PortBYTE_ALIGNMENT is defined in portmacro.h
# define portBYTE_ALIGNMENT8
Number of bytes allocated
/ / the number of bytes that have been allocated, that is, the offset of the next free memory from the first address (pucAlignedHeap) static size_t xNextFreeByte = (size_t) 0; assign void * pvPortMalloc (size_t xWantedSize) {void * pvReturn = NULL;// to be returned to the user to assign the address static uint8_t * pucAlignedHeap = the first address of the array actually managed by NULL;// / * Ensure that blocks are always aligned to the required number of bytes. * / / if it is not 1-byte alignment, portBYTE_ALIGNMENT byte alignment is required first # if (portBYTE_ALIGNMENT! = 1) {if (xWantedSize & portBYTE_ALIGNMENT_MASK) {/ * Byte alignment required. * / / if the number of bytes requested by the user is not portBYTE_ALIGNMENT_MASK byte alignment, first adjust to portBYTE_ALIGNMENT_MASK byte alignment / / for example, apply for 13 bytes and require portBYTE_ALIGNMENT = 8, / / then xWantedSize = 13 + (8-(13-7)) = 13 + (8-5) = 16 / / final application 16-byte xWantedSize + = (portBYTE_ALIGNMENT-(xWantedSize & portBYTE_ALIGNMENT_MASK)) } # endif// suspends the scheduler to prevent functions from reentering vTaskSuspendAll () {if (pucAlignedHeap = = NULL) / / indicates that the first call to this function requires initializing the memory heap to ensure that the memory heap header address is also 8-byte aligned {/ * Ensure the heap starts on a correctly aligned boundary. * / / assume that & ucHeap is 0x20000C64 / / then & ucHeap [portBYTE_ALIGNMENT] is 0x20000C64+7=0x20000C6B / / pucAlignedHeap = 0x20000C6B & (~ 0x00000007) = 0x20000C68 / / pucAlignedHeap is the actual heap header pucAlignedHeap = (uint8_t *) (portPOINTER_SIZE_TYPE) & ucHeap [portBYTE_ALIGNMENT]) & ((portPOINTER_) SIZE_TYPE) portBYTE_ALIGNMENT_MASK) } / * Check there is enough room left for the allocation. * / / the number of bytes allocated xNextFreeByte + the number of bytes to be allocated xWantedSize / / is less than the total total number of bytes configADJUSTED_HEAP_SIZE if (xNextFreeByte + xWantedSize)
< configADJUSTED_HEAP_SIZE ) && //此条件是防止溢出,因为内存是地址是单调增长 ( ( xNextFreeByte + xWantedSize ) >XNextFreeByte) / * Check for overflow. * / {/ * Return the next free byte then increment the index past this block * / / return address to user pvReturn = pucAlignedHeap + xNextFreeByte; / / update allocated memory bytes xNextFreeByte + = xWantedSize;} traceMALLOC (pvReturn, xWantedSize);} (void) xTaskResumeAll () / / unmount the scheduler / / if the enabled memory request fails the hook function will execute the request failure hook function # if (configUSE_MALLOC_FAILED_HOOK = = 1) {if (pvReturn = = NULL) {extern void vApplicationMallocFailedHook (void); vApplicationMallocFailedHook () }} # endif return pvReturn;}
Where portBYTE_ALIGNMENT_MASK is defined by portBYTE_ALIGNMENT, in portable.h
# if portBYTE_ALIGNMENT = = 8 # define portBYTE_ALIGNMENT_MASK (0x0007) # endif release
You can see that heap_1 does not provide release, so it cannot be released.
Void vPortFree (void * pv) {/ * Memory cannot be freed using this scheme. See heap_2.c, heap_3.c and heap_4.c for alternative implementations, and the memory management pages of http://www.FreeRTOS.org for more information. * / (void) pv; / * Force an assert as it is invalid to call this function * / configASSERT (pv = = NULL);} size_t xPortGetFreeHeapSize (void) {return (configADJUSTED_HEAP_SIZE-xNextFreeByte);}
Scope of application and characteristics
It is suitable for only allocation, no release, fixed execution time, no fragmentation, but low memory utilization.
At this point, I believe that everyone on the "FreeRTOS dynamic memory allocation management example analysis" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.