2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * A sample implementation of pvPortMalloc() and vPortFree() that combines
\r
31 * (coalescences) adjacent memory blocks as they are freed, and in so doing
\r
32 * limits memory fragmentation.
\r
34 * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the
\r
35 * memory management pages of http://www.FreeRTOS.org for more information.
\r
39 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
40 all the API functions to use the MPU wrappers. That should only be done when
\r
41 task.h is included from an application file. */
\r
42 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
44 #include "FreeRTOS.h"
\r
47 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
49 #if( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
\r
50 #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
\r
53 /* Block sizes must not get too small. */
\r
54 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize << 1 ) )
\r
56 /* Assumes 8bit bytes! */
\r
57 #define heapBITS_PER_BYTE ( ( size_t ) 8 )
\r
59 /* Allocate the memory for the heap. */
\r
60 #if( configAPPLICATION_ALLOCATED_HEAP == 1 )
\r
61 /* The application writer has already defined the array used for the RTOS
\r
62 heap - probably so it can be placed in a special segment or address. */
\r
63 extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
65 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
66 #endif /* configAPPLICATION_ALLOCATED_HEAP */
\r
68 /* Define the linked list structure. This is used to link free blocks in order
\r
69 of their memory address. */
\r
70 typedef struct A_BLOCK_LINK
\r
72 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
\r
73 size_t xBlockSize; /*<< The size of the free block. */
\r
76 /*-----------------------------------------------------------*/
\r
79 * Inserts a block of memory that is being freed into the correct position in
\r
80 * the list of free memory blocks. The block being freed will be merged with
\r
81 * the block in front it and/or the block behind it if the memory blocks are
\r
82 * adjacent to each other.
\r
84 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert );
\r
87 * Called automatically to setup the required heap structures the first time
\r
88 * pvPortMalloc() is called.
\r
90 static void prvHeapInit( void );
\r
92 /*-----------------------------------------------------------*/
\r
94 /* The size of the structure placed at the beginning of each allocated memory
\r
95 block must by correctly byte aligned. */
\r
96 static const size_t xHeapStructSize = ( sizeof( BlockLink_t ) + ( ( size_t ) ( portBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
\r
98 /* Create a couple of list links to mark the start and end of the list. */
\r
99 static BlockLink_t xStart, *pxEnd = NULL;
\r
101 /* Keeps track of the number of free bytes remaining, but says nothing about
\r
103 static size_t xFreeBytesRemaining = 0U;
\r
104 static size_t xMinimumEverFreeBytesRemaining = 0U;
\r
106 /* Gets set to the top bit of an size_t type. When this bit in the xBlockSize
\r
107 member of an BlockLink_t structure is set then the block belongs to the
\r
108 application. When the bit is free the block is still part of the free heap
\r
110 static size_t xBlockAllocatedBit = 0;
\r
112 /*-----------------------------------------------------------*/
\r
114 void *pvPortMalloc( size_t xWantedSize )
\r
116 BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
\r
117 void *pvReturn = NULL;
\r
121 /* If this is the first call to malloc then the heap will require
\r
122 initialisation to setup the list of free blocks. */
\r
123 if( pxEnd == NULL )
\r
129 mtCOVERAGE_TEST_MARKER();
\r
132 /* Check the requested block size is not so large that the top bit is
\r
133 set. The top bit of the block size member of the BlockLink_t structure
\r
134 is used to determine who owns the block - the application or the
\r
135 kernel, so it must be free. */
\r
136 if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
\r
138 /* The wanted size is increased so it can contain a BlockLink_t
\r
139 structure in addition to the requested amount of bytes. */
\r
140 if( xWantedSize > 0 )
\r
142 xWantedSize += xHeapStructSize;
\r
144 /* Ensure that blocks are always aligned to the required number
\r
146 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
\r
148 /* Byte alignment required. */
\r
149 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
\r
150 configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
\r
154 mtCOVERAGE_TEST_MARKER();
\r
159 mtCOVERAGE_TEST_MARKER();
\r
162 if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
\r
164 /* Traverse the list from the start (lowest address) block until
\r
165 one of adequate size is found. */
\r
166 pxPreviousBlock = &xStart;
\r
167 pxBlock = xStart.pxNextFreeBlock;
\r
168 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
\r
170 pxPreviousBlock = pxBlock;
\r
171 pxBlock = pxBlock->pxNextFreeBlock;
\r
174 /* If the end marker was reached then a block of adequate size
\r
176 if( pxBlock != pxEnd )
\r
178 /* Return the memory space pointed to - jumping over the
\r
179 BlockLink_t structure at its start. */
\r
180 pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );
\r
182 /* This block is being returned for use so must be taken out
\r
183 of the list of free blocks. */
\r
184 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
\r
186 /* If the block is larger than required it can be split into
\r
188 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
\r
190 /* This block is to be split into two. Create a new
\r
191 block following the number of bytes requested. The void
\r
192 cast is used to prevent byte alignment warnings from the
\r
194 pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
\r
195 configASSERT( ( ( ( size_t ) pxNewBlockLink ) & portBYTE_ALIGNMENT_MASK ) == 0 );
\r
197 /* Calculate the sizes of two blocks split from the
\r
199 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
\r
200 pxBlock->xBlockSize = xWantedSize;
\r
202 /* Insert the new block into the list of free blocks. */
\r
203 prvInsertBlockIntoFreeList( pxNewBlockLink );
\r
207 mtCOVERAGE_TEST_MARKER();
\r
210 xFreeBytesRemaining -= pxBlock->xBlockSize;
\r
212 if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
\r
214 xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
\r
218 mtCOVERAGE_TEST_MARKER();
\r
221 /* The block is being returned - it is allocated and owned
\r
222 by the application and has no "next" block. */
\r
223 pxBlock->xBlockSize |= xBlockAllocatedBit;
\r
224 pxBlock->pxNextFreeBlock = NULL;
\r
228 mtCOVERAGE_TEST_MARKER();
\r
233 mtCOVERAGE_TEST_MARKER();
\r
238 mtCOVERAGE_TEST_MARKER();
\r
241 traceMALLOC( pvReturn, xWantedSize );
\r
243 ( void ) xTaskResumeAll();
\r
245 #if( configUSE_MALLOC_FAILED_HOOK == 1 )
\r
247 if( pvReturn == NULL )
\r
249 extern void vApplicationMallocFailedHook( void );
\r
250 vApplicationMallocFailedHook();
\r
254 mtCOVERAGE_TEST_MARKER();
\r
259 configASSERT( ( ( ( size_t ) pvReturn ) & ( size_t ) portBYTE_ALIGNMENT_MASK ) == 0 );
\r
262 /*-----------------------------------------------------------*/
\r
264 void vPortFree( void *pv )
\r
266 uint8_t *puc = ( uint8_t * ) pv;
\r
267 BlockLink_t *pxLink;
\r
271 /* The memory being freed will have an BlockLink_t structure immediately
\r
273 puc -= xHeapStructSize;
\r
275 /* This casting is to keep the compiler from issuing warnings. */
\r
276 pxLink = ( void * ) puc;
\r
278 /* Check the block is actually allocated. */
\r
279 configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
\r
280 configASSERT( pxLink->pxNextFreeBlock == NULL );
\r
282 if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )
\r
284 if( pxLink->pxNextFreeBlock == NULL )
\r
286 /* The block is being returned to the heap - it is no longer
\r
288 pxLink->xBlockSize &= ~xBlockAllocatedBit;
\r
292 /* Add this block to the list of free blocks. */
\r
293 xFreeBytesRemaining += pxLink->xBlockSize;
\r
294 traceFREE( pv, pxLink->xBlockSize );
\r
295 prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
\r
297 ( void ) xTaskResumeAll();
\r
301 mtCOVERAGE_TEST_MARKER();
\r
306 mtCOVERAGE_TEST_MARKER();
\r
310 /*-----------------------------------------------------------*/
\r
312 size_t xPortGetFreeHeapSize( void )
\r
314 return xFreeBytesRemaining;
\r
316 /*-----------------------------------------------------------*/
\r
318 size_t xPortGetMinimumEverFreeHeapSize( void )
\r
320 return xMinimumEverFreeBytesRemaining;
\r
322 /*-----------------------------------------------------------*/
\r
324 void vPortInitialiseBlocks( void )
\r
326 /* This just exists to keep the linker quiet. */
\r
328 /*-----------------------------------------------------------*/
\r
330 static void prvHeapInit( void )
\r
332 BlockLink_t *pxFirstFreeBlock;
\r
333 uint8_t *pucAlignedHeap;
\r
335 size_t xTotalHeapSize = configTOTAL_HEAP_SIZE;
\r
337 /* Ensure the heap starts on a correctly aligned boundary. */
\r
338 uxAddress = ( size_t ) ucHeap;
\r
340 if( ( uxAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
\r
342 uxAddress += ( portBYTE_ALIGNMENT - 1 );
\r
343 uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
\r
344 xTotalHeapSize -= uxAddress - ( size_t ) ucHeap;
\r
347 pucAlignedHeap = ( uint8_t * ) uxAddress;
\r
349 /* xStart is used to hold a pointer to the first item in the list of free
\r
350 blocks. The void cast is used to prevent compiler warnings. */
\r
351 xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
\r
352 xStart.xBlockSize = ( size_t ) 0;
\r
354 /* pxEnd is used to mark the end of the list of free blocks and is inserted
\r
355 at the end of the heap space. */
\r
356 uxAddress = ( ( size_t ) pucAlignedHeap ) + xTotalHeapSize;
\r
357 uxAddress -= xHeapStructSize;
\r
358 uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
\r
359 pxEnd = ( void * ) uxAddress;
\r
360 pxEnd->xBlockSize = 0;
\r
361 pxEnd->pxNextFreeBlock = NULL;
\r
363 /* To start with there is a single free block that is sized to take up the
\r
364 entire heap space, minus the space taken by pxEnd. */
\r
365 pxFirstFreeBlock = ( void * ) pucAlignedHeap;
\r
366 pxFirstFreeBlock->xBlockSize = uxAddress - ( size_t ) pxFirstFreeBlock;
\r
367 pxFirstFreeBlock->pxNextFreeBlock = pxEnd;
\r
369 /* Only one block exists - and it covers the entire usable heap space. */
\r
370 xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
\r
371 xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
\r
373 /* Work out the position of the top bit in a size_t variable. */
\r
374 xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );
\r
376 /*-----------------------------------------------------------*/
\r
378 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )
\r
380 BlockLink_t *pxIterator;
\r
383 /* Iterate through the list until a block is found that has a higher address
\r
384 than the block being inserted. */
\r
385 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
\r
387 /* Nothing to do here, just iterate to the right position. */
\r
390 /* Do the block being inserted, and the block it is being inserted after
\r
391 make a contiguous block of memory? */
\r
392 puc = ( uint8_t * ) pxIterator;
\r
393 if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )
\r
395 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
\r
396 pxBlockToInsert = pxIterator;
\r
400 mtCOVERAGE_TEST_MARKER();
\r
403 /* Do the block being inserted, and the block it is being inserted before
\r
404 make a contiguous block of memory? */
\r
405 puc = ( uint8_t * ) pxBlockToInsert;
\r
406 if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )
\r
408 if( pxIterator->pxNextFreeBlock != pxEnd )
\r
410 /* Form one big block from the two blocks. */
\r
411 pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
\r
412 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
\r
416 pxBlockToInsert->pxNextFreeBlock = pxEnd;
\r
421 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
\r
424 /* If the block being inserted plugged a gab, so was merged with the block
\r
425 before and the block after, then it's pxNextFreeBlock pointer will have
\r
426 already been set, and should not be set here as that would make it point
\r
428 if( pxIterator != pxBlockToInsert )
\r
430 pxIterator->pxNextFreeBlock = pxBlockToInsert;
\r
434 mtCOVERAGE_TEST_MARKER();
\r