2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 * A sample implementation of pvPortMalloc() and vPortFree() that combines
\r
71 * (coalescences) adjacent memory blocks as they are freed, and in so doing
\r
72 * limits memory fragmentation.
\r
74 * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the
\r
75 * memory management pages of http://www.FreeRTOS.org for more information.
\r
79 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
80 all the API functions to use the MPU wrappers. That should only be done when
\r
81 task.h is included from an application file. */
\r
82 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
84 #include "FreeRTOS.h"
\r
87 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
89 /* Block sizes must not get too small. */
\r
90 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )
\r
92 /* Allocate the memory for the heap. The struct is used to force byte
\r
93 alignment without using any non-portable code. */
\r
94 static union xRTOS_HEAP
\r
96 #if portBYTE_ALIGNMENT == 8
\r
97 volatile portDOUBLE dDummy;
\r
99 volatile unsigned long ulDummy;
\r
101 unsigned char ucHeap[ configTOTAL_HEAP_SIZE ];
\r
104 /* Define the linked list structure. This is used to link free blocks in order
\r
105 of their memory address. */
\r
106 typedef struct A_BLOCK_LINK
\r
108 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
\r
109 size_t xBlockSize; /*<< The size of the free block. */
\r
112 /*-----------------------------------------------------------*/
\r
115 * Inserts a block of memory that is being freed into the correct position in
\r
116 * the list of free memory blocks. The block being freed will be merged with
\r
117 * the block in front it and/or the block behind it if the memory blocks are
\r
118 * adjacent to each other.
\r
120 static void prvInsertBlockIntoFreeList( xBlockLink *pxBlockToInsert );
\r
123 * Called automatically to setup the required heap structures the first time
\r
124 * pvPortMalloc() is called.
\r
126 static void prvHeapInit( void );
\r
128 /*-----------------------------------------------------------*/
\r
130 /* The size of the structure placed at the beginning of each allocated memory
\r
131 block must by correctly byte aligned. */
\r
132 static const unsigned short heapSTRUCT_SIZE = ( sizeof( xBlockLink ) + portBYTE_ALIGNMENT - ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) );
\r
134 /* Ensure the pxEnd pointer will end up on the correct byte alignment. */
\r
135 static const size_t xTotalHeapSize = ( ( size_t ) configTOTAL_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );
\r
137 /* Create a couple of list links to mark the start and end of the list. */
\r
138 static xBlockLink xStart, *pxEnd = NULL;
\r
140 /* Keeps track of the number of free bytes remaining, but says nothing about
\r
142 static size_t xFreeBytesRemaining = ( ( size_t ) configTOTAL_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );
\r
144 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */
\r
146 /*-----------------------------------------------------------*/
\r
148 void *pvPortMalloc( size_t xWantedSize )
\r
150 xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
\r
151 void *pvReturn = NULL;
\r
155 /* If this is the first call to malloc then the heap will require
\r
156 initialisation to setup the list of free blocks. */
\r
157 if( pxEnd == NULL )
\r
162 /* The wanted size is increased so it can contain a xBlockLink
\r
163 structure in addition to the requested amount of bytes. */
\r
164 if( xWantedSize > 0 )
\r
166 xWantedSize += heapSTRUCT_SIZE;
\r
168 /* Ensure that blocks are always aligned to the required number of
\r
170 if( xWantedSize & portBYTE_ALIGNMENT_MASK )
\r
172 /* Byte alignment required. */
\r
173 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
\r
177 if( ( xWantedSize > 0 ) && ( xWantedSize < xTotalHeapSize ) )
\r
179 /* Traverse the list from the start (lowest address) block until one
\r
180 of adequate size is found. */
\r
181 pxPreviousBlock = &xStart;
\r
182 pxBlock = xStart.pxNextFreeBlock;
\r
183 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
\r
185 pxPreviousBlock = pxBlock;
\r
186 pxBlock = pxBlock->pxNextFreeBlock;
\r
189 /* If the end marker was reached then a block of adequate size was
\r
191 if( pxBlock != pxEnd )
\r
193 /* Return the memory space - jumping over the xBlockLink structure
\r
195 pvReturn = ( void * ) ( ( ( unsigned char * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
\r
197 /* This block is being returned for use so must be taken out of
\r
198 the list of free blocks. */
\r
199 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
\r
201 /* If the block is larger than required it can be split into two. */
\r
202 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
\r
204 /* This block is to be split into two. Create a new block
\r
205 following the number of bytes requested. The void cast is
\r
206 used to prevent byte alignment warnings from the compiler. */
\r
207 pxNewBlockLink = ( void * ) ( ( ( unsigned char * ) pxBlock ) + xWantedSize );
\r
209 /* Calculate the sizes of two blocks split from the single
\r
211 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
\r
212 pxBlock->xBlockSize = xWantedSize;
\r
214 /* Insert the new block into the list of free blocks. */
\r
215 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
\r
218 xFreeBytesRemaining -= pxBlock->xBlockSize;
\r
224 #if( configUSE_MALLOC_FAILED_HOOK == 1 )
\r
226 if( pvReturn == NULL )
\r
228 extern void vApplicationMallocFailedHook( void );
\r
229 vApplicationMallocFailedHook();
\r
236 /*-----------------------------------------------------------*/
\r
238 void vPortFree( void *pv )
\r
240 unsigned char *puc = ( unsigned char * ) pv;
\r
241 xBlockLink *pxLink;
\r
245 /* The memory being freed will have an xBlockLink structure immediately
\r
247 puc -= heapSTRUCT_SIZE;
\r
249 /* This casting is to keep the compiler from issuing warnings. */
\r
250 pxLink = ( void * ) puc;
\r
254 /* Add this block to the list of free blocks. */
\r
255 xFreeBytesRemaining += pxLink->xBlockSize;
\r
256 prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );
\r
261 /*-----------------------------------------------------------*/
\r
263 size_t xPortGetFreeHeapSize( void )
\r
265 return xFreeBytesRemaining;
\r
267 /*-----------------------------------------------------------*/
\r
269 void vPortInitialiseBlocks( void )
\r
271 /* This just exists to keep the linker quiet. */
\r
273 /*-----------------------------------------------------------*/
\r
275 static void prvHeapInit( void )
\r
277 xBlockLink *pxFirstFreeBlock;
\r
278 unsigned char *pucHeapEnd;
\r
280 /* Ensure the start of the heap is aligned. */
\r
281 configASSERT( ( ( ( unsigned long ) xHeap.ucHeap ) & ( ( unsigned long ) portBYTE_ALIGNMENT_MASK ) ) == 0UL );
\r
283 /* xStart is used to hold a pointer to the first item in the list of free
\r
284 blocks. The void cast is used to prevent compiler warnings. */
\r
285 xStart.pxNextFreeBlock = ( void * ) xHeap.ucHeap;
\r
286 xStart.xBlockSize = ( size_t ) 0;
\r
288 /* pxEnd is used to mark the end of the list of free blocks and is inserted
\r
289 at the end of the heap space. */
\r
290 pucHeapEnd = xHeap.ucHeap + xTotalHeapSize;
\r
291 pucHeapEnd -= heapSTRUCT_SIZE;
\r
292 pxEnd = ( void * ) pucHeapEnd;
\r
293 configASSERT( ( ( ( unsigned long ) pxEnd ) & ( ( unsigned long ) portBYTE_ALIGNMENT_MASK ) ) == 0UL );
\r
294 pxEnd->xBlockSize = 0;
\r
295 pxEnd->pxNextFreeBlock = NULL;
\r
297 /* To start with there is a single free block that is sized to take up the
\r
298 entire heap space, minus the space taken by pxEnd. */
\r
299 pxFirstFreeBlock = ( void * ) xHeap.ucHeap;
\r
300 pxFirstFreeBlock->xBlockSize = xTotalHeapSize - heapSTRUCT_SIZE;
\r
301 pxFirstFreeBlock->pxNextFreeBlock = pxEnd;
\r
303 /* The heap now contains pxEnd. */
\r
304 xFreeBytesRemaining -= heapSTRUCT_SIZE;
\r
306 /*-----------------------------------------------------------*/
\r
308 static void prvInsertBlockIntoFreeList( xBlockLink *pxBlockToInsert )
\r
310 xBlockLink *pxIterator;
\r
311 unsigned char *puc;
\r
313 /* Iterate through the list until a block is found that has a higher address
\r
314 than the block being inserted. */
\r
315 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
\r
317 /* Nothing to do here, just iterate to the right position. */
\r
320 /* Do the block being inserted, and the block it is being inserted after
\r
321 make a contiguous block of memory? */
\r
322 puc = ( unsigned char * ) pxIterator;
\r
323 if( ( puc + pxIterator->xBlockSize ) == ( unsigned char * ) pxBlockToInsert )
\r
325 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
\r
326 pxBlockToInsert = pxIterator;
\r
329 /* Do the block being inserted, and the block it is being inserted before
\r
330 make a contiguous block of memory? */
\r
331 puc = ( unsigned char * ) pxBlockToInsert;
\r
332 if( ( puc + pxBlockToInsert->xBlockSize ) == ( unsigned char * ) pxIterator->pxNextFreeBlock )
\r
334 if( pxIterator->pxNextFreeBlock != pxEnd )
\r
336 /* Form one big block from the two blocks. */
\r
337 pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
\r
338 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
\r
342 pxBlockToInsert->pxNextFreeBlock = pxEnd;
\r
347 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
\r
350 /* If the block being inserted plugged a gab, so was merged with the block
\r
351 before and the block after, then it's pxNextFreeBlock pointer will have
\r
352 already been set, and should not be set here as that would make it point
\r
354 if( pxIterator != pxBlockToInsert )
\r
356 pxIterator->pxNextFreeBlock = pxBlockToInsert;
\r