2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 * A sample implementation of pvPortMalloc() and vPortFree() that combines
\r
69 * (coalescences) adjacent memory blocks as they are freed, and in so doing
\r
70 * limits memory fragmentation.
\r
72 * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the
\r
73 * memory management pages of http://www.FreeRTOS.org for more information.
\r
77 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
78 all the API functions to use the MPU wrappers. That should only be done when
\r
79 task.h is included from an application file. */
\r
80 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
82 #include "FreeRTOS.h"
\r
85 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
87 /* Block sizes must not get too small. */
\r
88 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )
\r
90 /* Allocate the memory for the heap. The struct is used to force byte
\r
91 alignment without using any non-portable code. */
\r
92 static union xRTOS_HEAP
\r
94 #if portBYTE_ALIGNMENT == 8
\r
95 volatile portDOUBLE dDummy;
\r
97 volatile unsigned long ulDummy;
\r
99 unsigned char ucHeap[ configTOTAL_HEAP_SIZE ];
\r
102 /* Define the linked list structure. This is used to link free blocks in order
\r
103 of their memory address. */
\r
104 typedef struct A_BLOCK_LINK
\r
106 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
\r
107 size_t xBlockSize; /*<< The size of the free block. */
\r
110 /*-----------------------------------------------------------*/
\r
113 * Inserts a block of memory that is being freed into the correct position in
\r
114 * the list of free memory blocks. The block being freed will be merged with
\r
115 * the block in front it and/or the block behind it if the memory blocks are
\r
116 * adjacent to each other.
\r
118 static void prvInsertBlockIntoFreeList( xBlockLink *pxBlockToInsert );
\r
121 * Called automatically to setup the required heap structures the first time
\r
122 * pvPortMalloc() is called.
\r
124 static void prvHeapInit( void );
\r
126 /*-----------------------------------------------------------*/
\r
128 /* The size of the structure placed at the beginning of each allocated memory
\r
129 block must by correctly byte aligned. */
\r
130 static const unsigned short heapSTRUCT_SIZE = ( sizeof( xBlockLink ) + portBYTE_ALIGNMENT - ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) );
\r
132 /* Ensure the pxEnd pointer will end up on the correct byte alignment. */
\r
133 static const size_t xTotalHeapSize = ( ( size_t ) configTOTAL_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );
\r
135 /* Create a couple of list links to mark the start and end of the list. */
\r
136 static xBlockLink xStart, *pxEnd = NULL;
\r
138 /* Keeps track of the number of free bytes remaining, but says nothing about
\r
140 static size_t xFreeBytesRemaining = ( ( size_t ) configTOTAL_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );
\r
142 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */
\r
144 /*-----------------------------------------------------------*/
\r
146 void *pvPortMalloc( size_t xWantedSize )
\r
148 xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
\r
149 void *pvReturn = NULL;
\r
153 /* If this is the first call to malloc then the heap will require
\r
154 initialisation to setup the list of free blocks. */
\r
155 if( pxEnd == NULL )
\r
160 /* The wanted size is increased so it can contain a xBlockLink
\r
161 structure in addition to the requested amount of bytes. */
\r
162 if( xWantedSize > 0 )
\r
164 xWantedSize += heapSTRUCT_SIZE;
\r
166 /* Ensure that blocks are always aligned to the required number of
\r
168 if( xWantedSize & portBYTE_ALIGNMENT_MASK )
\r
170 /* Byte alignment required. */
\r
171 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
\r
175 if( ( xWantedSize > 0 ) && ( xWantedSize < xTotalHeapSize ) )
\r
177 /* Traverse the list from the start (lowest address) block until one
\r
178 of adequate size is found. */
\r
179 pxPreviousBlock = &xStart;
\r
180 pxBlock = xStart.pxNextFreeBlock;
\r
181 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
\r
183 pxPreviousBlock = pxBlock;
\r
184 pxBlock = pxBlock->pxNextFreeBlock;
\r
187 /* If the end marker was reached then a block of adequate size was
\r
189 if( pxBlock != pxEnd )
\r
191 /* Return the memory space - jumping over the xBlockLink structure
\r
193 pvReturn = ( void * ) ( ( ( unsigned char * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
\r
195 /* This block is being returned for use so must be taken out of
\r
196 the list of free blocks. */
\r
197 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
\r
199 /* If the block is larger than required it can be split into two. */
\r
200 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
\r
202 /* This block is to be split into two. Create a new block
\r
203 following the number of bytes requested. The void cast is
\r
204 used to prevent byte alignment warnings from the compiler. */
\r
205 pxNewBlockLink = ( void * ) ( ( ( unsigned char * ) pxBlock ) + xWantedSize );
\r
207 /* Calculate the sizes of two blocks split from the single
\r
209 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
\r
210 pxBlock->xBlockSize = xWantedSize;
\r
212 /* Insert the new block into the list of free blocks. */
\r
213 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
\r
216 xFreeBytesRemaining -= pxBlock->xBlockSize;
\r
222 #if( configUSE_MALLOC_FAILED_HOOK == 1 )
\r
224 if( pvReturn == NULL )
\r
226 extern void vApplicationMallocFailedHook( void );
\r
227 vApplicationMallocFailedHook();
\r
234 /*-----------------------------------------------------------*/
\r
236 void vPortFree( void *pv )
\r
238 unsigned char *puc = ( unsigned char * ) pv;
\r
239 xBlockLink *pxLink;
\r
243 /* The memory being freed will have an xBlockLink structure immediately
\r
245 puc -= heapSTRUCT_SIZE;
\r
247 /* This casting is to keep the compiler from issuing warnings. */
\r
248 pxLink = ( void * ) puc;
\r
252 /* Add this block to the list of free blocks. */
\r
253 xFreeBytesRemaining += pxLink->xBlockSize;
\r
254 prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );
\r
259 /*-----------------------------------------------------------*/
\r
261 size_t xPortGetFreeHeapSize( void )
\r
263 return xFreeBytesRemaining;
\r
265 /*-----------------------------------------------------------*/
\r
267 void vPortInitialiseBlocks( void )
\r
269 /* This just exists to keep the linker quiet. */
\r
271 /*-----------------------------------------------------------*/
\r
273 static void prvHeapInit( void )
\r
275 xBlockLink *pxFirstFreeBlock;
\r
276 unsigned char *pucHeapEnd;
\r
278 /* Ensure the start of the heap is aligned. */
\r
279 configASSERT( ( ( ( unsigned long ) xHeap.ucHeap ) & ( ( unsigned long ) portBYTE_ALIGNMENT_MASK ) ) == 0UL );
\r
281 /* xStart is used to hold a pointer to the first item in the list of free
\r
282 blocks. The void cast is used to prevent compiler warnings. */
\r
283 xStart.pxNextFreeBlock = ( void * ) xHeap.ucHeap;
\r
284 xStart.xBlockSize = ( size_t ) 0;
\r
286 /* pxEnd is used to mark the end of the list of free blocks and is inserted
\r
287 at the end of the heap space. */
\r
288 pucHeapEnd = xHeap.ucHeap + xTotalHeapSize;
\r
289 pucHeapEnd -= heapSTRUCT_SIZE;
\r
290 pxEnd = ( void * ) pucHeapEnd;
\r
291 configASSERT( ( ( ( unsigned long ) pxEnd ) & ( ( unsigned long ) portBYTE_ALIGNMENT_MASK ) ) == 0UL );
\r
292 pxEnd->xBlockSize = 0;
\r
293 pxEnd->pxNextFreeBlock = NULL;
\r
295 /* To start with there is a single free block that is sized to take up the
\r
296 entire heap space, minus the space taken by pxEnd. */
\r
297 pxFirstFreeBlock = ( void * ) xHeap.ucHeap;
\r
298 pxFirstFreeBlock->xBlockSize = xTotalHeapSize - heapSTRUCT_SIZE;
\r
299 pxFirstFreeBlock->pxNextFreeBlock = pxEnd;
\r
301 /* The heap now contains pxEnd. */
\r
302 xFreeBytesRemaining -= heapSTRUCT_SIZE;
\r
304 /*-----------------------------------------------------------*/
\r
306 static void prvInsertBlockIntoFreeList( xBlockLink *pxBlockToInsert )
\r
308 xBlockLink *pxIterator;
\r
309 unsigned char *puc;
\r
311 /* Iterate through the list until a block is found that has a higher address
\r
312 than the block being inserted. */
\r
313 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
\r
315 /* Nothing to do here, just iterate to the right position. */
\r
318 /* Do the block being inserted, and the block it is being inserted after
\r
319 make a contiguous block of memory? */
\r
320 puc = ( unsigned char * ) pxIterator;
\r
321 if( ( puc + pxIterator->xBlockSize ) == ( unsigned char * ) pxBlockToInsert )
\r
323 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
\r
324 pxBlockToInsert = pxIterator;
\r
327 /* Do the block being inserted, and the block it is being inserted before
\r
328 make a contiguous block of memory? */
\r
329 puc = ( unsigned char * ) pxBlockToInsert;
\r
330 if( ( puc + pxBlockToInsert->xBlockSize ) == ( unsigned char * ) pxIterator->pxNextFreeBlock )
\r
332 if( pxIterator->pxNextFreeBlock != pxEnd )
\r
334 /* Form one big block from the two blocks. */
\r
335 pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
\r
336 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
\r
340 pxBlockToInsert->pxNextFreeBlock = pxEnd;
\r
345 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
\r
348 /* If the block being inserted plugged a gab, so was merged with the block
\r
349 before and the block after, then it's pxNextFreeBlock pointer will have
\r
350 already been set, and should not be set here as that would make it point
\r
352 if( pxIterator != pxBlockToInsert )
\r
354 pxIterator->pxNextFreeBlock = pxBlockToInsert;
\r