2 FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\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 exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 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 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * A sample implementation of pvPortMalloc() and vPortFree() that permits
\r
56 * allocated blocks to be freed, but does not combine adjacent free blocks
\r
57 * into a single larger block.
\r
59 * See heap_1.c and heap_3.c for alternative implementations, and the memory
\r
60 * management pages of http://www.FreeRTOS.org for more information.
\r
64 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
65 all the API functions to use the MPU wrappers. That should only be done when
\r
66 task.h is included from an application file. */
\r
67 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
69 #include "FreeRTOS.h"
\r
72 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
74 /* Allocate the memory for the heap. The struct is used to force byte
\r
75 alignment without using any non-portable code. */
\r
76 static union xRTOS_HEAP
\r
78 #if portBYTE_ALIGNMENT == 8
\r
79 volatile portDOUBLE dDummy;
\r
81 volatile unsigned long ulDummy;
\r
83 unsigned char ucHeap[ configTOTAL_HEAP_SIZE ];
\r
86 /* Define the linked list structure. This is used to link free blocks in order
\r
88 typedef struct A_BLOCK_LINK
\r
90 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
\r
91 size_t xBlockSize; /*<< The size of the free block. */
\r
95 static const unsigned short heapSTRUCT_SIZE = ( sizeof( xBlockLink ) + portBYTE_ALIGNMENT - ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) );
\r
96 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )
\r
98 /* Create a couple of list links to mark the start and end of the list. */
\r
99 static xBlockLink xStart, xEnd;
\r
101 /* Keeps track of the number of free bytes remaining, but says nothing about
\r
103 static size_t xFreeBytesRemaining = configTOTAL_HEAP_SIZE;
\r
105 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */
\r
108 * Insert a block into the list of free blocks - which is ordered by size of
\r
109 * the block. Small blocks at the start of the list and large blocks at the end
\r
112 #define prvInsertBlockIntoFreeList( pxBlockToInsert ) \
\r
114 xBlockLink *pxIterator; \
\r
115 size_t xBlockSize; \
\r
117 xBlockSize = pxBlockToInsert->xBlockSize; \
\r
119 /* Iterate through the list until a block is found that has a larger size */ \
\r
120 /* than the block we are inserting. */ \
\r
121 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock ) \
\r
123 /* There is nothing to do here - just iterate to the correct position. */ \
\r
126 /* Update the list to include the block being inserted in the correct */ \
\r
128 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock; \
\r
129 pxIterator->pxNextFreeBlock = pxBlockToInsert; \
\r
131 /*-----------------------------------------------------------*/
\r
133 #define prvHeapInit() \
\r
135 xBlockLink *pxFirstFreeBlock; \
\r
137 /* xStart is used to hold a pointer to the first item in the list of free */ \
\r
138 /* blocks. The void cast is used to prevent compiler warnings. */ \
\r
139 xStart.pxNextFreeBlock = ( void * ) xHeap.ucHeap; \
\r
140 xStart.xBlockSize = ( size_t ) 0; \
\r
142 /* xEnd is used to mark the end of the list of free blocks. */ \
\r
143 xEnd.xBlockSize = configTOTAL_HEAP_SIZE; \
\r
144 xEnd.pxNextFreeBlock = NULL; \
\r
146 /* To start with there is a single free block that is sized to take up the \
\r
147 entire heap space. */ \
\r
148 pxFirstFreeBlock = ( void * ) xHeap.ucHeap; \
\r
149 pxFirstFreeBlock->xBlockSize = configTOTAL_HEAP_SIZE; \
\r
150 pxFirstFreeBlock->pxNextFreeBlock = &xEnd; \
\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 < configTOTAL_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 ) )
\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 our 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 casting is to keep the compiler from issuing warnings. */
\r
256 pxLink = ( void * ) puc;
\r
260 /* Add this block to the list of free blocks. */
\r
261 prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );
\r
262 xFreeBytesRemaining += pxLink->xBlockSize;
\r
267 /*-----------------------------------------------------------*/
\r
269 size_t xPortGetFreeHeapSize( void )
\r
271 return xFreeBytesRemaining;
\r
273 /*-----------------------------------------------------------*/
\r
275 void vPortInitialiseBlocks( void )
\r
277 /* This just exists to keep the linker quiet. */
\r