2 FreeRTOS V7.4.1 - Copyright (C) 2013 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
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 * A sample implementation of pvPortMalloc() and vPortFree() that permits
\r
77 * allocated blocks to be freed, but does not combine adjacent free blocks
\r
78 * into a single larger block (and so will fragment memory). See heap_4.c for
\r
79 * an aquivalent that does combine adjacent blocks into single larger blocks.
\r
81 * See heap_1.c, heap_3.c and heap_4.c for alternative implementations, and the
\r
82 * memory management pages of http://www.FreeRTOS.org for more information.
\r
86 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
87 all the API functions to use the MPU wrappers. That should only be done when
\r
88 task.h is included from an application file. */
\r
89 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
91 #include "FreeRTOS.h"
\r
94 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
96 /* A few bytes might be lost to byte aligning the heap start address. */
\r
97 #define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
\r
100 * Initialises the heap structures before their first use.
\r
102 static void prvHeapInit( void );
\r
104 /* Allocate the memory for the heap. */
\r
105 static unsigned char ucHeap[ configTOTAL_HEAP_SIZE ];
\r
107 /* Define the linked list structure. This is used to link free blocks in order
\r
109 typedef struct A_BLOCK_LINK
\r
111 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
\r
112 size_t xBlockSize; /*<< The size of the free block. */
\r
116 static const unsigned short heapSTRUCT_SIZE = ( sizeof( xBlockLink ) + portBYTE_ALIGNMENT - ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) );
\r
117 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )
\r
119 /* Create a couple of list links to mark the start and end of the list. */
\r
120 static xBlockLink xStart, xEnd;
\r
122 /* Keeps track of the number of free bytes remaining, but says nothing about
\r
124 static size_t xFreeBytesRemaining = configADJUSTED_HEAP_SIZE;
\r
126 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */
\r
129 * Insert a block into the list of free blocks - which is ordered by size of
\r
130 * the block. Small blocks at the start of the list and large blocks at the end
\r
133 #define prvInsertBlockIntoFreeList( pxBlockToInsert ) \
\r
135 xBlockLink *pxIterator; \
\r
136 size_t xBlockSize; \
\r
138 xBlockSize = pxBlockToInsert->xBlockSize; \
\r
140 /* Iterate through the list until a block is found that has a larger size */ \
\r
141 /* than the block we are inserting. */ \
\r
142 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock ) \
\r
144 /* There is nothing to do here - just iterate to the correct position. */ \
\r
147 /* Update the list to include the block being inserted in the correct */ \
\r
149 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock; \
\r
150 pxIterator->pxNextFreeBlock = pxBlockToInsert; \
\r
152 /*-----------------------------------------------------------*/
\r
154 void *pvPortMalloc( size_t xWantedSize )
\r
156 xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
\r
157 static portBASE_TYPE xHeapHasBeenInitialised = pdFALSE;
\r
158 void *pvReturn = NULL;
\r
162 /* If this is the first call to malloc then the heap will require
\r
163 initialisation to setup the list of free blocks. */
\r
164 if( xHeapHasBeenInitialised == pdFALSE )
\r
167 xHeapHasBeenInitialised = pdTRUE;
\r
170 /* The wanted size is increased so it can contain a xBlockLink
\r
171 structure in addition to the requested amount of bytes. */
\r
172 if( xWantedSize > 0 )
\r
174 xWantedSize += heapSTRUCT_SIZE;
\r
176 /* Ensure that blocks are always aligned to the required number of bytes. */
\r
177 if( xWantedSize & portBYTE_ALIGNMENT_MASK )
\r
179 /* Byte alignment required. */
\r
180 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
\r
184 if( ( xWantedSize > 0 ) && ( xWantedSize < configADJUSTED_HEAP_SIZE ) )
\r
186 /* Blocks are stored in byte order - traverse the list from the start
\r
187 (smallest) block until one of adequate size is found. */
\r
188 pxPreviousBlock = &xStart;
\r
189 pxBlock = xStart.pxNextFreeBlock;
\r
190 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
\r
192 pxPreviousBlock = pxBlock;
\r
193 pxBlock = pxBlock->pxNextFreeBlock;
\r
196 /* If we found the end marker then a block of adequate size was not found. */
\r
197 if( pxBlock != &xEnd )
\r
199 /* Return the memory space - jumping over the xBlockLink structure
\r
201 pvReturn = ( void * ) ( ( ( unsigned char * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
\r
203 /* This block is being returned for use so must be taken out of the
\r
204 list of free blocks. */
\r
205 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
\r
207 /* If the block is larger than required it can be split into two. */
\r
208 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
\r
210 /* This block is to be split into two. Create a new block
\r
211 following the number of bytes requested. The void cast is
\r
212 used to prevent byte alignment warnings from the compiler. */
\r
213 pxNewBlockLink = ( void * ) ( ( ( unsigned char * ) pxBlock ) + xWantedSize );
\r
215 /* Calculate the sizes of two blocks split from the single
\r
217 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
\r
218 pxBlock->xBlockSize = xWantedSize;
\r
220 /* Insert the new block into the list of free blocks. */
\r
221 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
\r
224 xFreeBytesRemaining -= pxBlock->xBlockSize;
\r
230 #if( configUSE_MALLOC_FAILED_HOOK == 1 )
\r
232 if( pvReturn == NULL )
\r
234 extern void vApplicationMallocFailedHook( void );
\r
235 vApplicationMallocFailedHook();
\r
242 /*-----------------------------------------------------------*/
\r
244 void vPortFree( void *pv )
\r
246 unsigned char *puc = ( unsigned char * ) pv;
\r
247 xBlockLink *pxLink;
\r
251 /* The memory being freed will have an xBlockLink structure immediately
\r
253 puc -= heapSTRUCT_SIZE;
\r
255 /* This unexpected casting is to keep some compilers from issuing
\r
256 byte alignment warnings. */
\r
257 pxLink = ( void * ) puc;
\r
261 /* Add this block to the list of free blocks. */
\r
262 prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );
\r
263 xFreeBytesRemaining += pxLink->xBlockSize;
\r
268 /*-----------------------------------------------------------*/
\r
270 size_t xPortGetFreeHeapSize( void )
\r
272 return xFreeBytesRemaining;
\r
274 /*-----------------------------------------------------------*/
\r
276 void vPortInitialiseBlocks( void )
\r
278 /* This just exists to keep the linker quiet. */
\r
280 /*-----------------------------------------------------------*/
\r
282 static void prvHeapInit( void )
\r
284 xBlockLink *pxFirstFreeBlock;
\r
285 unsigned char *pucAlignedHeap;
\r
287 /* Ensure the heap starts on a correctly aligned boundary. */
\r
288 pucAlignedHeap = ( unsigned char * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK ) );
\r
290 /* xStart is used to hold a pointer to the first item in the list of free
\r
291 blocks. The void cast is used to prevent compiler warnings. */
\r
292 xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
\r
293 xStart.xBlockSize = ( size_t ) 0;
\r
295 /* xEnd is used to mark the end of the list of free blocks. */
\r
296 xEnd.xBlockSize = configADJUSTED_HEAP_SIZE;
\r
297 xEnd.pxNextFreeBlock = NULL;
\r
299 /* To start with there is a single free block that is sized to take up the
\r
300 entire heap space. */
\r
301 pxFirstFreeBlock = ( void * ) pucAlignedHeap;
\r
302 pxFirstFreeBlock->xBlockSize = configADJUSTED_HEAP_SIZE;
\r
303 pxFirstFreeBlock->pxNextFreeBlock = &xEnd;
\r
305 /*-----------------------------------------------------------*/
\r