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 /* A few bytes might be lost to byte aligning the heap start address. */
\r
93 #define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
\r
95 /* Allocate the memory for the heap. */
\r
96 static unsigned char ucHeap[ configTOTAL_HEAP_SIZE ];
\r
98 /* Define the linked list structure. This is used to link free blocks in order
\r
99 of their memory address. */
\r
100 typedef struct A_BLOCK_LINK
\r
102 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
\r
103 size_t xBlockSize; /*<< The size of the free block. */
\r
106 /*-----------------------------------------------------------*/
\r
109 * Inserts a block of memory that is being freed into the correct position in
\r
110 * the list of free memory blocks. The block being freed will be merged with
\r
111 * the block in front it and/or the block behind it if the memory blocks are
\r
112 * adjacent to each other.
\r
114 static void prvInsertBlockIntoFreeList( xBlockLink *pxBlockToInsert );
\r
117 * Called automatically to setup the required heap structures the first time
\r
118 * pvPortMalloc() is called.
\r
120 static void prvHeapInit( void );
\r
122 /*-----------------------------------------------------------*/
\r
124 /* The size of the structure placed at the beginning of each allocated memory
\r
125 block must by correctly byte aligned. */
\r
126 static const unsigned short heapSTRUCT_SIZE = ( sizeof( xBlockLink ) + portBYTE_ALIGNMENT - ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) );
\r
128 /* Ensure the pxEnd pointer will end up on the correct byte alignment. */
\r
129 static const size_t xTotalHeapSize = ( ( size_t ) configADJUSTED_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );
\r
131 /* Create a couple of list links to mark the start and end of the list. */
\r
132 static xBlockLink xStart, *pxEnd = NULL;
\r
134 /* Keeps track of the number of free bytes remaining, but says nothing about
\r
136 static size_t xFreeBytesRemaining = ( ( size_t ) configADJUSTED_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );
\r
138 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */
\r
140 /*-----------------------------------------------------------*/
\r
142 void *pvPortMalloc( size_t xWantedSize )
\r
144 xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
\r
145 void *pvReturn = NULL;
\r
149 /* If this is the first call to malloc then the heap will require
\r
150 initialisation to setup the list of free blocks. */
\r
151 if( pxEnd == NULL )
\r
156 /* The wanted size is increased so it can contain a xBlockLink
\r
157 structure in addition to the requested amount of bytes. */
\r
158 if( xWantedSize > 0 )
\r
160 xWantedSize += heapSTRUCT_SIZE;
\r
162 /* Ensure that blocks are always aligned to the required number of
\r
164 if( xWantedSize & portBYTE_ALIGNMENT_MASK )
\r
166 /* Byte alignment required. */
\r
167 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
\r
171 if( ( xWantedSize > 0 ) && ( xWantedSize < xTotalHeapSize ) )
\r
173 /* Traverse the list from the start (lowest address) block until one
\r
174 of adequate size is found. */
\r
175 pxPreviousBlock = &xStart;
\r
176 pxBlock = xStart.pxNextFreeBlock;
\r
177 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
\r
179 pxPreviousBlock = pxBlock;
\r
180 pxBlock = pxBlock->pxNextFreeBlock;
\r
183 /* If the end marker was reached then a block of adequate size was
\r
185 if( pxBlock != pxEnd )
\r
187 /* Return the memory space - jumping over the xBlockLink structure
\r
189 pvReturn = ( void * ) ( ( ( unsigned char * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
\r
191 /* This block is being returned for use so must be taken out of
\r
192 the list of free blocks. */
\r
193 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
\r
195 /* If the block is larger than required it can be split into two. */
\r
196 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
\r
198 /* This block is to be split into two. Create a new block
\r
199 following the number of bytes requested. The void cast is
\r
200 used to prevent byte alignment warnings from the compiler. */
\r
201 pxNewBlockLink = ( void * ) ( ( ( unsigned char * ) pxBlock ) + xWantedSize );
\r
203 /* Calculate the sizes of two blocks split from the single
\r
205 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
\r
206 pxBlock->xBlockSize = xWantedSize;
\r
208 /* Insert the new block into the list of free blocks. */
\r
209 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
\r
212 xFreeBytesRemaining -= pxBlock->xBlockSize;
\r
218 #if( configUSE_MALLOC_FAILED_HOOK == 1 )
\r
220 if( pvReturn == NULL )
\r
222 extern void vApplicationMallocFailedHook( void );
\r
223 vApplicationMallocFailedHook();
\r
230 /*-----------------------------------------------------------*/
\r
232 void vPortFree( void *pv )
\r
234 unsigned char *puc = ( unsigned char * ) pv;
\r
235 xBlockLink *pxLink;
\r
239 /* The memory being freed will have an xBlockLink structure immediately
\r
241 puc -= heapSTRUCT_SIZE;
\r
243 /* This casting is to keep the compiler from issuing warnings. */
\r
244 pxLink = ( void * ) puc;
\r
248 /* Add this block to the list of free blocks. */
\r
249 xFreeBytesRemaining += pxLink->xBlockSize;
\r
250 prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );
\r
255 /*-----------------------------------------------------------*/
\r
257 size_t xPortGetFreeHeapSize( void )
\r
259 return xFreeBytesRemaining;
\r
261 /*-----------------------------------------------------------*/
\r
263 void vPortInitialiseBlocks( void )
\r
265 /* This just exists to keep the linker quiet. */
\r
267 /*-----------------------------------------------------------*/
\r
269 static void prvHeapInit( void )
\r
271 xBlockLink *pxFirstFreeBlock;
\r
272 unsigned char *pucHeapEnd, *pucAlignedHeap;
\r
274 /* Ensure the heap starts on a correctly aligned boundary. */
\r
275 pucAlignedHeap = ( unsigned char * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK ) );
\r
277 /* xStart is used to hold a pointer to the first item in the list of free
\r
278 blocks. The void cast is used to prevent compiler warnings. */
\r
279 xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
\r
280 xStart.xBlockSize = ( size_t ) 0;
\r
282 /* pxEnd is used to mark the end of the list of free blocks and is inserted
\r
283 at the end of the heap space. */
\r
284 pucHeapEnd = pucAlignedHeap + xTotalHeapSize;
\r
285 pucHeapEnd -= heapSTRUCT_SIZE;
\r
286 pxEnd = ( void * ) pucHeapEnd;
\r
287 configASSERT( ( ( ( unsigned long ) pxEnd ) & ( ( unsigned long ) portBYTE_ALIGNMENT_MASK ) ) == 0UL );
\r
288 pxEnd->xBlockSize = 0;
\r
289 pxEnd->pxNextFreeBlock = NULL;
\r
291 /* To start with there is a single free block that is sized to take up the
\r
292 entire heap space, minus the space taken by pxEnd. */
\r
293 pxFirstFreeBlock = ( void * ) pucAlignedHeap;
\r
294 pxFirstFreeBlock->xBlockSize = xTotalHeapSize - heapSTRUCT_SIZE;
\r
295 pxFirstFreeBlock->pxNextFreeBlock = pxEnd;
\r
297 /* The heap now contains pxEnd. */
\r
298 xFreeBytesRemaining -= heapSTRUCT_SIZE;
\r
300 /*-----------------------------------------------------------*/
\r
302 static void prvInsertBlockIntoFreeList( xBlockLink *pxBlockToInsert )
\r
304 xBlockLink *pxIterator;
\r
305 unsigned char *puc;
\r
307 /* Iterate through the list until a block is found that has a higher address
\r
308 than the block being inserted. */
\r
309 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
\r
311 /* Nothing to do here, just iterate to the right position. */
\r
314 /* Do the block being inserted, and the block it is being inserted after
\r
315 make a contiguous block of memory? */
\r
316 puc = ( unsigned char * ) pxIterator;
\r
317 if( ( puc + pxIterator->xBlockSize ) == ( unsigned char * ) pxBlockToInsert )
\r
319 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
\r
320 pxBlockToInsert = pxIterator;
\r
323 /* Do the block being inserted, and the block it is being inserted before
\r
324 make a contiguous block of memory? */
\r
325 puc = ( unsigned char * ) pxBlockToInsert;
\r
326 if( ( puc + pxBlockToInsert->xBlockSize ) == ( unsigned char * ) pxIterator->pxNextFreeBlock )
\r
328 if( pxIterator->pxNextFreeBlock != pxEnd )
\r
330 /* Form one big block from the two blocks. */
\r
331 pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
\r
332 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
\r
336 pxBlockToInsert->pxNextFreeBlock = pxEnd;
\r
341 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
\r
344 /* If the block being inserted plugged a gab, so was merged with the block
\r
345 before and the block after, then it's pxNextFreeBlock pointer will have
\r
346 already been set, and should not be set here as that would make it point
\r
348 if( pxIterator != pxBlockToInsert )
\r
350 pxIterator->pxNextFreeBlock = pxBlockToInsert;
\r