2 FreeRTOS V9.0.0rc1 - 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 /* Block sizes must not get too small. */
\r
125 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize << 1 ) )
\r
127 /* Assumes 8bit bytes! */
\r
128 #define heapBITS_PER_BYTE ( ( size_t ) 8 )
\r
130 /* Define the linked list structure. This is used to link free blocks in order
\r
131 of their memory address. */
\r
132 typedef struct A_BLOCK_LINK
\r
134 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
\r
135 size_t xBlockSize; /*<< The size of the free block. */
\r
138 /*-----------------------------------------------------------*/
\r
141 * Inserts a block of memory that is being freed into the correct position in
\r
142 * the list of free memory blocks. The block being freed will be merged with
\r
143 * the block in front it and/or the block behind it if the memory blocks are
\r
144 * adjacent to each other.
\r
146 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert );
\r
148 /*-----------------------------------------------------------*/
\r
150 /* The size of the structure placed at the beginning of each allocated memory
\r
151 block must by correctly byte aligned. */
\r
152 static const size_t xHeapStructSize = ( sizeof( BlockLink_t ) + ( ( size_t ) ( portBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
\r
154 /* Create a couple of list links to mark the start and end of the list. */
\r
155 static BlockLink_t xStart, *pxEnd = NULL;
\r
157 /* Keeps track of the number of free bytes remaining, but says nothing about
\r
159 static size_t xFreeBytesRemaining = 0U;
\r
160 static size_t xMinimumEverFreeBytesRemaining = 0U;
\r
162 /* Gets set to the top bit of an size_t type. When this bit in the xBlockSize
\r
163 member of an BlockLink_t structure is set then the block belongs to the
\r
164 application. When the bit is free the block is still part of the free heap
\r
166 static size_t xBlockAllocatedBit = 0;
\r
168 /*-----------------------------------------------------------*/
\r
170 void *pvPortMalloc( size_t xWantedSize )
\r
172 BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
\r
173 void *pvReturn = NULL;
\r
175 /* The heap must be initialised before the first call to
\r
176 prvPortMalloc(). */
\r
177 configASSERT( pxEnd );
\r
181 /* Check the requested block size is not so large that the top bit is
\r
182 set. The top bit of the block size member of the BlockLink_t structure
\r
183 is used to determine who owns the block - the application or the
\r
184 kernel, so it must be free. */
\r
185 if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
\r
187 /* The wanted size is increased so it can contain a BlockLink_t
\r
188 structure in addition to the requested amount of bytes. */
\r
189 if( xWantedSize > 0 )
\r
191 xWantedSize += xHeapStructSize;
\r
193 /* Ensure that blocks are always aligned to the required number
\r
195 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
\r
197 /* Byte alignment required. */
\r
198 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
\r
202 mtCOVERAGE_TEST_MARKER();
\r
207 mtCOVERAGE_TEST_MARKER();
\r
210 if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
\r
212 /* Traverse the list from the start (lowest address) block until
\r
213 one of adequate size is found. */
\r
214 pxPreviousBlock = &xStart;
\r
215 pxBlock = xStart.pxNextFreeBlock;
\r
216 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
\r
218 pxPreviousBlock = pxBlock;
\r
219 pxBlock = pxBlock->pxNextFreeBlock;
\r
222 /* If the end marker was reached then a block of adequate size
\r
224 if( pxBlock != pxEnd )
\r
226 /* Return the memory space pointed to - jumping over the
\r
227 BlockLink_t structure at its start. */
\r
228 pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );
\r
230 /* This block is being returned for use so must be taken out
\r
231 of the list of free blocks. */
\r
232 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
\r
234 /* If the block is larger than required it can be split into
\r
236 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
\r
238 /* This block is to be split into two. Create a new
\r
239 block following the number of bytes requested. The void
\r
240 cast is used to prevent byte alignment warnings from the
\r
242 pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
\r
244 /* Calculate the sizes of two blocks split from the
\r
246 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
\r
247 pxBlock->xBlockSize = xWantedSize;
\r
249 /* Insert the new block into the list of free blocks. */
\r
250 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
\r
254 mtCOVERAGE_TEST_MARKER();
\r
257 xFreeBytesRemaining -= pxBlock->xBlockSize;
\r
259 if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
\r
261 xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
\r
265 mtCOVERAGE_TEST_MARKER();
\r
268 /* The block is being returned - it is allocated and owned
\r
269 by the application and has no "next" block. */
\r
270 pxBlock->xBlockSize |= xBlockAllocatedBit;
\r
271 pxBlock->pxNextFreeBlock = NULL;
\r
275 mtCOVERAGE_TEST_MARKER();
\r
280 mtCOVERAGE_TEST_MARKER();
\r
285 mtCOVERAGE_TEST_MARKER();
\r
288 traceMALLOC( pvReturn, xWantedSize );
\r
290 ( void ) xTaskResumeAll();
\r
292 #if( configUSE_MALLOC_FAILED_HOOK == 1 )
\r
294 if( pvReturn == NULL )
\r
296 extern void vApplicationMallocFailedHook( void );
\r
297 vApplicationMallocFailedHook();
\r
301 mtCOVERAGE_TEST_MARKER();
\r
308 /*-----------------------------------------------------------*/
\r
310 void vPortFree( void *pv )
\r
312 uint8_t *puc = ( uint8_t * ) pv;
\r
313 BlockLink_t *pxLink;
\r
317 /* The memory being freed will have an BlockLink_t structure immediately
\r
319 puc -= xHeapStructSize;
\r
321 /* This casting is to keep the compiler from issuing warnings. */
\r
322 pxLink = ( void * ) puc;
\r
324 /* Check the block is actually allocated. */
\r
325 configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
\r
326 configASSERT( pxLink->pxNextFreeBlock == NULL );
\r
328 if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )
\r
330 if( pxLink->pxNextFreeBlock == NULL )
\r
332 /* The block is being returned to the heap - it is no longer
\r
334 pxLink->xBlockSize &= ~xBlockAllocatedBit;
\r
338 /* Add this block to the list of free blocks. */
\r
339 xFreeBytesRemaining += pxLink->xBlockSize;
\r
340 traceFREE( pv, pxLink->xBlockSize );
\r
341 prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
\r
343 ( void ) xTaskResumeAll();
\r
347 mtCOVERAGE_TEST_MARKER();
\r
352 mtCOVERAGE_TEST_MARKER();
\r
356 /*-----------------------------------------------------------*/
\r
358 size_t xPortGetFreeHeapSize( void )
\r
360 return xFreeBytesRemaining;
\r
362 /*-----------------------------------------------------------*/
\r
364 size_t xPortGetMinimumEverFreeHeapSize( void )
\r
366 return xMinimumEverFreeBytesRemaining;
\r
368 /*-----------------------------------------------------------*/
\r
370 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )
\r
372 BlockLink_t *pxIterator;
\r
375 /* Iterate through the list until a block is found that has a higher address
\r
376 than the block being inserted. */
\r
377 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
\r
379 /* Nothing to do here, just iterate to the right position. */
\r
382 /* Do the block being inserted, and the block it is being inserted after
\r
383 make a contiguous block of memory? */
\r
384 puc = ( uint8_t * ) pxIterator;
\r
385 if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )
\r
387 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
\r
388 pxBlockToInsert = pxIterator;
\r
392 mtCOVERAGE_TEST_MARKER();
\r
395 /* Do the block being inserted, and the block it is being inserted before
\r
396 make a contiguous block of memory? */
\r
397 puc = ( uint8_t * ) pxBlockToInsert;
\r
398 if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )
\r
400 if( pxIterator->pxNextFreeBlock != pxEnd )
\r
402 /* Form one big block from the two blocks. */
\r
403 pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
\r
404 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
\r
408 pxBlockToInsert->pxNextFreeBlock = pxEnd;
\r
413 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
\r
416 /* If the block being inserted plugged a gab, so was merged with the block
\r
417 before and the block after, then it's pxNextFreeBlock pointer will have
\r
418 already been set, and should not be set here as that would make it point
\r
420 if( pxIterator != pxBlockToInsert )
\r
422 pxIterator->pxNextFreeBlock = pxBlockToInsert;
\r
426 mtCOVERAGE_TEST_MARKER();
\r
429 /*-----------------------------------------------------------*/
\r
431 void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions )
\r
433 BlockLink_t *pxFirstFreeBlockInRegion = NULL, *pxPreviousFreeBlock;
\r
434 size_t xAlignedHeap;
\r
435 size_t xTotalRegionSize, xTotalHeapSize = 0;
\r
436 BaseType_t xDefinedRegions = 0;
\r
438 const HeapRegion_t *pxHeapRegion;
\r
440 /* Can only call once! */
\r
441 configASSERT( pxEnd == NULL );
\r
443 pxHeapRegion = &( pxHeapRegions[ xDefinedRegions ] );
\r
445 while( pxHeapRegion->xSizeInBytes > 0 )
\r
447 xTotalRegionSize = pxHeapRegion->xSizeInBytes;
\r
449 /* Ensure the heap region starts on a correctly aligned boundary. */
\r
450 xAddress = ( size_t ) pxHeapRegion->pucStartAddress;
\r
451 if( ( xAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
\r
453 xAddress += ( portBYTE_ALIGNMENT - 1 );
\r
454 xAddress &= ~portBYTE_ALIGNMENT_MASK;
\r
456 /* Adjust the size for the bytes lost to alignment. */
\r
457 xTotalRegionSize -= xAddress - ( size_t ) pxHeapRegion->pucStartAddress;
\r
460 xAlignedHeap = xAddress;
\r
462 /* Set xStart if it has not already been set. */
\r
463 if( xDefinedRegions == 0 )
\r
465 /* xStart is used to hold a pointer to the first item in the list of
\r
466 free blocks. The void cast is used to prevent compiler warnings. */
\r
467 xStart.pxNextFreeBlock = ( BlockLink_t * ) xAlignedHeap;
\r
468 xStart.xBlockSize = ( size_t ) 0;
\r
472 /* Should only get here if one region has already been added to the
\r
474 configASSERT( pxEnd != NULL );
\r
476 /* Check blocks are passed in with increasing start addresses. */
\r
477 configASSERT( xAddress > ( size_t ) pxEnd );
\r
480 /* Remember the location of the end marker in the previous region, if
\r
482 pxPreviousFreeBlock = pxEnd;
\r
484 /* pxEnd is used to mark the end of the list of free blocks and is
\r
485 inserted at the end of the region space. */
\r
486 xAddress = xAlignedHeap + xTotalRegionSize;
\r
487 xAddress -= xHeapStructSize;
\r
488 xAddress &= ~portBYTE_ALIGNMENT_MASK;
\r
489 pxEnd = ( BlockLink_t * ) xAddress;
\r
490 pxEnd->xBlockSize = 0;
\r
491 pxEnd->pxNextFreeBlock = NULL;
\r
493 /* To start with there is a single free block in this region that is
\r
494 sized to take up the entire heap region minus the space taken by the
\r
495 free block structure. */
\r
496 pxFirstFreeBlockInRegion = ( BlockLink_t * ) xAlignedHeap;
\r
497 pxFirstFreeBlockInRegion->xBlockSize = xAddress - ( size_t ) pxFirstFreeBlockInRegion;
\r
498 pxFirstFreeBlockInRegion->pxNextFreeBlock = pxEnd;
\r
500 /* If this is not the first region that makes up the entire heap space
\r
501 then link the previous region to this region. */
\r
502 if( pxPreviousFreeBlock != NULL )
\r
504 pxPreviousFreeBlock->pxNextFreeBlock = pxFirstFreeBlockInRegion;
\r
507 xTotalHeapSize += pxFirstFreeBlockInRegion->xBlockSize;
\r
509 /* Move onto the next HeapRegion_t structure. */
\r
511 pxHeapRegion = &( pxHeapRegions[ xDefinedRegions ] );
\r
514 xMinimumEverFreeBytesRemaining = xTotalHeapSize;
\r
515 xFreeBytesRemaining = xTotalHeapSize;
\r
517 /* Check something was actually defined before it is accessed. */
\r
518 configASSERT( xTotalHeapSize );
\r
520 /* Work out the position of the top bit in a size_t variable. */
\r
521 xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );
\r