2 FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 * A sample implementation of pvPortMalloc() and vPortFree() that combines
\r
68 * (coalescences) adjacent memory blocks as they are freed, and in so doing
\r
69 * limits memory fragmentation.
\r
71 * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the
\r
72 * memory management pages of http://www.FreeRTOS.org for more information.
\r
76 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
77 all the API functions to use the MPU wrappers. That should only be done when
\r
78 task.h is included from an application file. */
\r
79 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
81 #include "FreeRTOS.h"
\r
84 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
86 /* Block sizes must not get too small. */
\r
87 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize * 2 ) )
\r
89 /* Assumes 8bit bytes! */
\r
90 #define heapBITS_PER_BYTE ( ( size_t ) 8 )
\r
92 /* Allocate the memory for the heap. */
\r
93 #if( configAPPLICATION_ALLOCATED_HEAP == 1 )
\r
94 /* The application writer has already defined the array used for the RTOS
\r
95 heap - probably so it can be placed in a special segment or address. */
\r
96 extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
98 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
99 #endif /* configAPPLICATION_ALLOCATED_HEAP */
\r
101 /* Define the linked list structure. This is used to link free blocks in order
\r
102 of their memory address. */
\r
103 typedef struct A_BLOCK_LINK
\r
105 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
\r
106 size_t xBlockSize; /*<< The size of the free block. */
\r
109 /*-----------------------------------------------------------*/
\r
112 * Inserts a block of memory that is being freed into the correct position in
\r
113 * the list of free memory blocks. The block being freed will be merged with
\r
114 * the block in front it and/or the block behind it if the memory blocks are
\r
115 * adjacent to each other.
\r
117 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert );
\r
120 * Called automatically to setup the required heap structures the first time
\r
121 * pvPortMalloc() is called.
\r
123 static void prvHeapInit( void );
\r
125 /*-----------------------------------------------------------*/
\r
127 /* The size of the structure placed at the beginning of each allocated memory
\r
128 block must by correctly byte aligned. */
\r
129 static const size_t xHeapStructSize = ( ( sizeof( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );
\r
131 /* Create a couple of list links to mark the start and end of the list. */
\r
132 static BlockLink_t xStart, *pxEnd = NULL;
\r
134 /* Keeps track of the number of free bytes remaining, but says nothing about
\r
136 static size_t xFreeBytesRemaining = 0U;
\r
137 static size_t xMinimumEverFreeBytesRemaining = 0U;
\r
139 /* Gets set to the top bit of an size_t type. When this bit in the xBlockSize
\r
140 member of an BlockLink_t structure is set then the block belongs to the
\r
141 application. When the bit is free the block is still part of the free heap
\r
143 static size_t xBlockAllocatedBit = 0;
\r
145 /*-----------------------------------------------------------*/
\r
147 void *pvPortMalloc( size_t xWantedSize )
\r
149 BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
\r
150 void *pvReturn = NULL;
\r
154 /* If this is the first call to malloc then the heap will require
\r
155 initialisation to setup the list of free blocks. */
\r
156 if( pxEnd == NULL )
\r
162 mtCOVERAGE_TEST_MARKER();
\r
165 /* Check the requested block size is not so large that the top bit is
\r
166 set. The top bit of the block size member of the BlockLink_t structure
\r
167 is used to determine who owns the block - the application or the
\r
168 kernel, so it must be free. */
\r
169 if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
\r
171 /* The wanted size is increased so it can contain a BlockLink_t
\r
172 structure in addition to the requested amount of bytes. */
\r
173 if( xWantedSize > 0 )
\r
175 xWantedSize += xHeapStructSize;
\r
177 /* Ensure that blocks are always aligned to the required number
\r
179 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
\r
181 /* Byte alignment required. */
\r
182 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
\r
183 configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
\r
187 mtCOVERAGE_TEST_MARKER();
\r
192 mtCOVERAGE_TEST_MARKER();
\r
195 if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
\r
197 /* Traverse the list from the start (lowest address) block until
\r
198 one of adequate size is found. */
\r
199 pxPreviousBlock = &xStart;
\r
200 pxBlock = xStart.pxNextFreeBlock;
\r
201 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
\r
203 pxPreviousBlock = pxBlock;
\r
204 pxBlock = pxBlock->pxNextFreeBlock;
\r
207 /* If the end marker was reached then a block of adequate size
\r
209 if( pxBlock != pxEnd )
\r
211 /* Return the memory space pointed to - jumping over the
\r
212 BlockLink_t structure at its start. */
\r
213 pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );
\r
215 /* This block is being returned for use so must be taken out
\r
216 of the list of free blocks. */
\r
217 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
\r
219 /* If the block is larger than required it can be split into
\r
221 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
\r
223 /* This block is to be split into two. Create a new
\r
224 block following the number of bytes requested. The void
\r
225 cast is used to prevent byte alignment warnings from the
\r
227 pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
\r
228 configASSERT( ( ( ( uint32_t ) pxNewBlockLink ) & portBYTE_ALIGNMENT_MASK ) == 0 );
\r
230 /* Calculate the sizes of two blocks split from the
\r
232 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
\r
233 pxBlock->xBlockSize = xWantedSize;
\r
235 /* Insert the new block into the list of free blocks. */
\r
236 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
\r
240 mtCOVERAGE_TEST_MARKER();
\r
243 xFreeBytesRemaining -= pxBlock->xBlockSize;
\r
245 if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
\r
247 xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
\r
251 mtCOVERAGE_TEST_MARKER();
\r
254 /* The block is being returned - it is allocated and owned
\r
255 by the application and has no "next" block. */
\r
256 pxBlock->xBlockSize |= xBlockAllocatedBit;
\r
257 pxBlock->pxNextFreeBlock = NULL;
\r
261 mtCOVERAGE_TEST_MARKER();
\r
266 mtCOVERAGE_TEST_MARKER();
\r
271 mtCOVERAGE_TEST_MARKER();
\r
274 traceMALLOC( pvReturn, xWantedSize );
\r
276 ( void ) xTaskResumeAll();
\r
278 #if( configUSE_MALLOC_FAILED_HOOK == 1 )
\r
280 if( pvReturn == NULL )
\r
282 extern void vApplicationMallocFailedHook( void );
\r
283 vApplicationMallocFailedHook();
\r
287 mtCOVERAGE_TEST_MARKER();
\r
292 configASSERT( ( ( ( uint32_t ) pvReturn ) & portBYTE_ALIGNMENT_MASK ) == 0 );
\r
295 /*-----------------------------------------------------------*/
\r
297 void vPortFree( void *pv )
\r
299 uint8_t *puc = ( uint8_t * ) pv;
\r
300 BlockLink_t *pxLink;
\r
304 /* The memory being freed will have an BlockLink_t structure immediately
\r
306 puc -= xHeapStructSize;
\r
308 /* This casting is to keep the compiler from issuing warnings. */
\r
309 pxLink = ( void * ) puc;
\r
311 /* Check the block is actually allocated. */
\r
312 configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
\r
313 configASSERT( pxLink->pxNextFreeBlock == NULL );
\r
315 if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )
\r
317 if( pxLink->pxNextFreeBlock == NULL )
\r
319 /* The block is being returned to the heap - it is no longer
\r
321 pxLink->xBlockSize &= ~xBlockAllocatedBit;
\r
325 /* Add this block to the list of free blocks. */
\r
326 xFreeBytesRemaining += pxLink->xBlockSize;
\r
327 traceFREE( pv, pxLink->xBlockSize );
\r
328 prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
\r
330 ( void ) xTaskResumeAll();
\r
334 mtCOVERAGE_TEST_MARKER();
\r
339 mtCOVERAGE_TEST_MARKER();
\r
343 /*-----------------------------------------------------------*/
\r
345 size_t xPortGetFreeHeapSize( void )
\r
347 return xFreeBytesRemaining;
\r
349 /*-----------------------------------------------------------*/
\r
351 size_t xPortGetMinimumEverFreeHeapSize( void )
\r
353 return xMinimumEverFreeBytesRemaining;
\r
355 /*-----------------------------------------------------------*/
\r
357 void vPortInitialiseBlocks( void )
\r
359 /* This just exists to keep the linker quiet. */
\r
361 /*-----------------------------------------------------------*/
\r
363 static void prvHeapInit( void )
\r
365 BlockLink_t *pxFirstFreeBlock;
\r
366 uint8_t *pucAlignedHeap;
\r
367 uint32_t ulAddress;
\r
368 size_t xTotalHeapSize = configTOTAL_HEAP_SIZE;
\r
370 /* Ensure the heap starts on a correctly aligned boundary. */
\r
371 ulAddress = ( uint32_t ) ucHeap;
\r
373 if( ( ulAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
\r
375 ulAddress += ( portBYTE_ALIGNMENT - 1 );
\r
376 ulAddress &= ~portBYTE_ALIGNMENT_MASK;
\r
377 xTotalHeapSize -= ulAddress - ( uint32_t ) ucHeap;
\r
380 pucAlignedHeap = ( uint8_t * ) ulAddress;
\r
382 /* xStart is used to hold a pointer to the first item in the list of free
\r
383 blocks. The void cast is used to prevent compiler warnings. */
\r
384 xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
\r
385 xStart.xBlockSize = ( size_t ) 0;
\r
387 /* pxEnd is used to mark the end of the list of free blocks and is inserted
\r
388 at the end of the heap space. */
\r
389 ulAddress = ( ( uint32_t ) pucAlignedHeap ) + xTotalHeapSize;
\r
390 ulAddress -= xHeapStructSize;
\r
391 ulAddress &= ~portBYTE_ALIGNMENT_MASK;
\r
392 pxEnd = ( void * ) ulAddress;
\r
393 pxEnd->xBlockSize = 0;
\r
394 pxEnd->pxNextFreeBlock = NULL;
\r
396 /* To start with there is a single free block that is sized to take up the
\r
397 entire heap space, minus the space taken by pxEnd. */
\r
398 pxFirstFreeBlock = ( void * ) pucAlignedHeap;
\r
399 pxFirstFreeBlock->xBlockSize = ulAddress - ( uint32_t ) pxFirstFreeBlock;
\r
400 pxFirstFreeBlock->pxNextFreeBlock = pxEnd;
\r
402 /* Only one block exists - and it covers the entire usable heap space. */
\r
403 xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
\r
404 xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
\r
406 /* Work out the position of the top bit in a size_t variable. */
\r
407 xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );
\r
409 /*-----------------------------------------------------------*/
\r
411 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )
\r
413 BlockLink_t *pxIterator;
\r
416 /* Iterate through the list until a block is found that has a higher address
\r
417 than the block being inserted. */
\r
418 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
\r
420 /* Nothing to do here, just iterate to the right position. */
\r
423 /* Do the block being inserted, and the block it is being inserted after
\r
424 make a contiguous block of memory? */
\r
425 puc = ( uint8_t * ) pxIterator;
\r
426 if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )
\r
428 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
\r
429 pxBlockToInsert = pxIterator;
\r
433 mtCOVERAGE_TEST_MARKER();
\r
436 /* Do the block being inserted, and the block it is being inserted before
\r
437 make a contiguous block of memory? */
\r
438 puc = ( uint8_t * ) pxBlockToInsert;
\r
439 if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )
\r
441 if( pxIterator->pxNextFreeBlock != pxEnd )
\r
443 /* Form one big block from the two blocks. */
\r
444 pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
\r
445 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
\r
449 pxBlockToInsert->pxNextFreeBlock = pxEnd;
\r
454 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
\r
457 /* If the block being inserted plugged a gab, so was merged with the block
\r
458 before and the block after, then it's pxNextFreeBlock pointer will have
\r
459 already been set, and should not be set here as that would make it point
\r
461 if( pxIterator != pxBlockToInsert )
\r
463 pxIterator->pxNextFreeBlock = pxBlockToInsert;
\r
467 mtCOVERAGE_TEST_MARKER();
\r