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