2 * FreeRTOS Kernel V10.2.0
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 * A sample implementation of pvPortMalloc() that allows the heap to be defined
\r
30 * across multiple non-contigous blocks and combines (coalescences) adjacent
\r
31 * memory blocks as they are freed.
\r
33 * See heap_1.c, heap_2.c, heap_3.c and heap_4.c for alternative
\r
34 * implementations, and the memory management pages of http://www.FreeRTOS.org
\r
35 * for more information.
\r
39 * vPortDefineHeapRegions() ***must*** be called before pvPortMalloc().
\r
40 * pvPortMalloc() will be called if any task objects (tasks, queues, event
\r
41 * groups, etc.) are created, therefore vPortDefineHeapRegions() ***must*** be
\r
42 * called before any other objects are defined.
\r
44 * vPortDefineHeapRegions() takes a single parameter. The parameter is an array
\r
45 * of HeapRegion_t structures. HeapRegion_t is defined in portable.h as
\r
47 * typedef struct HeapRegion
\r
49 * uint8_t *pucStartAddress; << Start address of a block of memory that will be part of the heap.
\r
50 * size_t xSizeInBytes; << Size of the block of memory.
\r
53 * The array is terminated using a NULL zero sized region definition, and the
\r
54 * memory regions defined in the array ***must*** appear in address order from
\r
55 * low address to high address. So the following is a valid example of how
\r
56 * to use the function.
\r
58 * HeapRegion_t xHeapRegions[] =
\r
60 * { ( uint8_t * ) 0x80000000UL, 0x10000 }, << Defines a block of 0x10000 bytes starting at address 0x80000000
\r
61 * { ( uint8_t * ) 0x90000000UL, 0xa0000 }, << Defines a block of 0xa0000 bytes starting at address of 0x90000000
\r
62 * { NULL, 0 } << Terminates the array.
\r
65 * vPortDefineHeapRegions( xHeapRegions ); << Pass the array into vPortDefineHeapRegions().
\r
67 * Note 0x80000000 is the lower address so appears in the array first.
\r
72 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
73 all the API functions to use the MPU wrappers. That should only be done when
\r
74 task.h is included from an application file. */
\r
75 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
77 #include "FreeRTOS.h"
\r
80 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
82 #if( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
\r
83 #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
\r
86 /* Block sizes must not get too small. */
\r
87 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize << 1 ) )
\r
89 /* Assumes 8bit bytes! */
\r
90 #define heapBITS_PER_BYTE ( ( size_t ) 8 )
\r
92 /* Define the linked list structure. This is used to link free blocks in order
\r
93 of their memory address. */
\r
94 typedef struct A_BLOCK_LINK
\r
96 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
\r
97 size_t xBlockSize; /*<< The size of the free block. */
\r
100 /*-----------------------------------------------------------*/
\r
103 * Inserts a block of memory that is being freed into the correct position in
\r
104 * the list of free memory blocks. The block being freed will be merged with
\r
105 * the block in front it and/or the block behind it if the memory blocks are
\r
106 * adjacent to each other.
\r
108 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert );
\r
110 /*-----------------------------------------------------------*/
\r
112 /* The size of the structure placed at the beginning of each allocated memory
\r
113 block must by correctly byte aligned. */
\r
114 static const size_t xHeapStructSize = ( sizeof( BlockLink_t ) + ( ( size_t ) ( portBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
\r
116 /* Create a couple of list links to mark the start and end of the list. */
\r
117 static BlockLink_t xStart, *pxEnd = NULL;
\r
119 /* Keeps track of the number of free bytes remaining, but says nothing about
\r
121 static size_t xFreeBytesRemaining = 0U;
\r
122 static size_t xMinimumEverFreeBytesRemaining = 0U;
\r
124 /* Gets set to the top bit of an size_t type. When this bit in the xBlockSize
\r
125 member of an BlockLink_t structure is set then the block belongs to the
\r
126 application. When the bit is free the block is still part of the free heap
\r
128 static size_t xBlockAllocatedBit = 0;
\r
130 /*-----------------------------------------------------------*/
\r
132 void *pvPortMalloc( size_t xWantedSize )
\r
134 BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
\r
135 void *pvReturn = NULL;
\r
137 /* The heap must be initialised before the first call to
\r
138 prvPortMalloc(). */
\r
139 configASSERT( pxEnd );
\r
143 /* Check the requested block size is not so large that the top bit is
\r
144 set. The top bit of the block size member of the BlockLink_t structure
\r
145 is used to determine who owns the block - the application or the
\r
146 kernel, so it must be free. */
\r
147 if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
\r
149 /* The wanted size is increased so it can contain a BlockLink_t
\r
150 structure in addition to the requested amount of bytes. */
\r
151 if( xWantedSize > 0 )
\r
153 xWantedSize += xHeapStructSize;
\r
155 /* Ensure that blocks are always aligned to the required number
\r
157 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
\r
159 /* Byte alignment required. */
\r
160 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
\r
164 mtCOVERAGE_TEST_MARKER();
\r
169 mtCOVERAGE_TEST_MARKER();
\r
172 if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
\r
174 /* Traverse the list from the start (lowest address) block until
\r
175 one of adequate size is found. */
\r
176 pxPreviousBlock = &xStart;
\r
177 pxBlock = xStart.pxNextFreeBlock;
\r
178 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
\r
180 pxPreviousBlock = pxBlock;
\r
181 pxBlock = pxBlock->pxNextFreeBlock;
\r
184 /* If the end marker was reached then a block of adequate size
\r
186 if( pxBlock != pxEnd )
\r
188 /* Return the memory space pointed to - jumping over the
\r
189 BlockLink_t structure at its start. */
\r
190 pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );
\r
192 /* This block is being returned for use so must be taken out
\r
193 of the list of free blocks. */
\r
194 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
\r
196 /* If the block is larger than required it can be split into
\r
198 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
\r
200 /* This block is to be split into two. Create a new
\r
201 block following the number of bytes requested. The void
\r
202 cast is used to prevent byte alignment warnings from the
\r
204 pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
\r
206 /* Calculate the sizes of two blocks split from the
\r
208 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
\r
209 pxBlock->xBlockSize = xWantedSize;
\r
211 /* Insert the new block into the list of free blocks. */
\r
212 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
\r
216 mtCOVERAGE_TEST_MARKER();
\r
219 xFreeBytesRemaining -= pxBlock->xBlockSize;
\r
221 if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
\r
223 xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
\r
227 mtCOVERAGE_TEST_MARKER();
\r
230 /* The block is being returned - it is allocated and owned
\r
231 by the application and has no "next" block. */
\r
232 pxBlock->xBlockSize |= xBlockAllocatedBit;
\r
233 pxBlock->pxNextFreeBlock = NULL;
\r
237 mtCOVERAGE_TEST_MARKER();
\r
242 mtCOVERAGE_TEST_MARKER();
\r
247 mtCOVERAGE_TEST_MARKER();
\r
250 traceMALLOC( pvReturn, xWantedSize );
\r
252 ( void ) xTaskResumeAll();
\r
254 #if( configUSE_MALLOC_FAILED_HOOK == 1 )
\r
256 if( pvReturn == NULL )
\r
258 extern void vApplicationMallocFailedHook( void );
\r
259 vApplicationMallocFailedHook();
\r
263 mtCOVERAGE_TEST_MARKER();
\r
270 /*-----------------------------------------------------------*/
\r
272 void vPortFree( void *pv )
\r
274 uint8_t *puc = ( uint8_t * ) pv;
\r
275 BlockLink_t *pxLink;
\r
279 /* The memory being freed will have an BlockLink_t structure immediately
\r
281 puc -= xHeapStructSize;
\r
283 /* This casting is to keep the compiler from issuing warnings. */
\r
284 pxLink = ( void * ) puc;
\r
286 /* Check the block is actually allocated. */
\r
287 configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
\r
288 configASSERT( pxLink->pxNextFreeBlock == NULL );
\r
290 if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )
\r
292 if( pxLink->pxNextFreeBlock == NULL )
\r
294 /* The block is being returned to the heap - it is no longer
\r
296 pxLink->xBlockSize &= ~xBlockAllocatedBit;
\r
300 /* Add this block to the list of free blocks. */
\r
301 xFreeBytesRemaining += pxLink->xBlockSize;
\r
302 traceFREE( pv, pxLink->xBlockSize );
\r
303 prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
\r
305 ( void ) xTaskResumeAll();
\r
309 mtCOVERAGE_TEST_MARKER();
\r
314 mtCOVERAGE_TEST_MARKER();
\r
318 /*-----------------------------------------------------------*/
\r
320 size_t xPortGetFreeHeapSize( void )
\r
322 return xFreeBytesRemaining;
\r
324 /*-----------------------------------------------------------*/
\r
326 size_t xPortGetMinimumEverFreeHeapSize( void )
\r
328 return xMinimumEverFreeBytesRemaining;
\r
330 /*-----------------------------------------------------------*/
\r
332 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )
\r
334 BlockLink_t *pxIterator;
\r
337 /* Iterate through the list until a block is found that has a higher address
\r
338 than the block being inserted. */
\r
339 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
\r
341 /* Nothing to do here, just iterate to the right position. */
\r
344 /* Do the block being inserted, and the block it is being inserted after
\r
345 make a contiguous block of memory? */
\r
346 puc = ( uint8_t * ) pxIterator;
\r
347 if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )
\r
349 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
\r
350 pxBlockToInsert = pxIterator;
\r
354 mtCOVERAGE_TEST_MARKER();
\r
357 /* Do the block being inserted, and the block it is being inserted before
\r
358 make a contiguous block of memory? */
\r
359 puc = ( uint8_t * ) pxBlockToInsert;
\r
360 if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )
\r
362 if( pxIterator->pxNextFreeBlock != pxEnd )
\r
364 /* Form one big block from the two blocks. */
\r
365 pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
\r
366 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
\r
370 pxBlockToInsert->pxNextFreeBlock = pxEnd;
\r
375 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
\r
378 /* If the block being inserted plugged a gab, so was merged with the block
\r
379 before and the block after, then it's pxNextFreeBlock pointer will have
\r
380 already been set, and should not be set here as that would make it point
\r
382 if( pxIterator != pxBlockToInsert )
\r
384 pxIterator->pxNextFreeBlock = pxBlockToInsert;
\r
388 mtCOVERAGE_TEST_MARKER();
\r
391 /*-----------------------------------------------------------*/
\r
393 void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions )
\r
395 BlockLink_t *pxFirstFreeBlockInRegion = NULL, *pxPreviousFreeBlock;
\r
396 size_t xAlignedHeap;
\r
397 size_t xTotalRegionSize, xTotalHeapSize = 0;
\r
398 BaseType_t xDefinedRegions = 0;
\r
400 const HeapRegion_t *pxHeapRegion;
\r
402 /* Can only call once! */
\r
403 configASSERT( pxEnd == NULL );
\r
405 pxHeapRegion = &( pxHeapRegions[ xDefinedRegions ] );
\r
407 while( pxHeapRegion->xSizeInBytes > 0 )
\r
409 xTotalRegionSize = pxHeapRegion->xSizeInBytes;
\r
411 /* Ensure the heap region starts on a correctly aligned boundary. */
\r
412 xAddress = ( size_t ) pxHeapRegion->pucStartAddress;
\r
413 if( ( xAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
\r
415 xAddress += ( portBYTE_ALIGNMENT - 1 );
\r
416 xAddress &= ~portBYTE_ALIGNMENT_MASK;
\r
418 /* Adjust the size for the bytes lost to alignment. */
\r
419 xTotalRegionSize -= xAddress - ( size_t ) pxHeapRegion->pucStartAddress;
\r
422 xAlignedHeap = xAddress;
\r
424 /* Set xStart if it has not already been set. */
\r
425 if( xDefinedRegions == 0 )
\r
427 /* xStart is used to hold a pointer to the first item in the list of
\r
428 free blocks. The void cast is used to prevent compiler warnings. */
\r
429 xStart.pxNextFreeBlock = ( BlockLink_t * ) xAlignedHeap;
\r
430 xStart.xBlockSize = ( size_t ) 0;
\r
434 /* Should only get here if one region has already been added to the
\r
436 configASSERT( pxEnd != NULL );
\r
438 /* Check blocks are passed in with increasing start addresses. */
\r
439 configASSERT( xAddress > ( size_t ) pxEnd );
\r
442 /* Remember the location of the end marker in the previous region, if
\r
444 pxPreviousFreeBlock = pxEnd;
\r
446 /* pxEnd is used to mark the end of the list of free blocks and is
\r
447 inserted at the end of the region space. */
\r
448 xAddress = xAlignedHeap + xTotalRegionSize;
\r
449 xAddress -= xHeapStructSize;
\r
450 xAddress &= ~portBYTE_ALIGNMENT_MASK;
\r
451 pxEnd = ( BlockLink_t * ) xAddress;
\r
452 pxEnd->xBlockSize = 0;
\r
453 pxEnd->pxNextFreeBlock = NULL;
\r
455 /* To start with there is a single free block in this region that is
\r
456 sized to take up the entire heap region minus the space taken by the
\r
457 free block structure. */
\r
458 pxFirstFreeBlockInRegion = ( BlockLink_t * ) xAlignedHeap;
\r
459 pxFirstFreeBlockInRegion->xBlockSize = xAddress - ( size_t ) pxFirstFreeBlockInRegion;
\r
460 pxFirstFreeBlockInRegion->pxNextFreeBlock = pxEnd;
\r
462 /* If this is not the first region that makes up the entire heap space
\r
463 then link the previous region to this region. */
\r
464 if( pxPreviousFreeBlock != NULL )
\r
466 pxPreviousFreeBlock->pxNextFreeBlock = pxFirstFreeBlockInRegion;
\r
469 xTotalHeapSize += pxFirstFreeBlockInRegion->xBlockSize;
\r
471 /* Move onto the next HeapRegion_t structure. */
\r
473 pxHeapRegion = &( pxHeapRegions[ xDefinedRegions ] );
\r
476 xMinimumEverFreeBytesRemaining = xTotalHeapSize;
\r
477 xFreeBytesRemaining = xTotalHeapSize;
\r
479 /* Check something was actually defined before it is accessed. */
\r
480 configASSERT( xTotalHeapSize );
\r
482 /* Work out the position of the top bit in a size_t variable. */
\r
483 xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );
\r