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