2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 * A sample implementation of pvPortMalloc() and vPortFree() that combines
\r
68 * (coalescences) adjacent memory blocks as they are freed, and in so doing
\r
69 * limits memory fragmentation.
\r
71 * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the
\r
72 * memory management pages of http://www.FreeRTOS.org for more information.
\r
76 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
77 all the API functions to use the MPU wrappers. That should only be done when
\r
78 task.h is included from an application file. */
\r
79 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
81 #include "FreeRTOS.h"
\r
84 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
86 /* Block sizes must not get too small. */
\r
87 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )
\r
89 /* Assumes 8bit bytes! */
\r
90 #define heapBITS_PER_BYTE ( ( size_t ) 8 )
\r
92 /* A few bytes might be lost to byte aligning the heap start address. */
\r
93 #define heapADJUSTED_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 - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );
\r
128 /* Ensure the pxEnd pointer will end up on the correct byte alignment. */
\r
129 static const size_t xTotalHeapSize = ( ( size_t ) heapADJUSTED_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 ) heapADJUSTED_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );
\r
138 /* Gets set to the top bit of an size_t type. When this bit in the xBlockSize
\r
139 member of an xBlockLink structure is set then the block belongs to the
\r
140 application. When the bit is free the block is still part of the free heap
\r
142 static size_t xBlockAllocatedBit = 0;
\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 /* Check the requested block size is not so large that the top bit is
\r
161 set. The top bit of the block size member of the xBlockLink structure
\r
162 is used to determine who owns the block - the application or the
\r
163 kernel, so it must be free. */
\r
164 if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
\r
166 /* The wanted size is increased so it can contain a xBlockLink
\r
167 structure in addition to the requested amount of bytes. */
\r
168 if( xWantedSize > 0 )
\r
170 xWantedSize += heapSTRUCT_SIZE;
\r
172 /* Ensure that blocks are always aligned to the required number
\r
174 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
\r
176 /* Byte alignment required. */
\r
177 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
\r
181 if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
\r
183 /* Traverse the list from the start (lowest address) block until
\r
184 one of adequate size is found. */
\r
185 pxPreviousBlock = &xStart;
\r
186 pxBlock = xStart.pxNextFreeBlock;
\r
187 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
\r
189 pxPreviousBlock = pxBlock;
\r
190 pxBlock = pxBlock->pxNextFreeBlock;
\r
193 /* If the end marker was reached then a block of adequate size
\r
195 if( pxBlock != pxEnd )
\r
197 /* Return the memory space pointed to - jumping over the
\r
198 xBlockLink structure at its start. */
\r
199 pvReturn = ( void * ) ( ( ( unsigned char * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
\r
201 /* This block is being returned for use so must be taken out
\r
202 of the list of free blocks. */
\r
203 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
\r
205 /* If the block is larger than required it can be split into
\r
207 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
\r
209 /* This block is to be split into two. Create a new
\r
210 block following the number of bytes requested. The void
\r
211 cast is used to prevent byte alignment warnings from the
\r
213 pxNewBlockLink = ( void * ) ( ( ( unsigned char * ) pxBlock ) + xWantedSize );
\r
215 /* Calculate the sizes of two blocks split from the
\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
226 /* The block is being returned - it is allocated and owned
\r
227 by the application and has no "next" block. */
\r
228 pxBlock->xBlockSize |= xBlockAllocatedBit;
\r
229 pxBlock->pxNextFreeBlock = NULL;
\r
234 traceMALLOC( pvReturn, xWantedSize );
\r
238 #if( configUSE_MALLOC_FAILED_HOOK == 1 )
\r
240 if( pvReturn == NULL )
\r
242 extern void vApplicationMallocFailedHook( void );
\r
243 vApplicationMallocFailedHook();
\r
250 /*-----------------------------------------------------------*/
\r
252 void vPortFree( void *pv )
\r
254 unsigned char *puc = ( unsigned char * ) pv;
\r
255 xBlockLink *pxLink;
\r
259 /* The memory being freed will have an xBlockLink structure immediately
\r
261 puc -= heapSTRUCT_SIZE;
\r
263 /* This casting is to keep the compiler from issuing warnings. */
\r
264 pxLink = ( void * ) puc;
\r
266 /* Check the block is actually allocated. */
\r
267 configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
\r
268 configASSERT( pxLink->pxNextFreeBlock == NULL );
\r
270 if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )
\r
272 if( pxLink->pxNextFreeBlock == NULL )
\r
274 /* The block is being returned to the heap - it is no longer
\r
276 pxLink->xBlockSize &= ~xBlockAllocatedBit;
\r
280 /* Add this block to the list of free blocks. */
\r
281 xFreeBytesRemaining += pxLink->xBlockSize;
\r
282 prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );
\r
283 traceFREE( pv, pxLink->xBlockSize );
\r
290 /*-----------------------------------------------------------*/
\r
292 size_t xPortGetFreeHeapSize( void )
\r
294 return xFreeBytesRemaining;
\r
296 /*-----------------------------------------------------------*/
\r
298 void vPortInitialiseBlocks( void )
\r
300 /* This just exists to keep the linker quiet. */
\r
302 /*-----------------------------------------------------------*/
\r
304 static void prvHeapInit( void )
\r
306 xBlockLink *pxFirstFreeBlock;
\r
307 unsigned char *pucHeapEnd, *pucAlignedHeap;
\r
309 /* Ensure the heap starts on a correctly aligned boundary. */
\r
310 pucAlignedHeap = ( unsigned char * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK ) );
\r
312 /* xStart is used to hold a pointer to the first item in the list of free
\r
313 blocks. The void cast is used to prevent compiler warnings. */
\r
314 xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
\r
315 xStart.xBlockSize = ( size_t ) 0;
\r
317 /* pxEnd is used to mark the end of the list of free blocks and is inserted
\r
318 at the end of the heap space. */
\r
319 pucHeapEnd = pucAlignedHeap + xTotalHeapSize;
\r
320 pucHeapEnd -= heapSTRUCT_SIZE;
\r
321 pxEnd = ( void * ) pucHeapEnd;
\r
322 configASSERT( ( ( ( unsigned long ) pxEnd ) & ( ( unsigned long ) portBYTE_ALIGNMENT_MASK ) ) == 0UL );
\r
323 pxEnd->xBlockSize = 0;
\r
324 pxEnd->pxNextFreeBlock = NULL;
\r
326 /* To start with there is a single free block that is sized to take up the
\r
327 entire heap space, minus the space taken by pxEnd. */
\r
328 pxFirstFreeBlock = ( void * ) pucAlignedHeap;
\r
329 pxFirstFreeBlock->xBlockSize = xTotalHeapSize - heapSTRUCT_SIZE;
\r
330 pxFirstFreeBlock->pxNextFreeBlock = pxEnd;
\r
332 /* The heap now contains pxEnd. */
\r
333 xFreeBytesRemaining -= heapSTRUCT_SIZE;
\r
335 /* Work out the position of the top bit in a size_t variable. */
\r
336 xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );
\r
338 /*-----------------------------------------------------------*/
\r
340 static void prvInsertBlockIntoFreeList( xBlockLink *pxBlockToInsert )
\r
342 xBlockLink *pxIterator;
\r
343 unsigned char *puc;
\r
345 /* Iterate through the list until a block is found that has a higher address
\r
346 than the block being inserted. */
\r
347 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
\r
349 /* Nothing to do here, just iterate to the right position. */
\r
352 /* Do the block being inserted, and the block it is being inserted after
\r
353 make a contiguous block of memory? */
\r
354 puc = ( unsigned char * ) pxIterator;
\r
355 if( ( puc + pxIterator->xBlockSize ) == ( unsigned char * ) pxBlockToInsert )
\r
357 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
\r
358 pxBlockToInsert = pxIterator;
\r
361 /* Do the block being inserted, and the block it is being inserted before
\r
362 make a contiguous block of memory? */
\r
363 puc = ( unsigned char * ) pxBlockToInsert;
\r
364 if( ( puc + pxBlockToInsert->xBlockSize ) == ( unsigned char * ) pxIterator->pxNextFreeBlock )
\r
366 if( pxIterator->pxNextFreeBlock != pxEnd )
\r
368 /* Form one big block from the two blocks. */
\r
369 pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
\r
370 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
\r
374 pxBlockToInsert->pxNextFreeBlock = pxEnd;
\r
379 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
\r
382 /* If the block being inserted plugged a gab, so was merged with the block
\r
383 before and the block after, then it's pxNextFreeBlock pointer will have
\r
384 already been set, and should not be set here as that would make it point
\r
386 if( pxIterator != pxBlockToInsert )
\r
388 pxIterator->pxNextFreeBlock = pxBlockToInsert;
\r