2 FreeRTOS.org V5.3.1 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 * A sample implementation of pvPortMalloc() and vPortFree() that permits
\r
54 * allocated blocks to be freed, but does not combine adjacent free blocks
\r
55 * into a single larger block.
\r
57 * See heap_1.c and heap_3.c for alternative implementations, and the memory
\r
58 * management pages of http://www.FreeRTOS.org for more information.
\r
62 #include "FreeRTOS.h"
\r
65 /* Setup the correct byte alignment mask for the defined byte alignment. */
\r
67 #if portBYTE_ALIGNMENT == 8
\r
68 #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0007 )
\r
71 #if portBYTE_ALIGNMENT == 4
\r
72 #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0003 )
\r
75 #if portBYTE_ALIGNMENT == 2
\r
76 #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0001 )
\r
79 #if portBYTE_ALIGNMENT == 1
\r
80 #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0000 )
\r
83 #ifndef heapBYTE_ALIGNMENT_MASK
\r
84 #error "Invalid portBYTE_ALIGNMENT definition"
\r
87 /* Allocate the memory for the heap. The struct is used to force byte
\r
88 alignment without using any non-portable code. */
\r
89 static union xRTOS_HEAP
\r
91 #if portBYTE_ALIGNMENT == 8
\r
92 volatile portDOUBLE dDummy;
\r
94 volatile unsigned portLONG ulDummy;
\r
96 unsigned portCHAR ucHeap[ configTOTAL_HEAP_SIZE ];
\r
99 /* Define the linked list structure. This is used to link free blocks in order
\r
101 typedef struct A_BLOCK_LINK
\r
103 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
\r
104 size_t xBlockSize; /*<< The size of the free block. */
\r
108 static const unsigned portSHORT heapSTRUCT_SIZE = ( sizeof( xBlockLink ) + portBYTE_ALIGNMENT - ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) );
\r
109 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )
\r
111 /* Create a couple of list links to mark the start and end of the list. */
\r
112 static xBlockLink xStart, xEnd;
\r
114 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */
\r
117 * Insert a block into the list of free blocks - which is ordered by size of
\r
118 * the block. Small blocks at the start of the list and large blocks at the end
\r
121 #define prvInsertBlockIntoFreeList( pxBlockToInsert ) \
\r
123 xBlockLink *pxIterator; \
\r
124 size_t xBlockSize; \
\r
126 xBlockSize = pxBlockToInsert->xBlockSize; \
\r
128 /* Iterate through the list until a block is found that has a larger size */ \
\r
129 /* than the block we are inserting. */ \
\r
130 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock ) \
\r
132 /* There is nothing to do here - just iterate to the correct position. */ \
\r
135 /* Update the list to include the block being inserted in the correct */ \
\r
137 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock; \
\r
138 pxIterator->pxNextFreeBlock = pxBlockToInsert; \
\r
140 /*-----------------------------------------------------------*/
\r
142 #define prvHeapInit() \
\r
144 xBlockLink *pxFirstFreeBlock; \
\r
146 /* xStart is used to hold a pointer to the first item in the list of free */ \
\r
147 /* blocks. The void cast is used to prevent compiler warnings. */ \
\r
148 xStart.pxNextFreeBlock = ( void * ) xHeap.ucHeap; \
\r
149 xStart.xBlockSize = ( size_t ) 0; \
\r
151 /* xEnd is used to mark the end of the list of free blocks. */ \
\r
152 xEnd.xBlockSize = configTOTAL_HEAP_SIZE; \
\r
153 xEnd.pxNextFreeBlock = NULL; \
\r
155 /* To start with there is a single free block that is sized to take up the \
\r
156 entire heap space. */ \
\r
157 pxFirstFreeBlock = ( void * ) xHeap.ucHeap; \
\r
158 pxFirstFreeBlock->xBlockSize = configTOTAL_HEAP_SIZE; \
\r
159 pxFirstFreeBlock->pxNextFreeBlock = &xEnd; \
\r
161 /*-----------------------------------------------------------*/
\r
163 void *pvPortMalloc( size_t xWantedSize )
\r
165 xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
\r
166 static portBASE_TYPE xHeapHasBeenInitialised = pdFALSE;
\r
167 void *pvReturn = NULL;
\r
171 /* If this is the first call to malloc then the heap will require
\r
172 initialisation to setup the list of free blocks. */
\r
173 if( xHeapHasBeenInitialised == pdFALSE )
\r
176 xHeapHasBeenInitialised = pdTRUE;
\r
179 /* The wanted size is increased so it can contain a xBlockLink
\r
180 structure in addition to the requested amount of bytes. */
\r
181 if( xWantedSize > 0 )
\r
183 xWantedSize += heapSTRUCT_SIZE;
\r
185 /* Ensure that blocks are always aligned to the required number of bytes. */
\r
186 if( xWantedSize & heapBYTE_ALIGNMENT_MASK )
\r
188 /* Byte alignment required. */
\r
189 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & heapBYTE_ALIGNMENT_MASK ) );
\r
193 if( ( xWantedSize > 0 ) && ( xWantedSize < configTOTAL_HEAP_SIZE ) )
\r
195 /* Blocks are stored in byte order - traverse the list from the start
\r
196 (smallest) block until one of adequate size is found. */
\r
197 pxPreviousBlock = &xStart;
\r
198 pxBlock = xStart.pxNextFreeBlock;
\r
199 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock ) )
\r
201 pxPreviousBlock = pxBlock;
\r
202 pxBlock = pxBlock->pxNextFreeBlock;
\r
205 /* If we found the end marker then a block of adequate size was not found. */
\r
206 if( pxBlock != &xEnd )
\r
208 /* Return the memory space - jumping over the xBlockLink structure
\r
210 pvReturn = ( void * ) ( ( ( unsigned portCHAR * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
\r
212 /* This block is being returned for use so must be taken our of the
\r
213 list of free blocks. */
\r
214 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
\r
216 /* If the block is larger than required it can be split into two. */
\r
217 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
\r
219 /* This block is to be split into two. Create a new block
\r
220 following the number of bytes requested. The void cast is
\r
221 used to prevent byte alignment warnings from the compiler. */
\r
222 pxNewBlockLink = ( void * ) ( ( ( unsigned portCHAR * ) pxBlock ) + xWantedSize );
\r
224 /* Calculate the sizes of two blocks split from the single
\r
226 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
\r
227 pxBlock->xBlockSize = xWantedSize;
\r
229 /* Insert the new block into the list of free blocks. */
\r
230 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
\r
239 /*-----------------------------------------------------------*/
\r
241 void vPortFree( void *pv )
\r
243 unsigned portCHAR *puc = ( unsigned portCHAR * ) pv;
\r
244 xBlockLink *pxLink;
\r
248 /* The memory being freed will have an xBlockLink structure immediately
\r
250 puc -= heapSTRUCT_SIZE;
\r
252 /* This casting is to keep the compiler from issuing warnings. */
\r
253 pxLink = ( void * ) puc;
\r
257 /* Add this block to the list of free blocks. */
\r
258 prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );
\r
263 /*-----------------------------------------------------------*/
\r