2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 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() and vPortFree() that combines
\r
30 * (coalescences) adjacent memory blocks as they are freed, and in so doing
\r
31 * limits memory fragmentation.
\r
33 * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the
\r
34 * memory management pages of http://www.FreeRTOS.org for more information.
\r
38 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
39 all the API functions to use the MPU wrappers. That should only be done when
\r
40 task.h is included from an application file. */
\r
41 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
43 #include "FreeRTOS.h"
\r
46 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
48 #if( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
\r
49 #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
\r
52 /* Block sizes must not get too small. */
\r
53 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize << 1 ) )
\r
55 /* Assumes 8bit bytes! */
\r
56 #define heapBITS_PER_BYTE ( ( size_t ) 8 )
\r
58 /* Allocate the memory for the heap. */
\r
59 #if( configAPPLICATION_ALLOCATED_HEAP == 1 )
\r
60 /* The application writer has already defined the array used for the RTOS
\r
61 heap - probably so it can be placed in a special segment or address. */
\r
62 extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
64 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
65 #endif /* configAPPLICATION_ALLOCATED_HEAP */
\r
67 /* Define the linked list structure. This is used to link free blocks in order
\r
68 of their memory address. */
\r
69 typedef struct A_BLOCK_LINK
\r
71 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
\r
72 size_t xBlockSize; /*<< The size of the free block. */
\r
75 /*-----------------------------------------------------------*/
\r
78 * Inserts a block of memory that is being freed into the correct position in
\r
79 * the list of free memory blocks. The block being freed will be merged with
\r
80 * the block in front it and/or the block behind it if the memory blocks are
\r
81 * adjacent to each other.
\r
83 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert );
\r
86 * Called automatically to setup the required heap structures the first time
\r
87 * pvPortMalloc() is called.
\r
89 static void prvHeapInit( void );
\r
91 /*-----------------------------------------------------------*/
\r
93 /* The size of the structure placed at the beginning of each allocated memory
\r
94 block must by correctly byte aligned. */
\r
95 static const size_t xHeapStructSize = ( sizeof( BlockLink_t ) + ( ( size_t ) ( portBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
\r
97 /* Create a couple of list links to mark the start and end of the list. */
\r
98 static BlockLink_t xStart, *pxEnd = NULL;
\r
100 /* Keeps track of the number of calls to allocate and free memory as well as the
\r
101 number of free bytes remaining, but says nothing about fragmentation. */
\r
102 static size_t xFreeBytesRemaining = 0U;
\r
103 static size_t xMinimumEverFreeBytesRemaining = 0U;
\r
104 static size_t xNumberOfSuccessfulAllocations = 0;
\r
105 static size_t xNumberOfSuccessfulFrees = 0;
\r
107 /* Gets set to the top bit of an size_t type. When this bit in the xBlockSize
\r
108 member of an BlockLink_t structure is set then the block belongs to the
\r
109 application. When the bit is free the block is still part of the free heap
\r
111 static size_t xBlockAllocatedBit = 0;
\r
113 /*-----------------------------------------------------------*/
\r
115 void *pvPortMalloc( size_t xWantedSize )
\r
117 BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
\r
118 void *pvReturn = NULL;
\r
122 /* If this is the first call to malloc then the heap will require
\r
123 initialisation to setup the list of free blocks. */
\r
124 if( pxEnd == NULL )
\r
130 mtCOVERAGE_TEST_MARKER();
\r
133 /* Check the requested block size is not so large that the top bit is
\r
134 set. The top bit of the block size member of the BlockLink_t structure
\r
135 is used to determine who owns the block - the application or the
\r
136 kernel, so it must be free. */
\r
137 if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
\r
139 /* The wanted size is increased so it can contain a BlockLink_t
\r
140 structure in addition to the requested amount of bytes. */
\r
141 if( xWantedSize > 0 )
\r
143 xWantedSize += xHeapStructSize;
\r
145 /* Ensure that blocks are always aligned to the required number
\r
147 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
\r
149 /* Byte alignment required. */
\r
150 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
\r
151 configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
\r
155 mtCOVERAGE_TEST_MARKER();
\r
160 mtCOVERAGE_TEST_MARKER();
\r
163 if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
\r
165 /* Traverse the list from the start (lowest address) block until
\r
166 one of adequate size is found. */
\r
167 pxPreviousBlock = &xStart;
\r
168 pxBlock = xStart.pxNextFreeBlock;
\r
169 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
\r
171 pxPreviousBlock = pxBlock;
\r
172 pxBlock = pxBlock->pxNextFreeBlock;
\r
175 /* If the end marker was reached then a block of adequate size
\r
177 if( pxBlock != pxEnd )
\r
179 /* Return the memory space pointed to - jumping over the
\r
180 BlockLink_t structure at its start. */
\r
181 pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );
\r
183 /* This block is being returned for use so must be taken out
\r
184 of the list of free blocks. */
\r
185 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
\r
187 /* If the block is larger than required it can be split into
\r
189 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
\r
191 /* This block is to be split into two. Create a new
\r
192 block following the number of bytes requested. The void
\r
193 cast is used to prevent byte alignment warnings from the
\r
195 pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
\r
196 configASSERT( ( ( ( size_t ) pxNewBlockLink ) & portBYTE_ALIGNMENT_MASK ) == 0 );
\r
198 /* Calculate the sizes of two blocks split from the
\r
200 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
\r
201 pxBlock->xBlockSize = xWantedSize;
\r
203 /* Insert the new block into the list of free blocks. */
\r
204 prvInsertBlockIntoFreeList( pxNewBlockLink );
\r
208 mtCOVERAGE_TEST_MARKER();
\r
211 xFreeBytesRemaining -= pxBlock->xBlockSize;
\r
213 if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
\r
215 xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
\r
219 mtCOVERAGE_TEST_MARKER();
\r
222 /* The block is being returned - it is allocated and owned
\r
223 by the application and has no "next" block. */
\r
224 pxBlock->xBlockSize |= xBlockAllocatedBit;
\r
225 pxBlock->pxNextFreeBlock = NULL;
\r
226 xNumberOfSuccessfulAllocations++;
\r
230 mtCOVERAGE_TEST_MARKER();
\r
235 mtCOVERAGE_TEST_MARKER();
\r
240 mtCOVERAGE_TEST_MARKER();
\r
243 traceMALLOC( pvReturn, xWantedSize );
\r
245 ( void ) xTaskResumeAll();
\r
247 #if( configUSE_MALLOC_FAILED_HOOK == 1 )
\r
249 if( pvReturn == NULL )
\r
251 extern void vApplicationMallocFailedHook( void );
\r
252 vApplicationMallocFailedHook();
\r
256 mtCOVERAGE_TEST_MARKER();
\r
261 configASSERT( ( ( ( size_t ) pvReturn ) & ( size_t ) portBYTE_ALIGNMENT_MASK ) == 0 );
\r
264 /*-----------------------------------------------------------*/
\r
266 void vPortFree( void *pv )
\r
268 uint8_t *puc = ( uint8_t * ) pv;
\r
269 BlockLink_t *pxLink;
\r
273 /* The memory being freed will have an BlockLink_t structure immediately
\r
275 puc -= xHeapStructSize;
\r
277 /* This casting is to keep the compiler from issuing warnings. */
\r
278 pxLink = ( void * ) puc;
\r
280 /* Check the block is actually allocated. */
\r
281 configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
\r
282 configASSERT( pxLink->pxNextFreeBlock == NULL );
\r
284 if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )
\r
286 if( pxLink->pxNextFreeBlock == NULL )
\r
288 /* The block is being returned to the heap - it is no longer
\r
290 pxLink->xBlockSize &= ~xBlockAllocatedBit;
\r
294 /* Add this block to the list of free blocks. */
\r
295 xFreeBytesRemaining += pxLink->xBlockSize;
\r
296 traceFREE( pv, pxLink->xBlockSize );
\r
297 prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
\r
298 xNumberOfSuccessfulFrees++;
\r
300 ( void ) xTaskResumeAll();
\r
304 mtCOVERAGE_TEST_MARKER();
\r
309 mtCOVERAGE_TEST_MARKER();
\r
313 /*-----------------------------------------------------------*/
\r
315 size_t xPortGetFreeHeapSize( void )
\r
317 return xFreeBytesRemaining;
\r
319 /*-----------------------------------------------------------*/
\r
321 size_t xPortGetMinimumEverFreeHeapSize( void )
\r
323 return xMinimumEverFreeBytesRemaining;
\r
325 /*-----------------------------------------------------------*/
\r
327 void vPortInitialiseBlocks( void )
\r
329 /* This just exists to keep the linker quiet. */
\r
331 /*-----------------------------------------------------------*/
\r
333 static void prvHeapInit( void )
\r
335 BlockLink_t *pxFirstFreeBlock;
\r
336 uint8_t *pucAlignedHeap;
\r
338 size_t xTotalHeapSize = configTOTAL_HEAP_SIZE;
\r
340 /* Ensure the heap starts on a correctly aligned boundary. */
\r
341 uxAddress = ( size_t ) ucHeap;
\r
343 if( ( uxAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
\r
345 uxAddress += ( portBYTE_ALIGNMENT - 1 );
\r
346 uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
\r
347 xTotalHeapSize -= uxAddress - ( size_t ) ucHeap;
\r
350 pucAlignedHeap = ( uint8_t * ) uxAddress;
\r
352 /* xStart is used to hold a pointer to the first item in the list of free
\r
353 blocks. The void cast is used to prevent compiler warnings. */
\r
354 xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
\r
355 xStart.xBlockSize = ( size_t ) 0;
\r
357 /* pxEnd is used to mark the end of the list of free blocks and is inserted
\r
358 at the end of the heap space. */
\r
359 uxAddress = ( ( size_t ) pucAlignedHeap ) + xTotalHeapSize;
\r
360 uxAddress -= xHeapStructSize;
\r
361 uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
\r
362 pxEnd = ( void * ) uxAddress;
\r
363 pxEnd->xBlockSize = 0;
\r
364 pxEnd->pxNextFreeBlock = NULL;
\r
366 /* To start with there is a single free block that is sized to take up the
\r
367 entire heap space, minus the space taken by pxEnd. */
\r
368 pxFirstFreeBlock = ( void * ) pucAlignedHeap;
\r
369 pxFirstFreeBlock->xBlockSize = uxAddress - ( size_t ) pxFirstFreeBlock;
\r
370 pxFirstFreeBlock->pxNextFreeBlock = pxEnd;
\r
372 /* Only one block exists - and it covers the entire usable heap space. */
\r
373 xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
\r
374 xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
\r
376 /* Work out the position of the top bit in a size_t variable. */
\r
377 xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );
\r
379 /*-----------------------------------------------------------*/
\r
381 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )
\r
383 BlockLink_t *pxIterator;
\r
386 /* Iterate through the list until a block is found that has a higher address
\r
387 than the block being inserted. */
\r
388 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
\r
390 /* Nothing to do here, just iterate to the right position. */
\r
393 /* Do the block being inserted, and the block it is being inserted after
\r
394 make a contiguous block of memory? */
\r
395 puc = ( uint8_t * ) pxIterator;
\r
396 if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )
\r
398 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
\r
399 pxBlockToInsert = pxIterator;
\r
403 mtCOVERAGE_TEST_MARKER();
\r
406 /* Do the block being inserted, and the block it is being inserted before
\r
407 make a contiguous block of memory? */
\r
408 puc = ( uint8_t * ) pxBlockToInsert;
\r
409 if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )
\r
411 if( pxIterator->pxNextFreeBlock != pxEnd )
\r
413 /* Form one big block from the two blocks. */
\r
414 pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
\r
415 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
\r
419 pxBlockToInsert->pxNextFreeBlock = pxEnd;
\r
424 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
\r
427 /* If the block being inserted plugged a gab, so was merged with the block
\r
428 before and the block after, then it's pxNextFreeBlock pointer will have
\r
429 already been set, and should not be set here as that would make it point
\r
431 if( pxIterator != pxBlockToInsert )
\r
433 pxIterator->pxNextFreeBlock = pxBlockToInsert;
\r
437 mtCOVERAGE_TEST_MARKER();
\r
440 /*-----------------------------------------------------------*/
\r
442 void vPortGetHeapStats( HeapStats_t *pxHeapStats )
\r
444 BlockLink_t *pxBlock;
\r
445 size_t xBlocks = 0, xMaxSize = 0, xMinSize = portMAX_DELAY; /* portMAX_DELAY used as a portable way of getting the maximum value. */
\r
449 pxBlock = xStart.pxNextFreeBlock;
\r
451 /* pxBlock will be NULL if the heap has not been initialised. The heap
\r
452 is initialised automatically when the first allocation is made. */
\r
453 if( pxBlock != NULL )
\r
457 /* Increment the number of blocks and record the largest block seen
\r
461 if( pxBlock->xBlockSize > xMaxSize )
\r
463 xMaxSize = pxBlock->xBlockSize;
\r
466 if( pxBlock->xBlockSize < xMinSize )
\r
468 xMinSize = pxBlock->xBlockSize;
\r
471 /* Move to the next block in the chain until the last block is
\r
473 pxBlock = pxBlock->pxNextFreeBlock;
\r
474 } while( pxBlock != pxEnd );
\r
479 pxHeapStats->xSizeOfLargestFreeBlockInBytes = xMaxSize;
\r
480 pxHeapStats->xSizeOfSmallestFreeBlockInBytes = xMinSize;
\r
481 pxHeapStats->xNumberOfFreeBlocks = xBlocks;
\r
483 taskENTER_CRITICAL();
\r
485 pxHeapStats->xAvailableHeapSpaceInBytes = xFreeBytesRemaining;
\r
486 pxHeapStats->xNumberOfSuccessfulAllocations = xNumberOfSuccessfulAllocations;
\r
487 pxHeapStats->xNumberOfSuccessfulFrees = xNumberOfSuccessfulFrees;
\r
488 pxHeapStats->xMinimumEverFreeBytesRemaining = xMinimumEverFreeBytesRemaining;
\r
490 taskEXIT_CRITICAL();
\r