]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MemMang/heap_4.c
Update version numbers ready for release.
[freertos] / FreeRTOS / Source / portable / MemMang / heap_4.c
1 /*\r
2  * FreeRTOS Kernel V10.1.1\r
3  * Copyright (C) 2018 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() and vPortFree() that combines\r
30  * (coalescences) adjacent memory blocks as they are freed, and in so doing\r
31  * limits memory fragmentation.\r
32  *\r
33  * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the\r
34  * memory management pages of http://www.FreeRTOS.org for more information.\r
35  */\r
36 #include <stdlib.h>\r
37 \r
38 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
39 all the API functions to use the MPU wrappers.  That should only be done when\r
40 task.h is included from an application file. */\r
41 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
42 \r
43 #include "FreeRTOS.h"\r
44 #include "task.h"\r
45 \r
46 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
47 \r
48 #if( configSUPPORT_DYNAMIC_ALLOCATION == 0 )\r
49         #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0\r
50 #endif\r
51 \r
52 /* Block sizes must not get too small. */\r
53 #define heapMINIMUM_BLOCK_SIZE  ( ( size_t ) ( xHeapStructSize << 1 ) )\r
54 \r
55 /* Assumes 8bit bytes! */\r
56 #define heapBITS_PER_BYTE               ( ( size_t ) 8 )\r
57 \r
58 /* Allocate the memory for the heap. */\r
59 #if( configAPPLICATION_ALLOCATED_HEAP == 1 )\r
60         /* The application writer has already defined the array used for the RTOS\r
61         heap - probably so it can be placed in a special segment or address. */\r
62         extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
63 #else\r
64         static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
65 #endif /* configAPPLICATION_ALLOCATED_HEAP */\r
66 \r
67 /* Define the linked list structure.  This is used to link free blocks in order\r
68 of their memory address. */\r
69 typedef struct A_BLOCK_LINK\r
70 {\r
71         struct A_BLOCK_LINK *pxNextFreeBlock;   /*<< The next free block in the list. */\r
72         size_t xBlockSize;                                              /*<< The size of the free block. */\r
73 } BlockLink_t;\r
74 \r
75 /*-----------------------------------------------------------*/\r
76 \r
77 /*\r
78  * Inserts a block of memory that is being freed into the correct position in\r
79  * the list of free memory blocks.  The block being freed will be merged with\r
80  * the block in front it and/or the block behind it if the memory blocks are\r
81  * adjacent to each other.\r
82  */\r
83 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert );\r
84 \r
85 /*\r
86  * Called automatically to setup the required heap structures the first time\r
87  * pvPortMalloc() is called.\r
88  */\r
89 static void prvHeapInit( void );\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /* The size of the structure placed at the beginning of each allocated memory\r
94 block must by correctly byte aligned. */\r
95 static const size_t xHeapStructSize     = ( sizeof( BlockLink_t ) + ( ( size_t ) ( portBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) portBYTE_ALIGNMENT_MASK );\r
96 \r
97 /* Create a couple of list links to mark the start and end of the list. */\r
98 static BlockLink_t xStart, *pxEnd = NULL;\r
99 \r
100 /* Keeps track of the number of free bytes remaining, but says nothing about\r
101 fragmentation. */\r
102 static size_t xFreeBytesRemaining = 0U;\r
103 static size_t xMinimumEverFreeBytesRemaining = 0U;\r
104 \r
105 /* Gets set to the top bit of an size_t type.  When this bit in the xBlockSize\r
106 member of an BlockLink_t structure is set then the block belongs to the\r
107 application.  When the bit is free the block is still part of the free heap\r
108 space. */\r
109 static size_t xBlockAllocatedBit = 0;\r
110 \r
111 /*-----------------------------------------------------------*/\r
112 \r
113 void *pvPortMalloc( size_t xWantedSize )\r
114 {\r
115 BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;\r
116 void *pvReturn = NULL;\r
117 \r
118         vTaskSuspendAll();\r
119         {\r
120                 /* If this is the first call to malloc then the heap will require\r
121                 initialisation to setup the list of free blocks. */\r
122                 if( pxEnd == NULL )\r
123                 {\r
124                         prvHeapInit();\r
125                 }\r
126                 else\r
127                 {\r
128                         mtCOVERAGE_TEST_MARKER();\r
129                 }\r
130 \r
131                 /* Check the requested block size is not so large that the top bit is\r
132                 set.  The top bit of the block size member of the BlockLink_t structure\r
133                 is used to determine who owns the block - the application or the\r
134                 kernel, so it must be free. */\r
135                 if( ( xWantedSize & xBlockAllocatedBit ) == 0 )\r
136                 {\r
137                         /* The wanted size is increased so it can contain a BlockLink_t\r
138                         structure in addition to the requested amount of bytes. */\r
139                         if( xWantedSize > 0 )\r
140                         {\r
141                                 xWantedSize += xHeapStructSize;\r
142 \r
143                                 /* Ensure that blocks are always aligned to the required number\r
144                                 of bytes. */\r
145                                 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )\r
146                                 {\r
147                                         /* Byte alignment required. */\r
148                                         xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );\r
149                                         configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );\r
150                                 }\r
151                                 else\r
152                                 {\r
153                                         mtCOVERAGE_TEST_MARKER();\r
154                                 }\r
155                         }\r
156                         else\r
157                         {\r
158                                 mtCOVERAGE_TEST_MARKER();\r
159                         }\r
160 \r
161                         if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )\r
162                         {\r
163                                 /* Traverse the list from the start     (lowest address) block until\r
164                                 one     of adequate size is found. */\r
165                                 pxPreviousBlock = &xStart;\r
166                                 pxBlock = xStart.pxNextFreeBlock;\r
167                                 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )\r
168                                 {\r
169                                         pxPreviousBlock = pxBlock;\r
170                                         pxBlock = pxBlock->pxNextFreeBlock;\r
171                                 }\r
172 \r
173                                 /* If the end marker was reached then a block of adequate size\r
174                                 was     not found. */\r
175                                 if( pxBlock != pxEnd )\r
176                                 {\r
177                                         /* Return the memory space pointed to - jumping over the\r
178                                         BlockLink_t structure at its start. */\r
179                                         pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );\r
180 \r
181                                         /* This block is being returned for use so must be taken out\r
182                                         of the list of free blocks. */\r
183                                         pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;\r
184 \r
185                                         /* If the block is larger than required it can be split into\r
186                                         two. */\r
187                                         if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )\r
188                                         {\r
189                                                 /* This block is to be split into two.  Create a new\r
190                                                 block following the number of bytes requested. The void\r
191                                                 cast is used to prevent byte alignment warnings from the\r
192                                                 compiler. */\r
193                                                 pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );\r
194                                                 configASSERT( ( ( ( size_t ) pxNewBlockLink ) & portBYTE_ALIGNMENT_MASK ) == 0 );\r
195 \r
196                                                 /* Calculate the sizes of two blocks split from the\r
197                                                 single block. */\r
198                                                 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;\r
199                                                 pxBlock->xBlockSize = xWantedSize;\r
200 \r
201                                                 /* Insert the new block into the list of free blocks. */\r
202                                                 prvInsertBlockIntoFreeList( pxNewBlockLink );\r
203                                         }\r
204                                         else\r
205                                         {\r
206                                                 mtCOVERAGE_TEST_MARKER();\r
207                                         }\r
208 \r
209                                         xFreeBytesRemaining -= pxBlock->xBlockSize;\r
210 \r
211                                         if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )\r
212                                         {\r
213                                                 xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;\r
214                                         }\r
215                                         else\r
216                                         {\r
217                                                 mtCOVERAGE_TEST_MARKER();\r
218                                         }\r
219 \r
220                                         /* The block is being returned - it is allocated and owned\r
221                                         by the application and has no "next" block. */\r
222                                         pxBlock->xBlockSize |= xBlockAllocatedBit;\r
223                                         pxBlock->pxNextFreeBlock = NULL;\r
224                                 }\r
225                                 else\r
226                                 {\r
227                                         mtCOVERAGE_TEST_MARKER();\r
228                                 }\r
229                         }\r
230                         else\r
231                         {\r
232                                 mtCOVERAGE_TEST_MARKER();\r
233                         }\r
234                 }\r
235                 else\r
236                 {\r
237                         mtCOVERAGE_TEST_MARKER();\r
238                 }\r
239 \r
240                 traceMALLOC( pvReturn, xWantedSize );\r
241         }\r
242         ( void ) xTaskResumeAll();\r
243 \r
244         #if( configUSE_MALLOC_FAILED_HOOK == 1 )\r
245         {\r
246                 if( pvReturn == NULL )\r
247                 {\r
248                         extern void vApplicationMallocFailedHook( void );\r
249                         vApplicationMallocFailedHook();\r
250                 }\r
251                 else\r
252                 {\r
253                         mtCOVERAGE_TEST_MARKER();\r
254                 }\r
255         }\r
256         #endif\r
257 \r
258         configASSERT( ( ( ( size_t ) pvReturn ) & ( size_t ) portBYTE_ALIGNMENT_MASK ) == 0 );\r
259         return pvReturn;\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 void vPortFree( void *pv )\r
264 {\r
265 uint8_t *puc = ( uint8_t * ) pv;\r
266 BlockLink_t *pxLink;\r
267 \r
268         if( pv != NULL )\r
269         {\r
270                 /* The memory being freed will have an BlockLink_t structure immediately\r
271                 before it. */\r
272                 puc -= xHeapStructSize;\r
273 \r
274                 /* This casting is to keep the compiler from issuing warnings. */\r
275                 pxLink = ( void * ) puc;\r
276 \r
277                 /* Check the block is actually allocated. */\r
278                 configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );\r
279                 configASSERT( pxLink->pxNextFreeBlock == NULL );\r
280 \r
281                 if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )\r
282                 {\r
283                         if( pxLink->pxNextFreeBlock == NULL )\r
284                         {\r
285                                 /* The block is being returned to the heap - it is no longer\r
286                                 allocated. */\r
287                                 pxLink->xBlockSize &= ~xBlockAllocatedBit;\r
288 \r
289                                 vTaskSuspendAll();\r
290                                 {\r
291                                         /* Add this block to the list of free blocks. */\r
292                                         xFreeBytesRemaining += pxLink->xBlockSize;\r
293                                         traceFREE( pv, pxLink->xBlockSize );\r
294                                         prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );\r
295                                 }\r
296                                 ( void ) xTaskResumeAll();\r
297                         }\r
298                         else\r
299                         {\r
300                                 mtCOVERAGE_TEST_MARKER();\r
301                         }\r
302                 }\r
303                 else\r
304                 {\r
305                         mtCOVERAGE_TEST_MARKER();\r
306                 }\r
307         }\r
308 }\r
309 /*-----------------------------------------------------------*/\r
310 \r
311 size_t xPortGetFreeHeapSize( void )\r
312 {\r
313         return xFreeBytesRemaining;\r
314 }\r
315 /*-----------------------------------------------------------*/\r
316 \r
317 size_t xPortGetMinimumEverFreeHeapSize( void )\r
318 {\r
319         return xMinimumEverFreeBytesRemaining;\r
320 }\r
321 /*-----------------------------------------------------------*/\r
322 \r
323 void vPortInitialiseBlocks( void )\r
324 {\r
325         /* This just exists to keep the linker quiet. */\r
326 }\r
327 /*-----------------------------------------------------------*/\r
328 \r
329 static void prvHeapInit( void )\r
330 {\r
331 BlockLink_t *pxFirstFreeBlock;\r
332 uint8_t *pucAlignedHeap;\r
333 size_t uxAddress;\r
334 size_t xTotalHeapSize = configTOTAL_HEAP_SIZE;\r
335 \r
336         /* Ensure the heap starts on a correctly aligned boundary. */\r
337         uxAddress = ( size_t ) ucHeap;\r
338 \r
339         if( ( uxAddress & portBYTE_ALIGNMENT_MASK ) != 0 )\r
340         {\r
341                 uxAddress += ( portBYTE_ALIGNMENT - 1 );\r
342                 uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK );\r
343                 xTotalHeapSize -= uxAddress - ( size_t ) ucHeap;\r
344         }\r
345 \r
346         pucAlignedHeap = ( uint8_t * ) uxAddress;\r
347 \r
348         /* xStart is used to hold a pointer to the first item in the list of free\r
349         blocks.  The void cast is used to prevent compiler warnings. */\r
350         xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;\r
351         xStart.xBlockSize = ( size_t ) 0;\r
352 \r
353         /* pxEnd is used to mark the end of the list of free blocks and is inserted\r
354         at the end of the heap space. */\r
355         uxAddress = ( ( size_t ) pucAlignedHeap ) + xTotalHeapSize;\r
356         uxAddress -= xHeapStructSize;\r
357         uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK );\r
358         pxEnd = ( void * ) uxAddress;\r
359         pxEnd->xBlockSize = 0;\r
360         pxEnd->pxNextFreeBlock = NULL;\r
361 \r
362         /* To start with there is a single free block that is sized to take up the\r
363         entire heap space, minus the space taken by pxEnd. */\r
364         pxFirstFreeBlock = ( void * ) pucAlignedHeap;\r
365         pxFirstFreeBlock->xBlockSize = uxAddress - ( size_t ) pxFirstFreeBlock;\r
366         pxFirstFreeBlock->pxNextFreeBlock = pxEnd;\r
367 \r
368         /* Only one block exists - and it covers the entire usable heap space. */\r
369         xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;\r
370         xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;\r
371 \r
372         /* Work out the position of the top bit in a size_t variable. */\r
373         xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );\r
374 }\r
375 /*-----------------------------------------------------------*/\r
376 \r
377 static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )\r
378 {\r
379 BlockLink_t *pxIterator;\r
380 uint8_t *puc;\r
381 \r
382         /* Iterate through the list until a block is found that has a higher address\r
383         than the block being inserted. */\r
384         for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )\r
385         {\r
386                 /* Nothing to do here, just iterate to the right position. */\r
387         }\r
388 \r
389         /* Do the block being inserted, and the block it is being inserted after\r
390         make a contiguous block of memory? */\r
391         puc = ( uint8_t * ) pxIterator;\r
392         if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )\r
393         {\r
394                 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;\r
395                 pxBlockToInsert = pxIterator;\r
396         }\r
397         else\r
398         {\r
399                 mtCOVERAGE_TEST_MARKER();\r
400         }\r
401 \r
402         /* Do the block being inserted, and the block it is being inserted before\r
403         make a contiguous block of memory? */\r
404         puc = ( uint8_t * ) pxBlockToInsert;\r
405         if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )\r
406         {\r
407                 if( pxIterator->pxNextFreeBlock != pxEnd )\r
408                 {\r
409                         /* Form one big block from the two blocks. */\r
410                         pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;\r
411                         pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;\r
412                 }\r
413                 else\r
414                 {\r
415                         pxBlockToInsert->pxNextFreeBlock = pxEnd;\r
416                 }\r
417         }\r
418         else\r
419         {\r
420                 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;\r
421         }\r
422 \r
423         /* If the block being inserted plugged a gab, so was merged with the block\r
424         before and the block after, then it's pxNextFreeBlock pointer will have\r
425         already been set, and should not be set here as that would make it point\r
426         to itself. */\r
427         if( pxIterator != pxBlockToInsert )\r
428         {\r
429                 pxIterator->pxNextFreeBlock = pxBlockToInsert;\r
430         }\r
431         else\r
432         {\r
433                 mtCOVERAGE_TEST_MARKER();\r
434         }\r
435 }\r
436 \r