2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
97 * A sample implementation of pvPortMalloc() that allows the heap to be defined
\r
98 * across multiple non-contigous blocks and combines (coalescences) adjacent
\r
99 * memory blocks as they are freed.
\r
101 * See heap_1.c, heap_2.c, heap_3.c and heap_4.c for alternative
\r
102 * implementations, and the memory management pages of http://www.FreeRTOS.org
\r
103 * for more information.
\r
107 * vPortDefineHeapRegions() ***must*** be called before pvPortMalloc().
\r
108 * pvPortMalloc() will be called if any task objects (tasks, queues, event
\r
109 * groups, etc.) are created, therefore vPortDefineHeapRegions() ***must*** be
\r
110 * called before any other objects are defined.
\r
112 * vPortDefineHeapRegions() takes a single parameter. The parameter is an array
\r
113 * of HeapRegion_t structures. HeapRegion_t is defined in portable.h as
\r
115 * typedef struct HeapRegion
\r
117 * uint8_t *pucStartAddress; << Start address of a block of memory that will be part of the heap.
\r
118 * size_t xSizeInBytes; << Size of the block of memory.
\r
121 * The array is terminated using a NULL zero sized region definition, and the
\r
122 * memory regions defined in the array ***must*** appear in address order from
\r
123 * low address to high address. So the following is a valid example of how
\r
124 * to use the function.
\r
126 * HeapRegion_t xHeapRegions[] =
\r
128 * { ( uint8_t * ) 0x80000000UL, 0x10000 }, << Defines a block of 0x10000 bytes starting at address 0x80000000
\r
129 * { ( uint8_t * ) 0x90000000UL, 0xa0000 }, << Defines a block of 0xa0000 bytes starting at address of 0x90000000
\r
130 * { NULL, 0 } << Terminates the array.
\r
133 * vPortDefineHeapRegions( xHeapRegions ); << Pass the array into vPortDefineHeapRegions().
\r
135 * Note 0x80000000 is the lower address so appears in the array first.
\r
138 #include <stdlib.h>
\r
140 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
141 all the API functions to use the MPU wrappers. That should only be done when
\r
142 task.h is included from an application file. */
\r
143 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
145 #include "FreeRTOS.h"
\r
148 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
150 /* Block sizes must not get too small. */
\r
151 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( uxHeapStructSize << 1 ) )
\r
153 /* Assumes 8bit bytes! */
\r
154 #define heapBITS_PER_BYTE ( ( size_t ) 8 )
\r
156 /* Define the linked list structure. This is used to link free blocks in order
\r
157 of their memory address. */
\r
158 typedef struct A_BLOCK_LINK
\r
160 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
\r
161 size_t xBlockSize; /*<< The size of the free block. */
\r
164 /*-----------------------------------------------------------*/
\r
167 * Inserts a block of memory that is being freed into the correct position in
\r
168 * the list of free memory blocks. The block being freed will be merged with
\r
169 * the block in front it and/or the block behind it if the memory blocks are
\r
170 * adjacent to each other.
\r
172 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert );
\r
174 /*-----------------------------------------------------------*/
\r
176 /* The size of the structure placed at the beginning of each allocated memory
\r
177 block must by correctly byte aligned. */
\r
178 static const uint32_t uxHeapStructSize = ( ( sizeof ( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );
\r
180 /* Create a couple of list links to mark the start and end of the list. */
\r
181 static BlockLink_t xStart, *pxEnd = NULL;
\r
183 /* Keeps track of the number of free bytes remaining, but says nothing about
\r
185 static size_t xFreeBytesRemaining = 0;
\r
186 static size_t xMinimumEverFreeBytesRemaining = 0;
\r
188 /* Gets set to the top bit of an size_t type. When this bit in the xBlockSize
\r
189 member of an BlockLink_t structure is set then the block belongs to the
\r
190 application. When the bit is free the block is still part of the free heap
\r
192 static size_t xBlockAllocatedBit = 0;
\r
194 /*-----------------------------------------------------------*/
\r
196 void *pvPortMalloc( size_t xWantedSize )
\r
198 BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
\r
199 void *pvReturn = NULL;
\r
201 /* The heap must be initialised before the first call to
\r
202 prvPortMalloc(). */
\r
203 configASSERT( pxEnd );
\r
207 /* Check the requested block size is not so large that the top bit is
\r
208 set. The top bit of the block size member of the BlockLink_t structure
\r
209 is used to determine who owns the block - the application or the
\r
210 kernel, so it must be free. */
\r
211 if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
\r
213 /* The wanted size is increased so it can contain a BlockLink_t
\r
214 structure in addition to the requested amount of bytes. */
\r
215 if( xWantedSize > 0 )
\r
217 xWantedSize += uxHeapStructSize;
\r
219 /* Ensure that blocks are always aligned to the required number
\r
221 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
\r
223 /* Byte alignment required. */
\r
224 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
\r
228 mtCOVERAGE_TEST_MARKER();
\r
233 mtCOVERAGE_TEST_MARKER();
\r
236 if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
\r
238 /* Traverse the list from the start (lowest address) block until
\r
239 one of adequate size is found. */
\r
240 pxPreviousBlock = &xStart;
\r
241 pxBlock = xStart.pxNextFreeBlock;
\r
242 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
\r
244 pxPreviousBlock = pxBlock;
\r
245 pxBlock = pxBlock->pxNextFreeBlock;
\r
248 /* If the end marker was reached then a block of adequate size
\r
250 if( pxBlock != pxEnd )
\r
252 /* Return the memory space pointed to - jumping over the
\r
253 BlockLink_t structure at its start. */
\r
254 pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + uxHeapStructSize );
\r
256 /* This block is being returned for use so must be taken out
\r
257 of the list of free blocks. */
\r
258 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
\r
260 /* If the block is larger than required it can be split into
\r
262 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
\r
264 /* This block is to be split into two. Create a new
\r
265 block following the number of bytes requested. The void
\r
266 cast is used to prevent byte alignment warnings from the
\r
268 pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
\r
270 /* Calculate the sizes of two blocks split from the
\r
272 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
\r
273 pxBlock->xBlockSize = xWantedSize;
\r
275 /* Insert the new block into the list of free blocks. */
\r
276 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
\r
280 mtCOVERAGE_TEST_MARKER();
\r
283 xFreeBytesRemaining -= pxBlock->xBlockSize;
\r
285 if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
\r
287 xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
\r
291 mtCOVERAGE_TEST_MARKER();
\r
294 /* The block is being returned - it is allocated and owned
\r
295 by the application and has no "next" block. */
\r
296 pxBlock->xBlockSize |= xBlockAllocatedBit;
\r
297 pxBlock->pxNextFreeBlock = NULL;
\r
301 mtCOVERAGE_TEST_MARKER();
\r
306 mtCOVERAGE_TEST_MARKER();
\r
311 mtCOVERAGE_TEST_MARKER();
\r
314 traceMALLOC( pvReturn, xWantedSize );
\r
316 ( void ) xTaskResumeAll();
\r
318 #if( configUSE_MALLOC_FAILED_HOOK == 1 )
\r
320 if( pvReturn == NULL )
\r
322 extern void vApplicationMallocFailedHook( void );
\r
323 vApplicationMallocFailedHook();
\r
327 mtCOVERAGE_TEST_MARKER();
\r
334 /*-----------------------------------------------------------*/
\r
336 void vPortFree( void *pv )
\r
338 uint8_t *puc = ( uint8_t * ) pv;
\r
339 BlockLink_t *pxLink;
\r
343 /* The memory being freed will have an BlockLink_t structure immediately
\r
345 puc -= uxHeapStructSize;
\r
347 /* This casting is to keep the compiler from issuing warnings. */
\r
348 pxLink = ( void * ) puc;
\r
350 /* Check the block is actually allocated. */
\r
351 configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
\r
352 configASSERT( pxLink->pxNextFreeBlock == NULL );
\r
354 if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )
\r
356 if( pxLink->pxNextFreeBlock == NULL )
\r
358 /* The block is being returned to the heap - it is no longer
\r
360 pxLink->xBlockSize &= ~xBlockAllocatedBit;
\r
364 /* Add this block to the list of free blocks. */
\r
365 xFreeBytesRemaining += pxLink->xBlockSize;
\r
366 traceFREE( pv, pxLink->xBlockSize );
\r
367 prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
\r
369 ( void ) xTaskResumeAll();
\r
373 mtCOVERAGE_TEST_MARKER();
\r
378 mtCOVERAGE_TEST_MARKER();
\r
382 /*-----------------------------------------------------------*/
\r
384 size_t xPortGetFreeHeapSize( void )
\r
386 return xFreeBytesRemaining;
\r
388 /*-----------------------------------------------------------*/
\r
390 size_t xPortGetMinimumEverFreeHeapSize( void )
\r
392 return xMinimumEverFreeBytesRemaining;
\r
394 /*-----------------------------------------------------------*/
\r
396 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )
\r
398 BlockLink_t *pxIterator;
\r
401 /* Iterate through the list until a block is found that has a higher address
\r
402 than the block being inserted. */
\r
403 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
\r
405 /* Nothing to do here, just iterate to the right position. */
\r
408 /* Do the block being inserted, and the block it is being inserted after
\r
409 make a contiguous block of memory? */
\r
410 puc = ( uint8_t * ) pxIterator;
\r
411 if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )
\r
413 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
\r
414 pxBlockToInsert = pxIterator;
\r
418 mtCOVERAGE_TEST_MARKER();
\r
421 /* Do the block being inserted, and the block it is being inserted before
\r
422 make a contiguous block of memory? */
\r
423 puc = ( uint8_t * ) pxBlockToInsert;
\r
424 if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )
\r
426 if( pxIterator->pxNextFreeBlock != pxEnd )
\r
428 /* Form one big block from the two blocks. */
\r
429 pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
\r
430 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
\r
434 pxBlockToInsert->pxNextFreeBlock = pxEnd;
\r
439 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
\r
442 /* If the block being inserted plugged a gab, so was merged with the block
\r
443 before and the block after, then it's pxNextFreeBlock pointer will have
\r
444 already been set, and should not be set here as that would make it point
\r
446 if( pxIterator != pxBlockToInsert )
\r
448 pxIterator->pxNextFreeBlock = pxBlockToInsert;
\r
452 mtCOVERAGE_TEST_MARKER();
\r
455 /*-----------------------------------------------------------*/
\r
457 void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions )
\r
459 BlockLink_t *pxFirstFreeBlockInRegion = NULL, *pxPreviousFreeBlock;
\r
460 uint8_t *pucAlignedHeap;
\r
461 size_t xTotalRegionSize, xTotalHeapSize = 0;
\r
462 BaseType_t xDefinedRegions = 0;
\r
463 uint32_t ulAddress;
\r
464 const HeapRegion_t *pxHeapRegion;
\r
466 /* Can only call once! */
\r
467 configASSERT( pxEnd == NULL );
\r
469 pxHeapRegion = &( pxHeapRegions[ xDefinedRegions ] );
\r
471 while( pxHeapRegion->xSizeInBytes > 0 )
\r
473 xTotalRegionSize = pxHeapRegion->xSizeInBytes;
\r
475 /* Ensure the heap region starts on a correctly aligned boundary. */
\r
476 ulAddress = ( uint32_t ) pxHeapRegion->pucStartAddress;
\r
477 if( ( ulAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
\r
479 ulAddress += ( portBYTE_ALIGNMENT - 1 );
\r
480 ulAddress &= ~portBYTE_ALIGNMENT_MASK;
\r
482 /* Adjust the size for the bytes lost to alignment. */
\r
483 xTotalRegionSize -= ulAddress - ( uint32_t ) pxHeapRegion->pucStartAddress;
\r
486 pucAlignedHeap = ( uint8_t * ) ulAddress;
\r
488 /* Set xStart if it has not already been set. */
\r
489 if( xDefinedRegions == 0 )
\r
491 /* xStart is used to hold a pointer to the first item in the list of
\r
492 free blocks. The void cast is used to prevent compiler warnings. */
\r
493 xStart.pxNextFreeBlock = ( BlockLink_t * ) pucAlignedHeap;
\r
494 xStart.xBlockSize = ( size_t ) 0;
\r
498 /* Should only get here if one region has already been added to the
\r
500 configASSERT( pxEnd != NULL );
\r
502 /* Check blocks are passed in with increasing start addresses. */
\r
503 configASSERT( ulAddress > ( uint32_t ) pxEnd );
\r
506 /* Remember the location of the end marker in the previous region, if
\r
508 pxPreviousFreeBlock = pxEnd;
\r
510 /* pxEnd is used to mark the end of the list of free blocks and is
\r
511 inserted at the end of the region space. */
\r
512 ulAddress = ( ( uint32_t ) pucAlignedHeap ) + xTotalRegionSize;
\r
513 ulAddress -= uxHeapStructSize;
\r
514 ulAddress &= ~portBYTE_ALIGNMENT_MASK;
\r
515 pxEnd = ( BlockLink_t * ) ulAddress;
\r
516 pxEnd->xBlockSize = 0;
\r
517 pxEnd->pxNextFreeBlock = NULL;
\r
519 /* To start with there is a single free block in this region that is
\r
520 sized to take up the entire heap region minus the space taken by the
\r
521 free block structure. */
\r
522 pxFirstFreeBlockInRegion = ( BlockLink_t * ) pucAlignedHeap;
\r
523 pxFirstFreeBlockInRegion->xBlockSize = ulAddress - ( uint32_t ) pxFirstFreeBlockInRegion;
\r
524 pxFirstFreeBlockInRegion->pxNextFreeBlock = pxEnd;
\r
526 /* If this is not the first region that makes up the entire heap space
\r
527 then link the previous region to this region. */
\r
528 if( pxPreviousFreeBlock != NULL )
\r
530 pxPreviousFreeBlock->pxNextFreeBlock = pxFirstFreeBlockInRegion;
\r
533 xTotalHeapSize += pxFirstFreeBlockInRegion->xBlockSize;
\r
535 /* Move onto the next HeapRegion_t structure. */
\r
537 pxHeapRegion = &( pxHeapRegions[ xDefinedRegions ] );
\r
540 xMinimumEverFreeBytesRemaining = xTotalHeapSize;
\r
541 xFreeBytesRemaining = xTotalHeapSize;
\r
543 /* Check something was actually defined before it is accessed. */
\r
544 configASSERT( xTotalHeapSize );
\r
546 /* Work out the position of the top bit in a size_t variable. */
\r
547 xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );
\r