FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Source / portable / MemMang / heap_2.c
blob43440575c5d30d94ab8acde5b74903c1450e7f84
1 /*
2 FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.
4 ***************************************************************************
5 * *
6 * If you are: *
7 * *
8 * + New to FreeRTOS, *
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
10 * + Looking for basic training, *
11 * + Wanting to improve your FreeRTOS skills and productivity *
12 * *
13 * then take a look at the FreeRTOS eBook *
14 * *
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
16 * http://www.FreeRTOS.org/Documentation *
17 * *
18 * A pdf reference manual is also available. Both are usually delivered *
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
20 * and 8pm GMT (although please allow up to 24 hours in case of *
21 * exceptional circumstances). Thank you for your support! *
22 * *
23 ***************************************************************************
25 This file is part of the FreeRTOS distribution.
27 FreeRTOS is free software; you can redistribute it and/or modify it under
28 the terms of the GNU General Public License (version 2) as published by the
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
31 a combined work that includes FreeRTOS without being obliged to provide the
32 source code for proprietary components outside of the FreeRTOS kernel.
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
36 more details. You should have received a copy of the GNU General Public
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
39 by writing to Richard Barry, contact details for whom are available on the
40 FreeRTOS WEB site.
42 1 tab == 4 spaces!
44 http://www.FreeRTOS.org - Documentation, latest information, license and
45 contact details.
47 http://www.SafeRTOS.com - A version that is certified for use in safety
48 critical systems.
50 http://www.OpenRTOS.com - Commercial support, development, porting,
51 licensing and training services.
55 * A sample implementation of pvPortMalloc() and vPortFree() that permits
56 * allocated blocks to be freed, but does not combine adjacent free blocks
57 * into a single larger block.
59 * See heap_1.c and heap_3.c for alternative implementations, and the memory
60 * management pages of http://www.FreeRTOS.org for more information.
62 #include <stdlib.h>
64 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
65 all the API functions to use the MPU wrappers. That should only be done when
66 task.h is included from an application file. */
67 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
69 #include "FreeRTOS.h"
70 #include "task.h"
72 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
74 /* Allocate the memory for the heap. The struct is used to force byte
75 alignment without using any non-portable code. */
76 static union xRTOS_HEAP
78 #if portBYTE_ALIGNMENT == 8
79 volatile portDOUBLE dDummy;
80 #else
81 volatile unsigned long ulDummy;
82 #endif
83 unsigned char ucHeap[ configTOTAL_HEAP_SIZE ];
84 } xHeap;
86 /* Define the linked list structure. This is used to link free blocks in order
87 of their size. */
88 typedef struct A_BLOCK_LINK
90 struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
91 size_t xBlockSize; /*<< The size of the free block. */
92 } xBlockLink;
95 static const unsigned short heapSTRUCT_SIZE = ( sizeof( xBlockLink ) + portBYTE_ALIGNMENT - ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) );
96 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )
98 /* Create a couple of list links to mark the start and end of the list. */
99 static xBlockLink xStart, xEnd;
101 /* Keeps track of the number of free bytes remaining, but says nothing about
102 fragmentation. */
103 static size_t xFreeBytesRemaining = configTOTAL_HEAP_SIZE;
105 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */
108 * Insert a block into the list of free blocks - which is ordered by size of
109 * the block. Small blocks at the start of the list and large blocks at the end
110 * of the list.
112 #define prvInsertBlockIntoFreeList( pxBlockToInsert ) \
114 xBlockLink *pxIterator; \
115 size_t xBlockSize; \
117 xBlockSize = pxBlockToInsert->xBlockSize; \
119 /* Iterate through the list until a block is found that has a larger size */ \
120 /* than the block we are inserting. */ \
121 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock ) \
123 /* There is nothing to do here - just iterate to the correct position. */ \
126 /* Update the list to include the block being inserted in the correct */ \
127 /* position. */ \
128 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock; \
129 pxIterator->pxNextFreeBlock = pxBlockToInsert; \
131 /*-----------------------------------------------------------*/
133 #define prvHeapInit() \
135 xBlockLink *pxFirstFreeBlock; \
137 /* xStart is used to hold a pointer to the first item in the list of free */ \
138 /* blocks. The void cast is used to prevent compiler warnings. */ \
139 xStart.pxNextFreeBlock = ( void * ) xHeap.ucHeap; \
140 xStart.xBlockSize = ( size_t ) 0; \
142 /* xEnd is used to mark the end of the list of free blocks. */ \
143 xEnd.xBlockSize = configTOTAL_HEAP_SIZE; \
144 xEnd.pxNextFreeBlock = NULL; \
146 /* To start with there is a single free block that is sized to take up the \
147 entire heap space. */ \
148 pxFirstFreeBlock = ( void * ) xHeap.ucHeap; \
149 pxFirstFreeBlock->xBlockSize = configTOTAL_HEAP_SIZE; \
150 pxFirstFreeBlock->pxNextFreeBlock = &xEnd; \
152 /*-----------------------------------------------------------*/
154 void *pvPortMalloc( size_t xWantedSize )
156 xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
157 static portBASE_TYPE xHeapHasBeenInitialised = pdFALSE;
158 void *pvReturn = NULL;
160 vTaskSuspendAll();
162 /* If this is the first call to malloc then the heap will require
163 initialisation to setup the list of free blocks. */
164 if( xHeapHasBeenInitialised == pdFALSE )
166 prvHeapInit();
167 xHeapHasBeenInitialised = pdTRUE;
170 /* The wanted size is increased so it can contain a xBlockLink
171 structure in addition to the requested amount of bytes. */
172 if( xWantedSize > 0 )
174 xWantedSize += heapSTRUCT_SIZE;
176 /* Ensure that blocks are always aligned to the required number of bytes. */
177 if( xWantedSize & portBYTE_ALIGNMENT_MASK )
179 /* Byte alignment required. */
180 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
184 if( ( xWantedSize > 0 ) && ( xWantedSize < configTOTAL_HEAP_SIZE ) )
186 /* Blocks are stored in byte order - traverse the list from the start
187 (smallest) block until one of adequate size is found. */
188 pxPreviousBlock = &xStart;
189 pxBlock = xStart.pxNextFreeBlock;
190 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock ) )
192 pxPreviousBlock = pxBlock;
193 pxBlock = pxBlock->pxNextFreeBlock;
196 /* If we found the end marker then a block of adequate size was not found. */
197 if( pxBlock != &xEnd )
199 /* Return the memory space - jumping over the xBlockLink structure
200 at its start. */
201 pvReturn = ( void * ) ( ( ( unsigned char * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
203 /* This block is being returned for use so must be taken our of the
204 list of free blocks. */
205 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
207 /* If the block is larger than required it can be split into two. */
208 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
210 /* This block is to be split into two. Create a new block
211 following the number of bytes requested. The void cast is
212 used to prevent byte alignment warnings from the compiler. */
213 pxNewBlockLink = ( void * ) ( ( ( unsigned char * ) pxBlock ) + xWantedSize );
215 /* Calculate the sizes of two blocks split from the single
216 block. */
217 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
218 pxBlock->xBlockSize = xWantedSize;
220 /* Insert the new block into the list of free blocks. */
221 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
224 xFreeBytesRemaining -= xWantedSize;
228 xTaskResumeAll();
230 #if( configUSE_MALLOC_FAILED_HOOK == 1 )
232 if( pvReturn == NULL )
234 extern void vApplicationMallocFailedHook( void );
235 vApplicationMallocFailedHook();
238 #endif
240 return pvReturn;
242 /*-----------------------------------------------------------*/
244 void vPortFree( void *pv )
246 unsigned char *puc = ( unsigned char * ) pv;
247 xBlockLink *pxLink;
249 if( pv )
251 /* The memory being freed will have an xBlockLink structure immediately
252 before it. */
253 puc -= heapSTRUCT_SIZE;
255 /* This casting is to keep the compiler from issuing warnings. */
256 pxLink = ( void * ) puc;
258 vTaskSuspendAll();
260 /* Add this block to the list of free blocks. */
261 prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );
262 xFreeBytesRemaining += pxLink->xBlockSize;
264 xTaskResumeAll();
267 /*-----------------------------------------------------------*/
269 size_t xPortGetFreeHeapSize( void )
271 return xFreeBytesRemaining;
273 /*-----------------------------------------------------------*/
275 void vPortInitialiseBlocks( void )
277 /* This just exists to keep the linker quiet. */