2 FreeRTOS.org V4.3.1 - Copyright (C) 2003-2007 Richard Barry.
4 This file is part of the FreeRTOS.org distribution.
6 FreeRTOS.org is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 FreeRTOS.org is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with FreeRTOS.org; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 A special exception to the GPL can be applied should you wish to distribute
21 a combined work that includes FreeRTOS.org, without being obliged to provide
22 the source code for any proprietary components. See the licensing section
23 of http://www.FreeRTOS.org for full details of how and when the exception
26 ***************************************************************************
27 See http://www.FreeRTOS.org for documentation, latest information, license
28 and contact details. Please ensure to read the configuration and relevant
29 port sections of the online documentation.
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
32 with commercial development and support options.
33 ***************************************************************************
37 * A sample implementation of pvPortMalloc() and vPortFree() that permits
38 * allocated blocks to be freed, but does not combine adjacent free blocks
39 * into a single larger block.
41 * See heap_1.c and heap_3.c for alternative implementations, and the memory
42 * management pages of http://www.FreeRTOS.org for more information.
49 /* Setup the correct byte alignment mask for the defined byte alignment. */
51 #if portBYTE_ALIGNMENT == 8
52 #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0007 )
55 #if portBYTE_ALIGNMENT == 4
56 #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0003 )
59 #if portBYTE_ALIGNMENT == 2
60 #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0001 )
63 #if portBYTE_ALIGNMENT == 1
64 #define heapBYTE_ALIGNMENT_MASK ( ( size_t ) 0x0000 )
67 #ifndef heapBYTE_ALIGNMENT_MASK
68 #error "Invalid portBYTE_ALIGNMENT definition"
71 /* Allocate the memory for the heap. The struct is used to force byte
72 alignment without using any non-portable code. */
73 static struct xRTOS_HEAP
75 unsigned portLONG ulDummy;
76 unsigned portCHAR ucHeap[ configTOTAL_HEAP_SIZE ];
79 /* Define the linked list structure. This is used to link free blocks in order
81 typedef struct A_BLOCK_LINK
83 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
84 size_t xBlockSize; /*<< The size of the free block. */
88 static const unsigned portSHORT heapSTRUCT_SIZE = ( sizeof( xBlockLink ) + ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) );
89 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )
91 /* Create a couple of list links to mark the start and end of the list. */
92 static xBlockLink xStart, xEnd;
94 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */
97 * Insert a block into the list of free blocks - which is ordered by size of
98 * the block. Small blocks at the start of the list and large blocks at the end
101 #define prvInsertBlockIntoFreeList( pxBlockToInsert ) \
103 xBlockLink *pxIterator; \
106 xBlockSize = pxBlockToInsert->xBlockSize; \
108 /* Iterate through the list until a block is found that has a larger size */ \
109 /* than the block we are inserting. */ \
110 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock ) \
112 /* There is nothing to do here - just iterate to the correct position. */ \
115 /* Update the list to include the block being inserted in the correct */ \
117 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock; \
118 pxIterator->pxNextFreeBlock = pxBlockToInsert; \
120 /*-----------------------------------------------------------*/
122 #define prvHeapInit() \
124 xBlockLink *pxFirstFreeBlock; \
126 /* xStart is used to hold a pointer to the first item in the list of free */ \
127 /* blocks. The void cast is used to prevent compiler warnings. */ \
128 xStart.pxNextFreeBlock = ( void * ) xHeap.ucHeap; \
129 xStart.xBlockSize = ( size_t ) 0; \
131 /* xEnd is used to mark the end of the list of free blocks. */ \
132 xEnd.xBlockSize = configTOTAL_HEAP_SIZE; \
133 xEnd.pxNextFreeBlock = NULL; \
135 /* To start with there is a single free block that is sized to take up the \
136 entire heap space. */ \
137 pxFirstFreeBlock = ( void * ) xHeap.ucHeap; \
138 pxFirstFreeBlock->xBlockSize = configTOTAL_HEAP_SIZE; \
139 pxFirstFreeBlock->pxNextFreeBlock = &xEnd; \
141 /*-----------------------------------------------------------*/
143 #if ( INCLUDE_vPortUsedMem == 1 )
145 void vPortUsedMem(int *bytesFree, int *bytesUsed, int *blocksFree)
147 xBlockLink *pxBlock = &xStart;
155 while ((pxBlock) && (pxBlock != &xEnd))
157 *bytesFree += pxBlock->xBlockSize;
158 pxBlock = pxBlock->pxNextFreeBlock;
164 *bytesUsed = configTOTAL_HEAP_SIZE - *bytesFree;
168 /*-----------------------------------------------------------*/
170 void *pvPortMalloc( size_t xWantedSize )
172 xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
173 static portBASE_TYPE xHeapHasBeenInitialised = pdFALSE;
174 void *pvReturn = NULL;
178 /* If this is the first call to malloc then the heap will require
179 initialisation to setup the list of free blocks. */
180 if( xHeapHasBeenInitialised == pdFALSE )
183 xHeapHasBeenInitialised = pdTRUE;
186 /* The wanted size is increased so it can contain a xBlockLink
187 structure in addition to the requested amount of bytes. */
188 if( xWantedSize > 0 )
190 xWantedSize += heapSTRUCT_SIZE;
192 /* Ensure that blocks are always aligned to the required number of bytes. */
193 if( xWantedSize & heapBYTE_ALIGNMENT_MASK )
195 /* Byte alignment required. */
196 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & heapBYTE_ALIGNMENT_MASK ) );
200 if( ( xWantedSize > 0 ) && ( xWantedSize < configTOTAL_HEAP_SIZE ) )
202 /* Blocks are stored in byte order - traverse the list from the start
203 (smallest) block until one of adequate size is found. */
204 pxPreviousBlock = &xStart;
205 pxBlock = xStart.pxNextFreeBlock;
206 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock ) )
208 pxPreviousBlock = pxBlock;
209 pxBlock = pxBlock->pxNextFreeBlock;
212 /* If we found the end marker then a block of adequate size was not found. */
213 if( pxBlock != &xEnd )
215 /* Return the memory space - jumping over the xBlockLink structure
217 pvReturn = ( void * ) ( ( ( unsigned portCHAR * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
219 /* This block is being returned for use so must be taken our of the
220 list of free blocks. */
221 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
223 /* If the block is larger than required it can be split into two. */
224 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
226 /* This block is to be split into two. Create a new block
227 following the number of bytes requested. The void cast is
228 used to prevent byte alignment warnings from the compiler. */
229 pxNewBlockLink = ( void * ) ( ( ( unsigned portCHAR * ) pxBlock ) + xWantedSize );
231 /* Calculate the sizes of two blocks split from the single
233 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
234 pxBlock->xBlockSize = xWantedSize;
236 /* Insert the new block into the list of free blocks. */
237 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
246 /*-----------------------------------------------------------*/
248 void vPortFree( void *pv )
250 unsigned portCHAR *puc = ( unsigned portCHAR * ) pv;
255 /* The memory being freed will have an xBlockLink structure immediately
257 puc -= heapSTRUCT_SIZE;
259 /* This casting is to keep the compiler from issuing warnings. */
260 pxLink = ( void * ) puc;
264 /* Add this block to the list of free blocks. */
265 prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );
270 /*-----------------------------------------------------------*/