]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MemMang/heap_5.c
Update version number in readiness for V10.2.0 release.
[freertos] / FreeRTOS / Source / portable / MemMang / heap_5.c
1 /*\r
2  * FreeRTOS Kernel V10.2.0\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * A sample implementation of pvPortMalloc() that allows the heap to be defined\r
30  * across multiple non-contigous blocks and combines (coalescences) adjacent\r
31  * memory blocks as they are freed.\r
32  *\r
33  * See heap_1.c, heap_2.c, heap_3.c and heap_4.c for alternative\r
34  * implementations, and the memory management pages of http://www.FreeRTOS.org\r
35  * for more information.\r
36  *\r
37  * Usage notes:\r
38  *\r
39  * vPortDefineHeapRegions() ***must*** be called before pvPortMalloc().\r
40  * pvPortMalloc() will be called if any task objects (tasks, queues, event\r
41  * groups, etc.) are created, therefore vPortDefineHeapRegions() ***must*** be\r
42  * called before any other objects are defined.\r
43  *\r
44  * vPortDefineHeapRegions() takes a single parameter.  The parameter is an array\r
45  * of HeapRegion_t structures.  HeapRegion_t is defined in portable.h as\r
46  *\r
47  * typedef struct HeapRegion\r
48  * {\r
49  *      uint8_t *pucStartAddress; << Start address of a block of memory that will be part of the heap.\r
50  *      size_t xSizeInBytes;      << Size of the block of memory.\r
51  * } HeapRegion_t;\r
52  *\r
53  * The array is terminated using a NULL zero sized region definition, and the\r
54  * memory regions defined in the array ***must*** appear in address order from\r
55  * low address to high address.  So the following is a valid example of how\r
56  * to use the function.\r
57  *\r
58  * HeapRegion_t xHeapRegions[] =\r
59  * {\r
60  *      { ( uint8_t * ) 0x80000000UL, 0x10000 }, << Defines a block of 0x10000 bytes starting at address 0x80000000\r
61  *      { ( uint8_t * ) 0x90000000UL, 0xa0000 }, << Defines a block of 0xa0000 bytes starting at address of 0x90000000\r
62  *      { NULL, 0 }                << Terminates the array.\r
63  * };\r
64  *\r
65  * vPortDefineHeapRegions( xHeapRegions ); << Pass the array into vPortDefineHeapRegions().\r
66  *\r
67  * Note 0x80000000 is the lower address so appears in the array first.\r
68  *\r
69  */\r
70 #include <stdlib.h>\r
71 \r
72 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
73 all the API functions to use the MPU wrappers.  That should only be done when\r
74 task.h is included from an application file. */\r
75 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
76 \r
77 #include "FreeRTOS.h"\r
78 #include "task.h"\r
79 \r
80 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
81 \r
82 #if( configSUPPORT_DYNAMIC_ALLOCATION == 0 )\r
83         #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0\r
84 #endif\r
85 \r
86 /* Block sizes must not get too small. */\r
87 #define heapMINIMUM_BLOCK_SIZE  ( ( size_t ) ( xHeapStructSize << 1 ) )\r
88 \r
89 /* Assumes 8bit bytes! */\r
90 #define heapBITS_PER_BYTE               ( ( size_t ) 8 )\r
91 \r
92 /* Define the linked list structure.  This is used to link free blocks in order\r
93 of their memory address. */\r
94 typedef struct A_BLOCK_LINK\r
95 {\r
96         struct A_BLOCK_LINK *pxNextFreeBlock;   /*<< The next free block in the list. */\r
97         size_t xBlockSize;                                              /*<< The size of the free block. */\r
98 } BlockLink_t;\r
99 \r
100 /*-----------------------------------------------------------*/\r
101 \r
102 /*\r
103  * Inserts a block of memory that is being freed into the correct position in\r
104  * the list of free memory blocks.  The block being freed will be merged with\r
105  * the block in front it and/or the block behind it if the memory blocks are\r
106  * adjacent to each other.\r
107  */\r
108 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert );\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /* The size of the structure placed at the beginning of each allocated memory\r
113 block must by correctly byte aligned. */\r
114 static const size_t xHeapStructSize     = ( sizeof( BlockLink_t ) + ( ( size_t ) ( portBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) portBYTE_ALIGNMENT_MASK );\r
115 \r
116 /* Create a couple of list links to mark the start and end of the list. */\r
117 static BlockLink_t xStart, *pxEnd = NULL;\r
118 \r
119 /* Keeps track of the number of free bytes remaining, but says nothing about\r
120 fragmentation. */\r
121 static size_t xFreeBytesRemaining = 0U;\r
122 static size_t xMinimumEverFreeBytesRemaining = 0U;\r
123 \r
124 /* Gets set to the top bit of an size_t type.  When this bit in the xBlockSize\r
125 member of an BlockLink_t structure is set then the block belongs to the\r
126 application.  When the bit is free the block is still part of the free heap\r
127 space. */\r
128 static size_t xBlockAllocatedBit = 0;\r
129 \r
130 /*-----------------------------------------------------------*/\r
131 \r
132 void *pvPortMalloc( size_t xWantedSize )\r
133 {\r
134 BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;\r
135 void *pvReturn = NULL;\r
136 \r
137         /* The heap must be initialised before the first call to\r
138         prvPortMalloc(). */\r
139         configASSERT( pxEnd );\r
140 \r
141         vTaskSuspendAll();\r
142         {\r
143                 /* Check the requested block size is not so large that the top bit is\r
144                 set.  The top bit of the block size member of the BlockLink_t structure\r
145                 is used to determine who owns the block - the application or the\r
146                 kernel, so it must be free. */\r
147                 if( ( xWantedSize & xBlockAllocatedBit ) == 0 )\r
148                 {\r
149                         /* The wanted size is increased so it can contain a BlockLink_t\r
150                         structure in addition to the requested amount of bytes. */\r
151                         if( xWantedSize > 0 )\r
152                         {\r
153                                 xWantedSize += xHeapStructSize;\r
154 \r
155                                 /* Ensure that blocks are always aligned to the required number\r
156                                 of bytes. */\r
157                                 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )\r
158                                 {\r
159                                         /* Byte alignment required. */\r
160                                         xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );\r
161                                 }\r
162                                 else\r
163                                 {\r
164                                         mtCOVERAGE_TEST_MARKER();\r
165                                 }\r
166                         }\r
167                         else\r
168                         {\r
169                                 mtCOVERAGE_TEST_MARKER();\r
170                         }\r
171 \r
172                         if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )\r
173                         {\r
174                                 /* Traverse the list from the start     (lowest address) block until\r
175                                 one     of adequate size is found. */\r
176                                 pxPreviousBlock = &xStart;\r
177                                 pxBlock = xStart.pxNextFreeBlock;\r
178                                 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )\r
179                                 {\r
180                                         pxPreviousBlock = pxBlock;\r
181                                         pxBlock = pxBlock->pxNextFreeBlock;\r
182                                 }\r
183 \r
184                                 /* If the end marker was reached then a block of adequate size\r
185                                 was     not found. */\r
186                                 if( pxBlock != pxEnd )\r
187                                 {\r
188                                         /* Return the memory space pointed to - jumping over the\r
189                                         BlockLink_t structure at its start. */\r
190                                         pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );\r
191 \r
192                                         /* This block is being returned for use so must be taken out\r
193                                         of the list of free blocks. */\r
194                                         pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;\r
195 \r
196                                         /* If the block is larger than required it can be split into\r
197                                         two. */\r
198                                         if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )\r
199                                         {\r
200                                                 /* This block is to be split into two.  Create a new\r
201                                                 block following the number of bytes requested. The void\r
202                                                 cast is used to prevent byte alignment warnings from the\r
203                                                 compiler. */\r
204                                                 pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );\r
205 \r
206                                                 /* Calculate the sizes of two blocks split from the\r
207                                                 single block. */\r
208                                                 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;\r
209                                                 pxBlock->xBlockSize = xWantedSize;\r
210 \r
211                                                 /* Insert the new block into the list of free blocks. */\r
212                                                 prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );\r
213                                         }\r
214                                         else\r
215                                         {\r
216                                                 mtCOVERAGE_TEST_MARKER();\r
217                                         }\r
218 \r
219                                         xFreeBytesRemaining -= pxBlock->xBlockSize;\r
220 \r
221                                         if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )\r
222                                         {\r
223                                                 xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;\r
224                                         }\r
225                                         else\r
226                                         {\r
227                                                 mtCOVERAGE_TEST_MARKER();\r
228                                         }\r
229 \r
230                                         /* The block is being returned - it is allocated and owned\r
231                                         by the application and has no "next" block. */\r
232                                         pxBlock->xBlockSize |= xBlockAllocatedBit;\r
233                                         pxBlock->pxNextFreeBlock = NULL;\r
234                                 }\r
235                                 else\r
236                                 {\r
237                                         mtCOVERAGE_TEST_MARKER();\r
238                                 }\r
239                         }\r
240                         else\r
241                         {\r
242                                 mtCOVERAGE_TEST_MARKER();\r
243                         }\r
244                 }\r
245                 else\r
246                 {\r
247                         mtCOVERAGE_TEST_MARKER();\r
248                 }\r
249 \r
250                 traceMALLOC( pvReturn, xWantedSize );\r
251         }\r
252         ( void ) xTaskResumeAll();\r
253 \r
254         #if( configUSE_MALLOC_FAILED_HOOK == 1 )\r
255         {\r
256                 if( pvReturn == NULL )\r
257                 {\r
258                         extern void vApplicationMallocFailedHook( void );\r
259                         vApplicationMallocFailedHook();\r
260                 }\r
261                 else\r
262                 {\r
263                         mtCOVERAGE_TEST_MARKER();\r
264                 }\r
265         }\r
266         #endif\r
267 \r
268         return pvReturn;\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 void vPortFree( void *pv )\r
273 {\r
274 uint8_t *puc = ( uint8_t * ) pv;\r
275 BlockLink_t *pxLink;\r
276 \r
277         if( pv != NULL )\r
278         {\r
279                 /* The memory being freed will have an BlockLink_t structure immediately\r
280                 before it. */\r
281                 puc -= xHeapStructSize;\r
282 \r
283                 /* This casting is to keep the compiler from issuing warnings. */\r
284                 pxLink = ( void * ) puc;\r
285 \r
286                 /* Check the block is actually allocated. */\r
287                 configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );\r
288                 configASSERT( pxLink->pxNextFreeBlock == NULL );\r
289 \r
290                 if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )\r
291                 {\r
292                         if( pxLink->pxNextFreeBlock == NULL )\r
293                         {\r
294                                 /* The block is being returned to the heap - it is no longer\r
295                                 allocated. */\r
296                                 pxLink->xBlockSize &= ~xBlockAllocatedBit;\r
297 \r
298                                 vTaskSuspendAll();\r
299                                 {\r
300                                         /* Add this block to the list of free blocks. */\r
301                                         xFreeBytesRemaining += pxLink->xBlockSize;\r
302                                         traceFREE( pv, pxLink->xBlockSize );\r
303                                         prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );\r
304                                 }\r
305                                 ( void ) xTaskResumeAll();\r
306                         }\r
307                         else\r
308                         {\r
309                                 mtCOVERAGE_TEST_MARKER();\r
310                         }\r
311                 }\r
312                 else\r
313                 {\r
314                         mtCOVERAGE_TEST_MARKER();\r
315                 }\r
316         }\r
317 }\r
318 /*-----------------------------------------------------------*/\r
319 \r
320 size_t xPortGetFreeHeapSize( void )\r
321 {\r
322         return xFreeBytesRemaining;\r
323 }\r
324 /*-----------------------------------------------------------*/\r
325 \r
326 size_t xPortGetMinimumEverFreeHeapSize( void )\r
327 {\r
328         return xMinimumEverFreeBytesRemaining;\r
329 }\r
330 /*-----------------------------------------------------------*/\r
331 \r
332 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )\r
333 {\r
334 BlockLink_t *pxIterator;\r
335 uint8_t *puc;\r
336 \r
337         /* Iterate through the list until a block is found that has a higher address\r
338         than the block being inserted. */\r
339         for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )\r
340         {\r
341                 /* Nothing to do here, just iterate to the right position. */\r
342         }\r
343 \r
344         /* Do the block being inserted, and the block it is being inserted after\r
345         make a contiguous block of memory? */\r
346         puc = ( uint8_t * ) pxIterator;\r
347         if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )\r
348         {\r
349                 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;\r
350                 pxBlockToInsert = pxIterator;\r
351         }\r
352         else\r
353         {\r
354                 mtCOVERAGE_TEST_MARKER();\r
355         }\r
356 \r
357         /* Do the block being inserted, and the block it is being inserted before\r
358         make a contiguous block of memory? */\r
359         puc = ( uint8_t * ) pxBlockToInsert;\r
360         if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )\r
361         {\r
362                 if( pxIterator->pxNextFreeBlock != pxEnd )\r
363                 {\r
364                         /* Form one big block from the two blocks. */\r
365                         pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;\r
366                         pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;\r
367                 }\r
368                 else\r
369                 {\r
370                         pxBlockToInsert->pxNextFreeBlock = pxEnd;\r
371                 }\r
372         }\r
373         else\r
374         {\r
375                 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;\r
376         }\r
377 \r
378         /* If the block being inserted plugged a gab, so was merged with the block\r
379         before and the block after, then it's pxNextFreeBlock pointer will have\r
380         already been set, and should not be set here as that would make it point\r
381         to itself. */\r
382         if( pxIterator != pxBlockToInsert )\r
383         {\r
384                 pxIterator->pxNextFreeBlock = pxBlockToInsert;\r
385         }\r
386         else\r
387         {\r
388                 mtCOVERAGE_TEST_MARKER();\r
389         }\r
390 }\r
391 /*-----------------------------------------------------------*/\r
392 \r
393 void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions )\r
394 {\r
395 BlockLink_t *pxFirstFreeBlockInRegion = NULL, *pxPreviousFreeBlock;\r
396 size_t xAlignedHeap;\r
397 size_t xTotalRegionSize, xTotalHeapSize = 0;\r
398 BaseType_t xDefinedRegions = 0;\r
399 size_t xAddress;\r
400 const HeapRegion_t *pxHeapRegion;\r
401 \r
402         /* Can only call once! */\r
403         configASSERT( pxEnd == NULL );\r
404 \r
405         pxHeapRegion = &( pxHeapRegions[ xDefinedRegions ] );\r
406 \r
407         while( pxHeapRegion->xSizeInBytes > 0 )\r
408         {\r
409                 xTotalRegionSize = pxHeapRegion->xSizeInBytes;\r
410 \r
411                 /* Ensure the heap region starts on a correctly aligned boundary. */\r
412                 xAddress = ( size_t ) pxHeapRegion->pucStartAddress;\r
413                 if( ( xAddress & portBYTE_ALIGNMENT_MASK ) != 0 )\r
414                 {\r
415                         xAddress += ( portBYTE_ALIGNMENT - 1 );\r
416                         xAddress &= ~portBYTE_ALIGNMENT_MASK;\r
417 \r
418                         /* Adjust the size for the bytes lost to alignment. */\r
419                         xTotalRegionSize -= xAddress - ( size_t ) pxHeapRegion->pucStartAddress;\r
420                 }\r
421 \r
422                 xAlignedHeap = xAddress;\r
423 \r
424                 /* Set xStart if it has not already been set. */\r
425                 if( xDefinedRegions == 0 )\r
426                 {\r
427                         /* xStart is used to hold a pointer to the first item in the list of\r
428                         free blocks.  The void cast is used to prevent compiler warnings. */\r
429                         xStart.pxNextFreeBlock = ( BlockLink_t * ) xAlignedHeap;\r
430                         xStart.xBlockSize = ( size_t ) 0;\r
431                 }\r
432                 else\r
433                 {\r
434                         /* Should only get here if one region has already been added to the\r
435                         heap. */\r
436                         configASSERT( pxEnd != NULL );\r
437 \r
438                         /* Check blocks are passed in with increasing start addresses. */\r
439                         configASSERT( xAddress > ( size_t ) pxEnd );\r
440                 }\r
441 \r
442                 /* Remember the location of the end marker in the previous region, if\r
443                 any. */\r
444                 pxPreviousFreeBlock = pxEnd;\r
445 \r
446                 /* pxEnd is used to mark the end of the list of free blocks and is\r
447                 inserted at the end of the region space. */\r
448                 xAddress = xAlignedHeap + xTotalRegionSize;\r
449                 xAddress -= xHeapStructSize;\r
450                 xAddress &= ~portBYTE_ALIGNMENT_MASK;\r
451                 pxEnd = ( BlockLink_t * ) xAddress;\r
452                 pxEnd->xBlockSize = 0;\r
453                 pxEnd->pxNextFreeBlock = NULL;\r
454 \r
455                 /* To start with there is a single free block in this region that is\r
456                 sized to take up the entire heap region minus the space taken by the\r
457                 free block structure. */\r
458                 pxFirstFreeBlockInRegion = ( BlockLink_t * ) xAlignedHeap;\r
459                 pxFirstFreeBlockInRegion->xBlockSize = xAddress - ( size_t ) pxFirstFreeBlockInRegion;\r
460                 pxFirstFreeBlockInRegion->pxNextFreeBlock = pxEnd;\r
461 \r
462                 /* If this is not the first region that makes up the entire heap space\r
463                 then link the previous region to this region. */\r
464                 if( pxPreviousFreeBlock != NULL )\r
465                 {\r
466                         pxPreviousFreeBlock->pxNextFreeBlock = pxFirstFreeBlockInRegion;\r
467                 }\r
468 \r
469                 xTotalHeapSize += pxFirstFreeBlockInRegion->xBlockSize;\r
470 \r
471                 /* Move onto the next HeapRegion_t structure. */\r
472                 xDefinedRegions++;\r
473                 pxHeapRegion = &( pxHeapRegions[ xDefinedRegions ] );\r
474         }\r
475 \r
476         xMinimumEverFreeBytesRemaining = xTotalHeapSize;\r
477         xFreeBytesRemaining = xTotalHeapSize;\r
478 \r
479         /* Check something was actually defined before it is accessed. */\r
480         configASSERT( xTotalHeapSize );\r
481 \r
482         /* Work out the position of the top bit in a size_t variable. */\r
483         xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );\r
484 }\r
485 \r