2 FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
71 * A sample implementation of pvPortMalloc() that allows the heap to be defined
\r
72 * across multiple non-contigous blocks and combines (coalescences) adjacent
\r
73 * memory blocks as they are freed.
\r
75 * See heap_1.c, heap_2.c, heap_3.c and heap_4.c for alternative
\r
76 * implementations, and the memory management pages of http://www.FreeRTOS.org
\r
77 * for more information.
\r
81 * vPortDefineHeapRegions() ***must*** be called before pvPortMalloc().
\r
82 * pvPortMalloc() will be called if any task objects (tasks, queues, event
\r
83 * groups, etc.) are created, therefore vPortDefineHeapRegions() ***must*** be
\r
84 * called before any other objects are defined.
\r
86 * vPortDefineHeapRegions() takes a single parameter. The parameter is an array
\r
87 * of HeapRegion_t structures. HeapRegion_t is defined in portable.h as
\r
89 * typedef struct HeapRegion
\r
91 * uint8_t *pucStartAddress; << Start address of a block of memory that will be part of the heap.
\r
92 * size_t xSizeInBytes; << Size of the block of memory.
\r
95 * The array is terminated using a NULL zero sized region definition, and the
\r
96 * memory regions defined in the array ***must*** appear in address order from
\r
97 * low address to high address. So the following is a valid example of how
\r
98 * to use the function.
\r
100 * HeapRegion_t xHeapRegions[] =
\r
102 * { ( uint8_t * ) 0x80000000UL, 0x10000 }, << Defines a block of 0x10000 bytes starting at address 0x80000000
\r
103 * { ( uint8_t * ) 0x90000000UL, 0xa0000 }, << Defines a block of 0xa0000 bytes starting at address of 0x90000000
\r
104 * { NULL, 0 } << Terminates the array.
\r
107 * vPortDefineHeapRegions( xHeapRegions ); << Pass the array into vPortDefineHeapRegions().
\r
109 * Note 0x80000000 is the lower address so appears in the array first.
\r
112 #include <stdlib.h>
\r
114 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
115 all the API functions to use the MPU wrappers. That should only be done when
\r
116 task.h is included from an application file. */
\r
117 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
119 #include "FreeRTOS.h"
\r
122 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
124 #if( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
\r
125 #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
\r
128 /* Block sizes must not get too small. */
\r
129 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize << 1 ) )
\r
131 /* Assumes 8bit bytes! */
\r
132 #define heapBITS_PER_BYTE ( ( size_t ) 8 )
\r
134 /* Define the linked list structure. This is used to link free blocks in order
\r
135 of their memory address. */
\r
136 typedef struct A_BLOCK_LINK
\r
138 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
\r
139 size_t xBlockSize; /*<< The size of the free block. */
\r
142 /*-----------------------------------------------------------*/
\r
145 * Inserts a block of memory that is being freed into the correct position in
\r
146 * the list of free memory blocks. The block being freed will be merged with
\r
147 * the block in front it and/or the block behind it if the memory blocks are
\r
148 * adjacent to each other.
\r
150 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert );
\r
152 /*-----------------------------------------------------------*/
\r
154 /* The size of the structure placed at the beginning of each allocated memory
\r
155 block must by correctly byte aligned. */
\r
156 static const size_t xHeapStructSize = ( sizeof( BlockLink_t ) + ( ( size_t ) ( portBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
\r
158 /* Create a couple of list links to mark the start and end of the list. */
\r
159 static BlockLink_t xStart, *pxEnd = NULL;
\r
161 /* Keeps track of the number of free bytes remaining, but says nothing about
\r
163 static size_t xFreeBytesRemaining = 0U;
\r
164 static size_t xMinimumEverFreeBytesRemaining = 0U;
\r
166 /* Gets set to the top bit of an size_t type. When this bit in the xBlockSize
\r
167 member of an BlockLink_t structure is set then the block belongs to the
\r
168 application. When the bit is free the block is still part of the free heap
\r
170 static size_t xBlockAllocatedBit = 0;
\r
172 /*-----------------------------------------------------------*/
\r
174 void *pvPortMalloc( size_t xWantedSize )
\r
176 BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
\r
177 void *pvReturn = NULL;
\r
179 /* The heap must be initialised before the first call to
\r
180 prvPortMalloc(). */
\r
181 configASSERT( pxEnd );
\r
185 /* Check the requested block size is not so large that the top bit is
\r
186 set. The top bit of the block size member of the BlockLink_t structure
\r
187 is used to determine who owns the block - the application or the
\r
188 kernel, so it must be free. */
\r
189 if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
\r
191 /* The wanted size is increased so it can contain a BlockLink_t
\r
192 structure in addition to the requested amount of bytes. */
\r
193 if( xWantedSize > 0 )
\r
195 xWantedSize += xHeapStructSize;
\r
197 /* Ensure that blocks are always aligned to the required number
\r
199 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
\r
201 /* Byte alignment required. */
\r
202 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
\r
206 mtCOVERAGE_TEST_MARKER();
\r
211 mtCOVERAGE_TEST_MARKER();
\r
214 if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
\r
216 /* Traverse the list from the start (lowest address) block until
\r
217 one of adequate size is found. */
\r
218 pxPreviousBlock = &xStart;
\r
219 pxBlock = xStart.pxNextFreeBlock;
\r
220 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
\r
222 pxPreviousBlock = pxBlock;
\r
223 pxBlock = pxBlock->pxNextFreeBlock;
\r
226 /* If the end marker was reached then a block of adequate size
\r
228 if( pxBlock != pxEnd )
\r
230 /* Return the memory space pointed to - jumping over the
\r
231 BlockLink_t structure at its start. */
\r
232 pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );
\r
234 /* This block is being returned for use so must be taken out
\r
235 of the list of free blocks. */
\r
236 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
\r
238 /* If the block is larger than required it can be split into
\r
240 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
\r
242 /* This block is to be split into two. Create a new
\r
243 block following the number of bytes requested. The void
\r
244 cast is used to prevent byte alignment warnings from the
\r
246 pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
\r
248 /* Calculate the sizes of two blocks split from the
\r
250 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
\r
251 pxBlock->xBlockSize = xWantedSize;
\r
253 /* Insert the new block into the list of free blocks. */
\r
254 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
\r
258 mtCOVERAGE_TEST_MARKER();
\r
261 xFreeBytesRemaining -= pxBlock->xBlockSize;
\r
263 if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
\r
265 xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
\r
269 mtCOVERAGE_TEST_MARKER();
\r
272 /* The block is being returned - it is allocated and owned
\r
273 by the application and has no "next" block. */
\r
274 pxBlock->xBlockSize |= xBlockAllocatedBit;
\r
275 pxBlock->pxNextFreeBlock = NULL;
\r
279 mtCOVERAGE_TEST_MARKER();
\r
284 mtCOVERAGE_TEST_MARKER();
\r
289 mtCOVERAGE_TEST_MARKER();
\r
292 traceMALLOC( pvReturn, xWantedSize );
\r
294 ( void ) xTaskResumeAll();
\r
296 #if( configUSE_MALLOC_FAILED_HOOK == 1 )
\r
298 if( pvReturn == NULL )
\r
300 extern void vApplicationMallocFailedHook( void );
\r
301 vApplicationMallocFailedHook();
\r
305 mtCOVERAGE_TEST_MARKER();
\r
312 /*-----------------------------------------------------------*/
\r
314 void vPortFree( void *pv )
\r
316 uint8_t *puc = ( uint8_t * ) pv;
\r
317 BlockLink_t *pxLink;
\r
321 /* The memory being freed will have an BlockLink_t structure immediately
\r
323 puc -= xHeapStructSize;
\r
325 /* This casting is to keep the compiler from issuing warnings. */
\r
326 pxLink = ( void * ) puc;
\r
328 /* Check the block is actually allocated. */
\r
329 configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
\r
330 configASSERT( pxLink->pxNextFreeBlock == NULL );
\r
332 if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )
\r
334 if( pxLink->pxNextFreeBlock == NULL )
\r
336 /* The block is being returned to the heap - it is no longer
\r
338 pxLink->xBlockSize &= ~xBlockAllocatedBit;
\r
342 /* Add this block to the list of free blocks. */
\r
343 xFreeBytesRemaining += pxLink->xBlockSize;
\r
344 traceFREE( pv, pxLink->xBlockSize );
\r
345 prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
\r
347 ( void ) xTaskResumeAll();
\r
351 mtCOVERAGE_TEST_MARKER();
\r
356 mtCOVERAGE_TEST_MARKER();
\r
360 /*-----------------------------------------------------------*/
\r
362 size_t xPortGetFreeHeapSize( void )
\r
364 return xFreeBytesRemaining;
\r
366 /*-----------------------------------------------------------*/
\r
368 size_t xPortGetMinimumEverFreeHeapSize( void )
\r
370 return xMinimumEverFreeBytesRemaining;
\r
372 /*-----------------------------------------------------------*/
\r
374 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )
\r
376 BlockLink_t *pxIterator;
\r
379 /* Iterate through the list until a block is found that has a higher address
\r
380 than the block being inserted. */
\r
381 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
\r
383 /* Nothing to do here, just iterate to the right position. */
\r
386 /* Do the block being inserted, and the block it is being inserted after
\r
387 make a contiguous block of memory? */
\r
388 puc = ( uint8_t * ) pxIterator;
\r
389 if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )
\r
391 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
\r
392 pxBlockToInsert = pxIterator;
\r
396 mtCOVERAGE_TEST_MARKER();
\r
399 /* Do the block being inserted, and the block it is being inserted before
\r
400 make a contiguous block of memory? */
\r
401 puc = ( uint8_t * ) pxBlockToInsert;
\r
402 if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )
\r
404 if( pxIterator->pxNextFreeBlock != pxEnd )
\r
406 /* Form one big block from the two blocks. */
\r
407 pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
\r
408 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
\r
412 pxBlockToInsert->pxNextFreeBlock = pxEnd;
\r
417 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
\r
420 /* If the block being inserted plugged a gab, so was merged with the block
\r
421 before and the block after, then it's pxNextFreeBlock pointer will have
\r
422 already been set, and should not be set here as that would make it point
\r
424 if( pxIterator != pxBlockToInsert )
\r
426 pxIterator->pxNextFreeBlock = pxBlockToInsert;
\r
430 mtCOVERAGE_TEST_MARKER();
\r
433 /*-----------------------------------------------------------*/
\r
435 void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions )
\r
437 BlockLink_t *pxFirstFreeBlockInRegion = NULL, *pxPreviousFreeBlock;
\r
438 size_t xAlignedHeap;
\r
439 size_t xTotalRegionSize, xTotalHeapSize = 0;
\r
440 BaseType_t xDefinedRegions = 0;
\r
442 const HeapRegion_t *pxHeapRegion;
\r
444 /* Can only call once! */
\r
445 configASSERT( pxEnd == NULL );
\r
447 pxHeapRegion = &( pxHeapRegions[ xDefinedRegions ] );
\r
449 while( pxHeapRegion->xSizeInBytes > 0 )
\r
451 xTotalRegionSize = pxHeapRegion->xSizeInBytes;
\r
453 /* Ensure the heap region starts on a correctly aligned boundary. */
\r
454 xAddress = ( size_t ) pxHeapRegion->pucStartAddress;
\r
455 if( ( xAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
\r
457 xAddress += ( portBYTE_ALIGNMENT - 1 );
\r
458 xAddress &= ~portBYTE_ALIGNMENT_MASK;
\r
460 /* Adjust the size for the bytes lost to alignment. */
\r
461 xTotalRegionSize -= xAddress - ( size_t ) pxHeapRegion->pucStartAddress;
\r
464 xAlignedHeap = xAddress;
\r
466 /* Set xStart if it has not already been set. */
\r
467 if( xDefinedRegions == 0 )
\r
469 /* xStart is used to hold a pointer to the first item in the list of
\r
470 free blocks. The void cast is used to prevent compiler warnings. */
\r
471 xStart.pxNextFreeBlock = ( BlockLink_t * ) xAlignedHeap;
\r
472 xStart.xBlockSize = ( size_t ) 0;
\r
476 /* Should only get here if one region has already been added to the
\r
478 configASSERT( pxEnd != NULL );
\r
480 /* Check blocks are passed in with increasing start addresses. */
\r
481 configASSERT( xAddress > ( size_t ) pxEnd );
\r
484 /* Remember the location of the end marker in the previous region, if
\r
486 pxPreviousFreeBlock = pxEnd;
\r
488 /* pxEnd is used to mark the end of the list of free blocks and is
\r
489 inserted at the end of the region space. */
\r
490 xAddress = xAlignedHeap + xTotalRegionSize;
\r
491 xAddress -= xHeapStructSize;
\r
492 xAddress &= ~portBYTE_ALIGNMENT_MASK;
\r
493 pxEnd = ( BlockLink_t * ) xAddress;
\r
494 pxEnd->xBlockSize = 0;
\r
495 pxEnd->pxNextFreeBlock = NULL;
\r
497 /* To start with there is a single free block in this region that is
\r
498 sized to take up the entire heap region minus the space taken by the
\r
499 free block structure. */
\r
500 pxFirstFreeBlockInRegion = ( BlockLink_t * ) xAlignedHeap;
\r
501 pxFirstFreeBlockInRegion->xBlockSize = xAddress - ( size_t ) pxFirstFreeBlockInRegion;
\r
502 pxFirstFreeBlockInRegion->pxNextFreeBlock = pxEnd;
\r
504 /* If this is not the first region that makes up the entire heap space
\r
505 then link the previous region to this region. */
\r
506 if( pxPreviousFreeBlock != NULL )
\r
508 pxPreviousFreeBlock->pxNextFreeBlock = pxFirstFreeBlockInRegion;
\r
511 xTotalHeapSize += pxFirstFreeBlockInRegion->xBlockSize;
\r
513 /* Move onto the next HeapRegion_t structure. */
\r
515 pxHeapRegion = &( pxHeapRegions[ xDefinedRegions ] );
\r
518 xMinimumEverFreeBytesRemaining = xTotalHeapSize;
\r
519 xFreeBytesRemaining = xTotalHeapSize;
\r
521 /* Check something was actually defined before it is accessed. */
\r
522 configASSERT( xTotalHeapSize );
\r
524 /* Work out the position of the top bit in a size_t variable. */
\r
525 xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );
\r