]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MemMang/heap_4.c
Update version number ready for release.
[freertos] / FreeRTOS / Source / portable / MemMang / heap_4.c
1 /*\r
2     FreeRTOS V8.0.1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67  * A sample implementation of pvPortMalloc() and vPortFree() that combines\r
68  * (coalescences) adjacent memory blocks as they are freed, and in so doing\r
69  * limits memory fragmentation.\r
70  *\r
71  * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the\r
72  * memory management pages of http://www.FreeRTOS.org for more information.\r
73  */\r
74 #include <stdlib.h>\r
75 \r
76 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
77 all the API functions to use the MPU wrappers.  That should only be done when\r
78 task.h is included from an application file. */\r
79 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
80 \r
81 #include "FreeRTOS.h"\r
82 #include "task.h"\r
83 \r
84 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
85 \r
86 /* Block sizes must not get too small. */\r
87 #define heapMINIMUM_BLOCK_SIZE  ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )\r
88 \r
89 /* Assumes 8bit bytes! */\r
90 #define heapBITS_PER_BYTE               ( ( size_t ) 8 )\r
91 \r
92 /* A few bytes might be lost to byte aligning the heap start address. */\r
93 #define heapADJUSTED_HEAP_SIZE  ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )\r
94 \r
95 /* Allocate the memory for the heap. */\r
96 static uint8_t 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 } BlockLink_t;\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( BlockLink_t *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 uint16_t heapSTRUCT_SIZE   = ( ( sizeof ( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );\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 ) heapADJUSTED_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 BlockLink_t 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 ) heapADJUSTED_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );\r
137 static size_t xMinimumEverFreeBytesRemaining = ( ( size_t ) heapADJUSTED_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );\r
138 \r
139 /* Gets set to the top bit of an size_t type.  When this bit in the xBlockSize\r
140 member of an BlockLink_t structure is set then the block belongs to the\r
141 application.  When the bit is free the block is still part of the free heap\r
142 space. */\r
143 static size_t xBlockAllocatedBit = 0;\r
144 \r
145 /*-----------------------------------------------------------*/\r
146 \r
147 void *pvPortMalloc( size_t xWantedSize )\r
148 {\r
149 BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;\r
150 void *pvReturn = NULL;\r
151 \r
152         vTaskSuspendAll();\r
153         {\r
154                 /* If this is the first call to malloc then the heap will require\r
155                 initialisation to setup the list of free blocks. */\r
156                 if( pxEnd == NULL )\r
157                 {\r
158                         prvHeapInit();\r
159                 }\r
160                 else\r
161                 {\r
162                         mtCOVERAGE_TEST_MARKER();\r
163                 }\r
164 \r
165                 /* Check the requested block size is not so large that the top bit is\r
166                 set.  The top bit of the block size member of the BlockLink_t structure\r
167                 is used to determine who owns the block - the application or the\r
168                 kernel, so it must be free. */\r
169                 if( ( xWantedSize & xBlockAllocatedBit ) == 0 )\r
170                 {\r
171                         /* The wanted size is increased so it can contain a BlockLink_t\r
172                         structure in addition to the requested amount of bytes. */\r
173                         if( xWantedSize > 0 )\r
174                         {\r
175                                 xWantedSize += heapSTRUCT_SIZE;\r
176 \r
177                                 /* Ensure that blocks are always aligned to the required number\r
178                                 of bytes. */\r
179                                 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )\r
180                                 {\r
181                                         /* Byte alignment required. */\r
182                                         xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );\r
183                                 }\r
184                                 else\r
185                                 {\r
186                                         mtCOVERAGE_TEST_MARKER();\r
187                                 }\r
188                         }\r
189                         else\r
190                         {\r
191                                 mtCOVERAGE_TEST_MARKER();\r
192                         }\r
193 \r
194                         if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )\r
195                         {\r
196                                 /* Traverse the list from the start     (lowest address) block until\r
197                                 one     of adequate size is found. */\r
198                                 pxPreviousBlock = &xStart;\r
199                                 pxBlock = xStart.pxNextFreeBlock;\r
200                                 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )\r
201                                 {\r
202                                         pxPreviousBlock = pxBlock;\r
203                                         pxBlock = pxBlock->pxNextFreeBlock;\r
204                                 }\r
205 \r
206                                 /* If the end marker was reached then a block of adequate size\r
207                                 was     not found. */\r
208                                 if( pxBlock != pxEnd )\r
209                                 {\r
210                                         /* Return the memory space pointed to - jumping over the\r
211                                         BlockLink_t structure at its start. */\r
212                                         pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );\r
213 \r
214                                         /* This block is being returned for use so must be taken out\r
215                                         of the list of free blocks. */\r
216                                         pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;\r
217 \r
218                                         /* If the block is larger than required it can be split into\r
219                                         two. */\r
220                                         if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )\r
221                                         {\r
222                                                 /* This block is to be split into two.  Create a new\r
223                                                 block following the number of bytes requested. The void\r
224                                                 cast is used to prevent byte alignment warnings from the\r
225                                                 compiler. */\r
226                                                 pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );\r
227 \r
228                                                 /* Calculate the sizes of two blocks split from the\r
229                                                 single block. */\r
230                                                 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;\r
231                                                 pxBlock->xBlockSize = xWantedSize;\r
232 \r
233                                                 /* Insert the new block into the list of free blocks. */\r
234                                                 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );\r
235                                         }\r
236                                         else\r
237                                         {\r
238                                                 mtCOVERAGE_TEST_MARKER();\r
239                                         }\r
240 \r
241                                         xFreeBytesRemaining -= pxBlock->xBlockSize;\r
242 \r
243                                         if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )\r
244                                         {\r
245                                                 xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;\r
246                                         }\r
247                                         else\r
248                                         {\r
249                                                 mtCOVERAGE_TEST_MARKER();\r
250                                         }\r
251 \r
252                                         /* The block is being returned - it is allocated and owned\r
253                                         by the application and has no "next" block. */\r
254                                         pxBlock->xBlockSize |= xBlockAllocatedBit;\r
255                                         pxBlock->pxNextFreeBlock = NULL;\r
256                                 }\r
257                                 else\r
258                                 {\r
259                                         mtCOVERAGE_TEST_MARKER();\r
260                                 }\r
261                         }\r
262                         else\r
263                         {\r
264                                 mtCOVERAGE_TEST_MARKER();\r
265                         }\r
266                 }\r
267                 else\r
268                 {\r
269                         mtCOVERAGE_TEST_MARKER();\r
270                 }\r
271 \r
272                 traceMALLOC( pvReturn, xWantedSize );\r
273         }\r
274         ( void ) xTaskResumeAll();\r
275 \r
276         #if( configUSE_MALLOC_FAILED_HOOK == 1 )\r
277         {\r
278                 if( pvReturn == NULL )\r
279                 {\r
280                         extern void vApplicationMallocFailedHook( void );\r
281                         vApplicationMallocFailedHook();\r
282                 }\r
283                 else\r
284                 {\r
285                         mtCOVERAGE_TEST_MARKER();\r
286                 }\r
287         }\r
288         #endif\r
289 \r
290         return pvReturn;\r
291 }\r
292 /*-----------------------------------------------------------*/\r
293 \r
294 void vPortFree( void *pv )\r
295 {\r
296 uint8_t *puc = ( uint8_t * ) pv;\r
297 BlockLink_t *pxLink;\r
298 \r
299         if( pv != NULL )\r
300         {\r
301                 /* The memory being freed will have an BlockLink_t structure immediately\r
302                 before it. */\r
303                 puc -= heapSTRUCT_SIZE;\r
304 \r
305                 /* This casting is to keep the compiler from issuing warnings. */\r
306                 pxLink = ( void * ) puc;\r
307 \r
308                 /* Check the block is actually allocated. */\r
309                 configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );\r
310                 configASSERT( pxLink->pxNextFreeBlock == NULL );\r
311 \r
312                 if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )\r
313                 {\r
314                         if( pxLink->pxNextFreeBlock == NULL )\r
315                         {\r
316                                 /* The block is being returned to the heap - it is no longer\r
317                                 allocated. */\r
318                                 pxLink->xBlockSize &= ~xBlockAllocatedBit;\r
319 \r
320                                 vTaskSuspendAll();\r
321                                 {\r
322                                         /* Add this block to the list of free blocks. */\r
323                                         xFreeBytesRemaining += pxLink->xBlockSize;\r
324                                         traceFREE( pv, pxLink->xBlockSize );\r
325                                         prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );\r
326                                 }\r
327                                 ( void ) xTaskResumeAll();\r
328                         }\r
329                         else\r
330                         {\r
331                                 mtCOVERAGE_TEST_MARKER();\r
332                         }\r
333                 }\r
334                 else\r
335                 {\r
336                         mtCOVERAGE_TEST_MARKER();\r
337                 }\r
338         }\r
339 }\r
340 /*-----------------------------------------------------------*/\r
341 \r
342 size_t xPortGetFreeHeapSize( void )\r
343 {\r
344         return xFreeBytesRemaining;\r
345 }\r
346 /*-----------------------------------------------------------*/\r
347 \r
348 size_t xPortGetMinimumEverFreeHeapSize( void )\r
349 {\r
350         return xMinimumEverFreeBytesRemaining;\r
351 }\r
352 /*-----------------------------------------------------------*/\r
353 \r
354 void vPortInitialiseBlocks( void )\r
355 {\r
356         /* This just exists to keep the linker quiet. */\r
357 }\r
358 /*-----------------------------------------------------------*/\r
359 \r
360 static void prvHeapInit( void )\r
361 {\r
362 BlockLink_t *pxFirstFreeBlock;\r
363 uint8_t *pucHeapEnd, *pucAlignedHeap;\r
364 \r
365         /* Ensure the heap starts on a correctly aligned boundary. */\r
366         pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK ) );\r
367 \r
368         /* xStart is used to hold a pointer to the first item in the list of free\r
369         blocks.  The void cast is used to prevent compiler warnings. */\r
370         xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;\r
371         xStart.xBlockSize = ( size_t ) 0;\r
372 \r
373         /* pxEnd is used to mark the end of the list of free blocks and is inserted\r
374         at the end of the heap space. */\r
375         pucHeapEnd = pucAlignedHeap + xTotalHeapSize;\r
376         pucHeapEnd -= heapSTRUCT_SIZE;\r
377         pxEnd = ( void * ) pucHeapEnd;\r
378         configASSERT( ( ( ( uint32_t ) pxEnd ) & ( ( uint32_t ) portBYTE_ALIGNMENT_MASK ) ) == 0UL );\r
379         pxEnd->xBlockSize = 0;\r
380         pxEnd->pxNextFreeBlock = NULL;\r
381 \r
382         /* To start with there is a single free block that is sized to take up the\r
383         entire heap space, minus the space taken by pxEnd. */\r
384         pxFirstFreeBlock = ( void * ) pucAlignedHeap;\r
385         pxFirstFreeBlock->xBlockSize = xTotalHeapSize - heapSTRUCT_SIZE;\r
386         pxFirstFreeBlock->pxNextFreeBlock = pxEnd;\r
387 \r
388         /* The heap now contains pxEnd. */\r
389         xFreeBytesRemaining -= heapSTRUCT_SIZE;\r
390 \r
391         /* Work out the position of the top bit in a size_t variable. */\r
392         xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );\r
393 }\r
394 /*-----------------------------------------------------------*/\r
395 \r
396 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )\r
397 {\r
398 BlockLink_t *pxIterator;\r
399 uint8_t *puc;\r
400 \r
401         /* Iterate through the list until a block is found that has a higher address\r
402         than the block being inserted. */\r
403         for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )\r
404         {\r
405                 /* Nothing to do here, just iterate to the right position. */\r
406         }\r
407 \r
408         /* Do the block being inserted, and the block it is being inserted after\r
409         make a contiguous block of memory? */\r
410         puc = ( uint8_t * ) pxIterator;\r
411         if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )\r
412         {\r
413                 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;\r
414                 pxBlockToInsert = pxIterator;\r
415         }\r
416         else\r
417         {\r
418                 mtCOVERAGE_TEST_MARKER();\r
419         }\r
420 \r
421         /* Do the block being inserted, and the block it is being inserted before\r
422         make a contiguous block of memory? */\r
423         puc = ( uint8_t * ) pxBlockToInsert;\r
424         if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )\r
425         {\r
426                 if( pxIterator->pxNextFreeBlock != pxEnd )\r
427                 {\r
428                         /* Form one big block from the two blocks. */\r
429                         pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;\r
430                         pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;\r
431                 }\r
432                 else\r
433                 {\r
434                         pxBlockToInsert->pxNextFreeBlock = pxEnd;\r
435                 }\r
436         }\r
437         else\r
438         {\r
439                 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;\r
440         }\r
441 \r
442         /* If the block being inserted plugged a gab, so was merged with the block\r
443         before and the block after, then it's pxNextFreeBlock pointer will have\r
444         already been set, and should not be set here as that would make it point\r
445         to itself. */\r
446         if( pxIterator != pxBlockToInsert )\r
447         {\r
448                 pxIterator->pxNextFreeBlock = pxBlockToInsert;\r
449         }\r
450         else\r
451         {\r
452                 mtCOVERAGE_TEST_MARKER();\r
453         }\r
454 }\r
455 \r