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() and vPortFree() that combines
\r
98 * (coalescences) adjacent memory blocks as they are freed, and in so doing
\r
99 * limits memory fragmentation.
\r
101 * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the
\r
102 * memory management pages of http://www.FreeRTOS.org for more information.
\r
104 #include <stdlib.h>
\r
106 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
107 all the API functions to use the MPU wrappers. That should only be done when
\r
108 task.h is included from an application file. */
\r
109 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
111 #include "FreeRTOS.h"
\r
114 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
116 /* Block sizes must not get too small. */
\r
117 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize * 2 ) )
\r
119 /* Assumes 8bit bytes! */
\r
120 #define heapBITS_PER_BYTE ( ( size_t ) 8 )
\r
122 /* Allocate the memory for the heap. */
\r
123 #if( configAPPLICATION_ALLOCATED_HEAP == 1 )
\r
124 /* The application writer has already defined the array used for the RTOS
\r
125 heap - probably so it can be placed in a special segment or address. */
\r
126 extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
128 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
129 #endif /* configAPPLICATION_ALLOCATED_HEAP */
\r
131 /* Define the linked list structure. This is used to link free blocks in order
\r
132 of their memory address. */
\r
133 typedef struct A_BLOCK_LINK
\r
135 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
\r
136 size_t xBlockSize; /*<< The size of the free block. */
\r
139 /*-----------------------------------------------------------*/
\r
142 * Inserts a block of memory that is being freed into the correct position in
\r
143 * the list of free memory blocks. The block being freed will be merged with
\r
144 * the block in front it and/or the block behind it if the memory blocks are
\r
145 * adjacent to each other.
\r
147 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert );
\r
150 * Called automatically to setup the required heap structures the first time
\r
151 * pvPortMalloc() is called.
\r
153 static void prvHeapInit( void );
\r
155 /*-----------------------------------------------------------*/
\r
157 /* The size of the structure placed at the beginning of each allocated memory
\r
158 block must by correctly byte aligned. */
\r
159 static const size_t xHeapStructSize = ( ( sizeof( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );
\r
161 /* Create a couple of list links to mark the start and end of the list. */
\r
162 static BlockLink_t xStart, *pxEnd = NULL;
\r
164 /* Keeps track of the number of free bytes remaining, but says nothing about
\r
166 static size_t xFreeBytesRemaining = 0U;
\r
167 static size_t xMinimumEverFreeBytesRemaining = 0U;
\r
169 /* Gets set to the top bit of an size_t type. When this bit in the xBlockSize
\r
170 member of an BlockLink_t structure is set then the block belongs to the
\r
171 application. When the bit is free the block is still part of the free heap
\r
173 static size_t xBlockAllocatedBit = 0;
\r
175 /*-----------------------------------------------------------*/
\r
177 void *pvPortMalloc( size_t xWantedSize )
\r
179 BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
\r
180 void *pvReturn = NULL;
\r
184 /* If this is the first call to malloc then the heap will require
\r
185 initialisation to setup the list of free blocks. */
\r
186 if( pxEnd == NULL )
\r
192 mtCOVERAGE_TEST_MARKER();
\r
195 /* Check the requested block size is not so large that the top bit is
\r
196 set. The top bit of the block size member of the BlockLink_t structure
\r
197 is used to determine who owns the block - the application or the
\r
198 kernel, so it must be free. */
\r
199 if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
\r
201 /* The wanted size is increased so it can contain a BlockLink_t
\r
202 structure in addition to the requested amount of bytes. */
\r
203 if( xWantedSize > 0 )
\r
205 xWantedSize += xHeapStructSize;
\r
207 /* Ensure that blocks are always aligned to the required number
\r
209 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
\r
211 /* Byte alignment required. */
\r
212 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
\r
213 configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
\r
217 mtCOVERAGE_TEST_MARKER();
\r
222 mtCOVERAGE_TEST_MARKER();
\r
225 if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
\r
227 /* Traverse the list from the start (lowest address) block until
\r
228 one of adequate size is found. */
\r
229 pxPreviousBlock = &xStart;
\r
230 pxBlock = xStart.pxNextFreeBlock;
\r
231 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
\r
233 pxPreviousBlock = pxBlock;
\r
234 pxBlock = pxBlock->pxNextFreeBlock;
\r
237 /* If the end marker was reached then a block of adequate size
\r
239 if( pxBlock != pxEnd )
\r
241 /* Return the memory space pointed to - jumping over the
\r
242 BlockLink_t structure at its start. */
\r
243 pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );
\r
245 /* This block is being returned for use so must be taken out
\r
246 of the list of free blocks. */
\r
247 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
\r
249 /* If the block is larger than required it can be split into
\r
251 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
\r
253 /* This block is to be split into two. Create a new
\r
254 block following the number of bytes requested. The void
\r
255 cast is used to prevent byte alignment warnings from the
\r
257 pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
\r
258 configASSERT( ( ( ( uint32_t ) pxNewBlockLink ) & portBYTE_ALIGNMENT_MASK ) == 0 );
\r
260 /* Calculate the sizes of two blocks split from the
\r
262 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
\r
263 pxBlock->xBlockSize = xWantedSize;
\r
265 /* Insert the new block into the list of free blocks. */
\r
266 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
\r
270 mtCOVERAGE_TEST_MARKER();
\r
273 xFreeBytesRemaining -= pxBlock->xBlockSize;
\r
275 if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
\r
277 xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
\r
281 mtCOVERAGE_TEST_MARKER();
\r
284 /* The block is being returned - it is allocated and owned
\r
285 by the application and has no "next" block. */
\r
286 pxBlock->xBlockSize |= xBlockAllocatedBit;
\r
287 pxBlock->pxNextFreeBlock = NULL;
\r
291 mtCOVERAGE_TEST_MARKER();
\r
296 mtCOVERAGE_TEST_MARKER();
\r
301 mtCOVERAGE_TEST_MARKER();
\r
304 traceMALLOC( pvReturn, xWantedSize );
\r
306 ( void ) xTaskResumeAll();
\r
308 #if( configUSE_MALLOC_FAILED_HOOK == 1 )
\r
310 if( pvReturn == NULL )
\r
312 extern void vApplicationMallocFailedHook( void );
\r
313 vApplicationMallocFailedHook();
\r
317 mtCOVERAGE_TEST_MARKER();
\r
322 configASSERT( ( ( ( uint32_t ) pvReturn ) & portBYTE_ALIGNMENT_MASK ) == 0 );
\r
325 /*-----------------------------------------------------------*/
\r
327 void vPortFree( void *pv )
\r
329 uint8_t *puc = ( uint8_t * ) pv;
\r
330 BlockLink_t *pxLink;
\r
334 /* The memory being freed will have an BlockLink_t structure immediately
\r
336 puc -= xHeapStructSize;
\r
338 /* This casting is to keep the compiler from issuing warnings. */
\r
339 pxLink = ( void * ) puc;
\r
341 /* Check the block is actually allocated. */
\r
342 configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
\r
343 configASSERT( pxLink->pxNextFreeBlock == NULL );
\r
345 if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )
\r
347 if( pxLink->pxNextFreeBlock == NULL )
\r
349 /* The block is being returned to the heap - it is no longer
\r
351 pxLink->xBlockSize &= ~xBlockAllocatedBit;
\r
355 /* Add this block to the list of free blocks. */
\r
356 xFreeBytesRemaining += pxLink->xBlockSize;
\r
357 traceFREE( pv, pxLink->xBlockSize );
\r
358 prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
\r
360 ( void ) xTaskResumeAll();
\r
364 mtCOVERAGE_TEST_MARKER();
\r
369 mtCOVERAGE_TEST_MARKER();
\r
373 /*-----------------------------------------------------------*/
\r
375 size_t xPortGetFreeHeapSize( void )
\r
377 return xFreeBytesRemaining;
\r
379 /*-----------------------------------------------------------*/
\r
381 size_t xPortGetMinimumEverFreeHeapSize( void )
\r
383 return xMinimumEverFreeBytesRemaining;
\r
385 /*-----------------------------------------------------------*/
\r
387 void vPortInitialiseBlocks( void )
\r
389 /* This just exists to keep the linker quiet. */
\r
391 /*-----------------------------------------------------------*/
\r
393 static void prvHeapInit( void )
\r
395 BlockLink_t *pxFirstFreeBlock;
\r
396 uint8_t *pucAlignedHeap;
\r
397 uint32_t ulAddress;
\r
398 size_t xTotalHeapSize = configTOTAL_HEAP_SIZE;
\r
400 /* Ensure the heap starts on a correctly aligned boundary. */
\r
401 ulAddress = ( uint32_t ) ucHeap;
\r
403 if( ( ulAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
\r
405 ulAddress += ( portBYTE_ALIGNMENT - 1 );
\r
406 ulAddress &= ~portBYTE_ALIGNMENT_MASK;
\r
407 xTotalHeapSize -= ulAddress - ( uint32_t ) ucHeap;
\r
410 pucAlignedHeap = ( uint8_t * ) ulAddress;
\r
412 /* xStart is used to hold a pointer to the first item in the list of free
\r
413 blocks. The void cast is used to prevent compiler warnings. */
\r
414 xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
\r
415 xStart.xBlockSize = ( size_t ) 0;
\r
417 /* pxEnd is used to mark the end of the list of free blocks and is inserted
\r
418 at the end of the heap space. */
\r
419 ulAddress = ( ( uint32_t ) pucAlignedHeap ) + xTotalHeapSize;
\r
420 ulAddress -= xHeapStructSize;
\r
421 ulAddress &= ~portBYTE_ALIGNMENT_MASK;
\r
422 pxEnd = ( void * ) ulAddress;
\r
423 pxEnd->xBlockSize = 0;
\r
424 pxEnd->pxNextFreeBlock = NULL;
\r
426 /* To start with there is a single free block that is sized to take up the
\r
427 entire heap space, minus the space taken by pxEnd. */
\r
428 pxFirstFreeBlock = ( void * ) pucAlignedHeap;
\r
429 pxFirstFreeBlock->xBlockSize = ulAddress - ( uint32_t ) pxFirstFreeBlock;
\r
430 pxFirstFreeBlock->pxNextFreeBlock = pxEnd;
\r
432 /* Only one block exists - and it covers the entire usable heap space. */
\r
433 xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
\r
434 xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
\r
436 /* Work out the position of the top bit in a size_t variable. */
\r
437 xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );
\r
439 /*-----------------------------------------------------------*/
\r
441 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )
\r
443 BlockLink_t *pxIterator;
\r
446 /* Iterate through the list until a block is found that has a higher address
\r
447 than the block being inserted. */
\r
448 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
\r
450 /* Nothing to do here, just iterate to the right position. */
\r
453 /* Do the block being inserted, and the block it is being inserted after
\r
454 make a contiguous block of memory? */
\r
455 puc = ( uint8_t * ) pxIterator;
\r
456 if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )
\r
458 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
\r
459 pxBlockToInsert = pxIterator;
\r
463 mtCOVERAGE_TEST_MARKER();
\r
466 /* Do the block being inserted, and the block it is being inserted before
\r
467 make a contiguous block of memory? */
\r
468 puc = ( uint8_t * ) pxBlockToInsert;
\r
469 if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )
\r
471 if( pxIterator->pxNextFreeBlock != pxEnd )
\r
473 /* Form one big block from the two blocks. */
\r
474 pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
\r
475 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
\r
479 pxBlockToInsert->pxNextFreeBlock = pxEnd;
\r
484 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
\r
487 /* If the block being inserted plugged a gab, so was merged with the block
\r
488 before and the block after, then it's pxNextFreeBlock pointer will have
\r
489 already been set, and should not be set here as that would make it point
\r
491 if( pxIterator != pxBlockToInsert )
\r
493 pxIterator->pxNextFreeBlock = pxBlockToInsert;
\r
497 mtCOVERAGE_TEST_MARKER();
\r