]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MemMang/heap_4.c
Added more files to the Rowley and IAR LM3S demos to test building the newer files...
[freertos] / FreeRTOS / Source / portable / MemMang / heap_4.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /*\r
70  * A sample implementation of pvPortMalloc() and vPortFree() that combines \r
71  * (coalescences) adjacent memory blocks as they are freed, and in so doing \r
72  * limits memory fragmentation.\r
73  *\r
74  * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the \r
75  * memory management pages of http://www.FreeRTOS.org for more information.\r
76  */\r
77 #include <stdlib.h>\r
78 \r
79 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
80 all the API functions to use the MPU wrappers.  That should only be done when\r
81 task.h is included from an application file. */\r
82 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
83 \r
84 #include "FreeRTOS.h"\r
85 #include "task.h"\r
86 \r
87 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
88 \r
89 /* Block sizes must not get too small. */\r
90 #define heapMINIMUM_BLOCK_SIZE  ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )\r
91 \r
92 /* A few bytes might be lost to byte aligning the heap start address. */\r
93 #define configADJUSTED_HEAP_SIZE        ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )\r
94 \r
95 /* Allocate the memory for the heap. */\r
96 static unsigned char ucHeap[ configTOTAL_HEAP_SIZE ];\r
97 \r
98 /* Define the linked list structure.  This is used to link free blocks in order\r
99 of their memory address. */\r
100 typedef struct A_BLOCK_LINK\r
101 {\r
102         struct A_BLOCK_LINK *pxNextFreeBlock;   /*<< The next free block in the list. */\r
103         size_t xBlockSize;                                              /*<< The size of the free block. */\r
104 } xBlockLink;\r
105 \r
106 /*-----------------------------------------------------------*/\r
107 \r
108 /*\r
109  * Inserts a block of memory that is being freed into the correct position in \r
110  * the list of free memory blocks.  The block being freed will be merged with\r
111  * the block in front it and/or the block behind it if the memory blocks are\r
112  * adjacent to each other.\r
113  */\r
114 static void prvInsertBlockIntoFreeList( xBlockLink *pxBlockToInsert );\r
115 \r
116 /*\r
117  * Called automatically to setup the required heap structures the first time\r
118  * pvPortMalloc() is called.\r
119  */\r
120 static void prvHeapInit( void );\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 \r
124 /* The size of the structure placed at the beginning of each allocated memory\r
125 block must by correctly byte aligned. */\r
126 static const unsigned short heapSTRUCT_SIZE     = ( sizeof( xBlockLink ) + portBYTE_ALIGNMENT - ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) );\r
127 \r
128 /* Ensure the pxEnd pointer will end up on the correct byte alignment. */\r
129 static const size_t xTotalHeapSize = ( ( size_t ) configADJUSTED_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );\r
130 \r
131 /* Create a couple of list links to mark the start and end of the list. */\r
132 static xBlockLink xStart, *pxEnd = NULL;\r
133 \r
134 /* Keeps track of the number of free bytes remaining, but says nothing about\r
135 fragmentation. */\r
136 static size_t xFreeBytesRemaining = ( ( size_t ) configADJUSTED_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );\r
137 \r
138 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */\r
139 \r
140 /*-----------------------------------------------------------*/\r
141 \r
142 void *pvPortMalloc( size_t xWantedSize )\r
143 {\r
144 xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;\r
145 void *pvReturn = NULL;\r
146 \r
147         vTaskSuspendAll();\r
148         {\r
149                 /* If this is the first call to malloc then the heap will require\r
150                 initialisation to setup the list of free blocks. */\r
151                 if( pxEnd == NULL )\r
152                 {\r
153                         prvHeapInit();\r
154                 }\r
155 \r
156                 /* The wanted size is increased so it can contain a xBlockLink\r
157                 structure in addition to the requested amount of bytes. */\r
158                 if( xWantedSize > 0 )\r
159                 {\r
160                         xWantedSize += heapSTRUCT_SIZE;\r
161 \r
162                         /* Ensure that blocks are always aligned to the required number of \r
163                         bytes. */\r
164                         if( xWantedSize & portBYTE_ALIGNMENT_MASK )\r
165                         {\r
166                                 /* Byte alignment required. */\r
167                                 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );\r
168                         }\r
169                 }\r
170 \r
171                 if( ( xWantedSize > 0 ) && ( xWantedSize < xTotalHeapSize ) )\r
172                 {\r
173                         /* Traverse the list from the start     (lowest address) block until one\r
174                         of adequate size is found. */\r
175                         pxPreviousBlock = &xStart;\r
176                         pxBlock = xStart.pxNextFreeBlock;\r
177                         while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )\r
178                         {\r
179                                 pxPreviousBlock = pxBlock;\r
180                                 pxBlock = pxBlock->pxNextFreeBlock;\r
181                         }\r
182 \r
183                         /* If the end marker was reached then a block of adequate size was\r
184                         not found. */\r
185                         if( pxBlock != pxEnd )\r
186                         {\r
187                                 /* Return the memory space - jumping over the xBlockLink structure\r
188                                 at its start. */\r
189                                 pvReturn = ( void * ) ( ( ( unsigned char * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );\r
190 \r
191                                 /* This block is being returned for use so must be taken out of\r
192                                 the     list of free blocks. */\r
193                                 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;\r
194 \r
195                                 /* If the block is larger than required it can be split into two. */\r
196                                 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )\r
197                                 {\r
198                                         /* This block is to be split into two.  Create a new block\r
199                                         following the number of bytes requested. The void cast is\r
200                                         used to prevent byte alignment warnings from the compiler. */\r
201                                         pxNewBlockLink = ( void * ) ( ( ( unsigned char * ) pxBlock ) + xWantedSize );\r
202 \r
203                                         /* Calculate the sizes of two blocks split from the single\r
204                                         block. */\r
205                                         pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;\r
206                                         pxBlock->xBlockSize = xWantedSize;\r
207 \r
208                                         /* Insert the new block into the list of free blocks. */\r
209                                         prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );\r
210                                 }\r
211 \r
212                                 xFreeBytesRemaining -= pxBlock->xBlockSize;\r
213                         }\r
214                 }\r
215         }\r
216         xTaskResumeAll();\r
217 \r
218         #if( configUSE_MALLOC_FAILED_HOOK == 1 )\r
219         {\r
220                 if( pvReturn == NULL )\r
221                 {\r
222                         extern void vApplicationMallocFailedHook( void );\r
223                         vApplicationMallocFailedHook();\r
224                 }\r
225         }\r
226         #endif\r
227 \r
228         return pvReturn;\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 void vPortFree( void *pv )\r
233 {\r
234 unsigned char *puc = ( unsigned char * ) pv;\r
235 xBlockLink *pxLink;\r
236 \r
237         if( pv != NULL )\r
238         {\r
239                 /* The memory being freed will have an xBlockLink structure immediately\r
240                 before it. */\r
241                 puc -= heapSTRUCT_SIZE;\r
242 \r
243                 /* This casting is to keep the compiler from issuing warnings. */\r
244                 pxLink = ( void * ) puc;\r
245 \r
246                 vTaskSuspendAll();\r
247                 {\r
248                         /* Add this block to the list of free blocks. */\r
249                         xFreeBytesRemaining += pxLink->xBlockSize;\r
250                         prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );                      \r
251                 }\r
252                 xTaskResumeAll();\r
253         }\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 size_t xPortGetFreeHeapSize( void )\r
258 {\r
259         return xFreeBytesRemaining;\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 void vPortInitialiseBlocks( void )\r
264 {\r
265         /* This just exists to keep the linker quiet. */\r
266 }\r
267 /*-----------------------------------------------------------*/\r
268 \r
269 static void prvHeapInit( void )\r
270 {\r
271 xBlockLink *pxFirstFreeBlock;\r
272 unsigned char *pucHeapEnd, *pucAlignedHeap;\r
273 \r
274         /* Ensure the heap starts on a correctly aligned boundary. */\r
275         pucAlignedHeap = ( unsigned char * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK ) );\r
276 \r
277         /* xStart is used to hold a pointer to the first item in the list of free\r
278         blocks.  The void cast is used to prevent compiler warnings. */\r
279         xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;\r
280         xStart.xBlockSize = ( size_t ) 0;\r
281 \r
282         /* pxEnd is used to mark the end of the list of free blocks and is inserted\r
283         at the end of the heap space. */\r
284         pucHeapEnd = pucAlignedHeap + xTotalHeapSize;\r
285         pucHeapEnd -= heapSTRUCT_SIZE;\r
286         pxEnd = ( void * ) pucHeapEnd;\r
287         configASSERT( ( ( ( unsigned long ) pxEnd ) & ( ( unsigned long ) portBYTE_ALIGNMENT_MASK ) ) == 0UL );\r
288         pxEnd->xBlockSize = 0;\r
289         pxEnd->pxNextFreeBlock = NULL;\r
290 \r
291         /* To start with there is a single free block that is sized to take up the\r
292         entire heap space, minus the space taken by pxEnd. */\r
293         pxFirstFreeBlock = ( void * ) pucAlignedHeap;\r
294         pxFirstFreeBlock->xBlockSize = xTotalHeapSize - heapSTRUCT_SIZE;\r
295         pxFirstFreeBlock->pxNextFreeBlock = pxEnd;\r
296 \r
297         /* The heap now contains pxEnd. */\r
298         xFreeBytesRemaining -= heapSTRUCT_SIZE;\r
299 }\r
300 /*-----------------------------------------------------------*/\r
301 \r
302 static void prvInsertBlockIntoFreeList( xBlockLink *pxBlockToInsert )\r
303 {\r
304 xBlockLink *pxIterator;\r
305 unsigned char *puc;\r
306 \r
307         /* Iterate through the list until a block is found that has a higher address\r
308         than the block being inserted. */\r
309         for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )\r
310         {\r
311                 /* Nothing to do here, just iterate to the right position. */\r
312         }\r
313 \r
314         /* Do the block being inserted, and the block it is being inserted after\r
315         make a contiguous block of memory? */   \r
316         puc = ( unsigned char * ) pxIterator;\r
317         if( ( puc + pxIterator->xBlockSize ) == ( unsigned char * ) pxBlockToInsert )\r
318         {\r
319                 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;\r
320                 pxBlockToInsert = pxIterator;\r
321         }\r
322 \r
323         /* Do the block being inserted, and the block it is being inserted before\r
324         make a contiguous block of memory? */\r
325         puc = ( unsigned char * ) pxBlockToInsert;\r
326         if( ( puc + pxBlockToInsert->xBlockSize ) == ( unsigned char * ) pxIterator->pxNextFreeBlock )\r
327         {\r
328                 if( pxIterator->pxNextFreeBlock != pxEnd )\r
329                 {\r
330                         /* Form one big block from the two blocks. */\r
331                         pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;\r
332                         pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;\r
333                 }\r
334                 else\r
335                 {\r
336                         pxBlockToInsert->pxNextFreeBlock = pxEnd;\r
337                 }\r
338         }\r
339         else\r
340         {\r
341                 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;         \r
342         }\r
343 \r
344         /* If the block being inserted plugged a gab, so was merged with the block\r
345         before and the block after, then it's pxNextFreeBlock pointer will have\r
346         already been set, and should not be set here as that would make it point\r
347         to itself. */\r
348         if( pxIterator != pxBlockToInsert )\r
349         {\r
350                 pxIterator->pxNextFreeBlock = pxBlockToInsert;\r
351         }\r
352 }\r
353 \r