1 /*
2 * FreeRTOS Kernel V10.6.2
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
26 *
27 */
28
29 /*
30 * A sample implementation of pvPortMalloc() and vPortFree() that combines
31 * (coalescences) adjacent memory blocks as they are freed, and in so doing
32 * limits memory fragmentation.
33 *
34 * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the
35 * memory management pages of https://www.FreeRTOS.org for more information.
36 */
37 #include <stdlib.h>
38 #include <string.h>
39
40 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
41 * all the API functions to use the MPU wrappers. That should only be done when
42 * task.h is included from an application file. */
43 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
44
45 #include "FreeRTOS.h"
46 #include "task.h"
47
48 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
49
50 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
51 #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
52 #endif
53
54 #ifndef configHEAP_CLEAR_MEMORY_ON_FREE
55 #define configHEAP_CLEAR_MEMORY_ON_FREE 0
56 #endif
57
58 /* Block sizes must not get too small. */
59 #define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize << 1 ) )
60
61 /* Assumes 8bit bytes! */
62 #define heapBITS_PER_BYTE ( ( size_t ) 8 )
63
64 /* Max value that fits in a size_t type. */
65 #define heapSIZE_MAX ( ~( ( size_t ) 0 ) )
66
67 /* Check if multiplying a and b will result in overflow. */
68 #define heapMULTIPLY_WILL_OVERFLOW( a, b ) ( ( ( a ) > 0 ) && ( ( b ) > ( heapSIZE_MAX / ( a ) ) ) )
69
70 /* Check if adding a and b will result in overflow. */
71 #define heapADD_WILL_OVERFLOW( a, b ) ( ( a ) > ( heapSIZE_MAX - ( b ) ) )
72
73 /* MSB of the xBlockSize member of an BlockLink_t structure is used to track
74 * the allocation status of a block. When MSB of the xBlockSize member of
75 * an BlockLink_t structure is set then the block belongs to the application.
76 * When the bit is free the block is still part of the free heap space. */
77 #define heapBLOCK_ALLOCATED_BITMASK ( ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 ) )
78 #define heapBLOCK_SIZE_IS_VALID( xBlockSize ) ( ( ( xBlockSize ) & heapBLOCK_ALLOCATED_BITMASK ) == 0 )
79 #define heapBLOCK_IS_ALLOCATED( pxBlock ) ( ( ( pxBlock->xBlockSize ) & heapBLOCK_ALLOCATED_BITMASK ) != 0 )
80 #define heapALLOCATE_BLOCK( pxBlock ) ( ( pxBlock->xBlockSize ) |= heapBLOCK_ALLOCATED_BITMASK )
81 #define heapFREE_BLOCK( pxBlock ) ( ( pxBlock->xBlockSize ) &= ~heapBLOCK_ALLOCATED_BITMASK )
82
83 /*-----------------------------------------------------------*/
84
85 /* Allocate the memory for the heap. */
86 #if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
87
88 /* The application writer has already defined the array used for the RTOS
89 * heap - probably so it can be placed in a special segment or address. */
90 extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
91 #else
92 PRIVILEGED_DATA static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
93 #endif /* configAPPLICATION_ALLOCATED_HEAP */
94
95 /* Define the linked list structure. This is used to link free blocks in order
96 * of their memory address. */
97 typedef struct A_BLOCK_LINK
98 {
99 struct A_BLOCK_LINK * pxNextFreeBlock; /**< The next free block in the list. */
100 size_t xBlockSize; /**< The size of the free block. */
101 } BlockLink_t;
102
103 /*-----------------------------------------------------------*/
104
105 /*
106 * Inserts a block of memory that is being freed into the correct position in
107 * the list of free memory blocks. The block being freed will be merged with
108 * the block in front it and/or the block behind it if the memory blocks are
109 * adjacent to each other.
110 */
111 static void prvInsertBlockIntoFreeList( BlockLink_t * pxBlockToInsert ) PRIVILEGED_FUNCTION;
112
113 /*
114 * Called automatically to setup the required heap structures the first time
115 * pvPortMalloc() is called.
116 */
117 static void prvHeapInit( void ) PRIVILEGED_FUNCTION;
118
119 /*-----------------------------------------------------------*/
120
121 /* The size of the structure placed at the beginning of each allocated memory
122 * block must by correctly byte aligned. */
123 static const size_t xHeapStructSize = ( sizeof( BlockLink_t ) + ( ( size_t ) ( portBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
124
125 /* Create a couple of list links to mark the start and end of the list. */
126 PRIVILEGED_DATA static BlockLink_t xStart;
127 PRIVILEGED_DATA static BlockLink_t * pxEnd = NULL;
128
129 /* Keeps track of the number of calls to allocate and free memory as well as the
130 * number of free bytes remaining, but says nothing about fragmentation. */
131 PRIVILEGED_DATA static size_t xFreeBytesRemaining = 0U;
132 PRIVILEGED_DATA static size_t xMinimumEverFreeBytesRemaining = 0U;
133 PRIVILEGED_DATA static size_t xNumberOfSuccessfulAllocations = 0;
134 PRIVILEGED_DATA static size_t xNumberOfSuccessfulFrees = 0;
135
136 /*-----------------------------------------------------------*/
137
pvPortMalloc(size_t xWantedSize)138 void * pvPortMalloc( size_t xWantedSize )
139 {
140 BlockLink_t * pxBlock;
141 BlockLink_t * pxPreviousBlock;
142 BlockLink_t * pxNewBlockLink;
143 void * pvReturn = NULL;
144 size_t xAdditionalRequiredSize;
145
146 vTaskSuspendAll();
147 {
148 /* If this is the first call to malloc then the heap will require
149 * initialisation to setup the list of free blocks. */
150 if( pxEnd == NULL )
151 {
152 prvHeapInit();
153 }
154 else
155 {
156 mtCOVERAGE_TEST_MARKER();
157 }
158
159 if( xWantedSize > 0 )
160 {
161 /* The wanted size must be increased so it can contain a BlockLink_t
162 * structure in addition to the requested amount of bytes. */
163 if( heapADD_WILL_OVERFLOW( xWantedSize, xHeapStructSize ) == 0 )
164 {
165 xWantedSize += xHeapStructSize;
166
167 /* Ensure that blocks are always aligned to the required number
168 * of bytes. */
169 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
170 {
171 /* Byte alignment required. */
172 xAdditionalRequiredSize = portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK );
173
174 if( heapADD_WILL_OVERFLOW( xWantedSize, xAdditionalRequiredSize ) == 0 )
175 {
176 xWantedSize += xAdditionalRequiredSize;
177 }
178 else
179 {
180 xWantedSize = 0;
181 }
182 }
183 else
184 {
185 mtCOVERAGE_TEST_MARKER();
186 }
187 }
188 else
189 {
190 xWantedSize = 0;
191 }
192 }
193 else
194 {
195 mtCOVERAGE_TEST_MARKER();
196 }
197
198 /* Check the block size we are trying to allocate is not so large that the
199 * top bit is set. The top bit of the block size member of the BlockLink_t
200 * structure is used to determine who owns the block - the application or
201 * the kernel, so it must be free. */
202 if( heapBLOCK_SIZE_IS_VALID( xWantedSize ) != 0 )
203 {
204 if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
205 {
206 /* Traverse the list from the start (lowest address) block until
207 * one of adequate size is found. */
208 pxPreviousBlock = &xStart;
209 pxBlock = xStart.pxNextFreeBlock;
210
211 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
212 {
213 pxPreviousBlock = pxBlock;
214 pxBlock = pxBlock->pxNextFreeBlock;
215 }
216
217 /* If the end marker was reached then a block of adequate size
218 * was not found. */
219 if( pxBlock != pxEnd )
220 {
221 /* Return the memory space pointed to - jumping over the
222 * BlockLink_t structure at its start. */
223 pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );
224
225 /* This block is being returned for use so must be taken out
226 * of the list of free blocks. */
227 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
228
229 /* If the block is larger than required it can be split into
230 * two. */
231 if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
232 {
233 /* This block is to be split into two. Create a new
234 * block following the number of bytes requested. The void
235 * cast is used to prevent byte alignment warnings from the
236 * compiler. */
237 pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
238 configASSERT( ( ( ( size_t ) pxNewBlockLink ) & portBYTE_ALIGNMENT_MASK ) == 0 );
239
240 /* Calculate the sizes of two blocks split from the
241 * single block. */
242 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
243 pxBlock->xBlockSize = xWantedSize;
244
245 /* Insert the new block into the list of free blocks. */
246 prvInsertBlockIntoFreeList( pxNewBlockLink );
247 }
248 else
249 {
250 mtCOVERAGE_TEST_MARKER();
251 }
252
253 xFreeBytesRemaining -= pxBlock->xBlockSize;
254
255 if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
256 {
257 xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
258 }
259 else
260 {
261 mtCOVERAGE_TEST_MARKER();
262 }
263
264 /* The block is being returned - it is allocated and owned
265 * by the application and has no "next" block. */
266 heapALLOCATE_BLOCK( pxBlock );
267 pxBlock->pxNextFreeBlock = NULL;
268 xNumberOfSuccessfulAllocations++;
269 }
270 else
271 {
272 mtCOVERAGE_TEST_MARKER();
273 }
274 }
275 else
276 {
277 mtCOVERAGE_TEST_MARKER();
278 }
279 }
280 else
281 {
282 mtCOVERAGE_TEST_MARKER();
283 }
284
285 traceMALLOC( pvReturn, xWantedSize );
286 }
287 ( void ) xTaskResumeAll();
288
289 #if ( configUSE_MALLOC_FAILED_HOOK == 1 )
290 {
291 if( pvReturn == NULL )
292 {
293 vApplicationMallocFailedHook();
294 }
295 else
296 {
297 mtCOVERAGE_TEST_MARKER();
298 }
299 }
300 #endif /* if ( configUSE_MALLOC_FAILED_HOOK == 1 ) */
301
302 configASSERT( ( ( ( size_t ) pvReturn ) & ( size_t ) portBYTE_ALIGNMENT_MASK ) == 0 );
303 return pvReturn;
304 }
305 /*-----------------------------------------------------------*/
306
vPortFree(void * pv)307 void vPortFree( void * pv )
308 {
309 uint8_t * puc = ( uint8_t * ) pv;
310 BlockLink_t * pxLink;
311
312 if( pv != NULL )
313 {
314 /* The memory being freed will have an BlockLink_t structure immediately
315 * before it. */
316 puc -= xHeapStructSize;
317
318 /* This casting is to keep the compiler from issuing warnings. */
319 pxLink = ( void * ) puc;
320
321 configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 );
322 configASSERT( pxLink->pxNextFreeBlock == NULL );
323
324 if( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 )
325 {
326 if( pxLink->pxNextFreeBlock == NULL )
327 {
328 /* The block is being returned to the heap - it is no longer
329 * allocated. */
330 heapFREE_BLOCK( pxLink );
331 #if ( configHEAP_CLEAR_MEMORY_ON_FREE == 1 )
332 {
333 ( void ) memset( puc + xHeapStructSize, 0, pxLink->xBlockSize - xHeapStructSize );
334 }
335 #endif
336
337 vTaskSuspendAll();
338 {
339 /* Add this block to the list of free blocks. */
340 xFreeBytesRemaining += pxLink->xBlockSize;
341 traceFREE( pv, pxLink->xBlockSize );
342 prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
343 xNumberOfSuccessfulFrees++;
344 }
345 ( void ) xTaskResumeAll();
346 }
347 else
348 {
349 mtCOVERAGE_TEST_MARKER();
350 }
351 }
352 else
353 {
354 mtCOVERAGE_TEST_MARKER();
355 }
356 }
357 }
358 /*-----------------------------------------------------------*/
359
xPortGetFreeHeapSize(void)360 size_t xPortGetFreeHeapSize( void )
361 {
362 return xFreeBytesRemaining;
363 }
364 /*-----------------------------------------------------------*/
365
xPortGetMinimumEverFreeHeapSize(void)366 size_t xPortGetMinimumEverFreeHeapSize( void )
367 {
368 return xMinimumEverFreeBytesRemaining;
369 }
370 /*-----------------------------------------------------------*/
371
vPortInitialiseBlocks(void)372 void vPortInitialiseBlocks( void )
373 {
374 /* This just exists to keep the linker quiet. */
375 }
376 /*-----------------------------------------------------------*/
377
pvPortCalloc(size_t xNum,size_t xSize)378 void * pvPortCalloc( size_t xNum,
379 size_t xSize )
380 {
381 void * pv = NULL;
382
383 if( heapMULTIPLY_WILL_OVERFLOW( xNum, xSize ) == 0 )
384 {
385 pv = pvPortMalloc( xNum * xSize );
386
387 if( pv != NULL )
388 {
389 ( void ) memset( pv, 0, xNum * xSize );
390 }
391 }
392
393 return pv;
394 }
395 /*-----------------------------------------------------------*/
396
prvHeapInit(void)397 static void prvHeapInit( void ) /* PRIVILEGED_FUNCTION */
398 {
399 BlockLink_t * pxFirstFreeBlock;
400 uint8_t * pucAlignedHeap;
401 portPOINTER_SIZE_TYPE uxAddress;
402 size_t xTotalHeapSize = configTOTAL_HEAP_SIZE;
403
404 /* Ensure the heap starts on a correctly aligned boundary. */
405 uxAddress = ( portPOINTER_SIZE_TYPE ) ucHeap;
406
407 if( ( uxAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
408 {
409 uxAddress += ( portBYTE_ALIGNMENT - 1 );
410 uxAddress &= ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK );
411 xTotalHeapSize -= ( size_t ) ( uxAddress - ( portPOINTER_SIZE_TYPE ) ucHeap );
412 }
413
414 pucAlignedHeap = ( uint8_t * ) uxAddress;
415
416 /* xStart is used to hold a pointer to the first item in the list of free
417 * blocks. The void cast is used to prevent compiler warnings. */
418 xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
419 xStart.xBlockSize = ( size_t ) 0;
420
421 /* pxEnd is used to mark the end of the list of free blocks and is inserted
422 * at the end of the heap space. */
423 uxAddress = ( portPOINTER_SIZE_TYPE ) ( pucAlignedHeap + xTotalHeapSize );
424 uxAddress -= xHeapStructSize;
425 uxAddress &= ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK );
426 pxEnd = ( BlockLink_t * ) uxAddress;
427 pxEnd->xBlockSize = 0;
428 pxEnd->pxNextFreeBlock = NULL;
429
430 /* To start with there is a single free block that is sized to take up the
431 * entire heap space, minus the space taken by pxEnd. */
432 pxFirstFreeBlock = ( BlockLink_t * ) pucAlignedHeap;
433 pxFirstFreeBlock->xBlockSize = ( size_t ) ( uxAddress - ( portPOINTER_SIZE_TYPE ) pxFirstFreeBlock );
434 pxFirstFreeBlock->pxNextFreeBlock = pxEnd;
435
436 /* Only one block exists - and it covers the entire usable heap space. */
437 xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
438 xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
439 }
440 /*-----------------------------------------------------------*/
441
prvInsertBlockIntoFreeList(BlockLink_t * pxBlockToInsert)442 static void prvInsertBlockIntoFreeList( BlockLink_t * pxBlockToInsert ) /* PRIVILEGED_FUNCTION */
443 {
444 BlockLink_t * pxIterator;
445 uint8_t * puc;
446
447 /* Iterate through the list until a block is found that has a higher address
448 * than the block being inserted. */
449 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
450 {
451 /* Nothing to do here, just iterate to the right position. */
452 }
453
454 /* Do the block being inserted, and the block it is being inserted after
455 * make a contiguous block of memory? */
456 puc = ( uint8_t * ) pxIterator;
457
458 if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )
459 {
460 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
461 pxBlockToInsert = pxIterator;
462 }
463 else
464 {
465 mtCOVERAGE_TEST_MARKER();
466 }
467
468 /* Do the block being inserted, and the block it is being inserted before
469 * make a contiguous block of memory? */
470 puc = ( uint8_t * ) pxBlockToInsert;
471
472 if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )
473 {
474 if( pxIterator->pxNextFreeBlock != pxEnd )
475 {
476 /* Form one big block from the two blocks. */
477 pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
478 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
479 }
480 else
481 {
482 pxBlockToInsert->pxNextFreeBlock = pxEnd;
483 }
484 }
485 else
486 {
487 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
488 }
489
490 /* If the block being inserted plugged a gab, so was merged with the block
491 * before and the block after, then it's pxNextFreeBlock pointer will have
492 * already been set, and should not be set here as that would make it point
493 * to itself. */
494 if( pxIterator != pxBlockToInsert )
495 {
496 pxIterator->pxNextFreeBlock = pxBlockToInsert;
497 }
498 else
499 {
500 mtCOVERAGE_TEST_MARKER();
501 }
502 }
503 /*-----------------------------------------------------------*/
504
vPortGetHeapStats(HeapStats_t * pxHeapStats)505 void vPortGetHeapStats( HeapStats_t * pxHeapStats )
506 {
507 BlockLink_t * pxBlock;
508 size_t xBlocks = 0, xMaxSize = 0, xMinSize = portMAX_DELAY; /* portMAX_DELAY used as a portable way of getting the maximum value. */
509
510 vTaskSuspendAll();
511 {
512 pxBlock = xStart.pxNextFreeBlock;
513
514 /* pxBlock will be NULL if the heap has not been initialised. The heap
515 * is initialised automatically when the first allocation is made. */
516 if( pxBlock != NULL )
517 {
518 while( pxBlock != pxEnd )
519 {
520 /* Increment the number of blocks and record the largest block seen
521 * so far. */
522 xBlocks++;
523
524 if( pxBlock->xBlockSize > xMaxSize )
525 {
526 xMaxSize = pxBlock->xBlockSize;
527 }
528
529 if( pxBlock->xBlockSize < xMinSize )
530 {
531 xMinSize = pxBlock->xBlockSize;
532 }
533
534 /* Move to the next block in the chain until the last block is
535 * reached. */
536 pxBlock = pxBlock->pxNextFreeBlock;
537 }
538 }
539 }
540 ( void ) xTaskResumeAll();
541
542 pxHeapStats->xSizeOfLargestFreeBlockInBytes = xMaxSize;
543 pxHeapStats->xSizeOfSmallestFreeBlockInBytes = xMinSize;
544 pxHeapStats->xNumberOfFreeBlocks = xBlocks;
545
546 taskENTER_CRITICAL();
547 {
548 pxHeapStats->xAvailableHeapSpaceInBytes = xFreeBytesRemaining;
549 pxHeapStats->xNumberOfSuccessfulAllocations = xNumberOfSuccessfulAllocations;
550 pxHeapStats->xNumberOfSuccessfulFrees = xNumberOfSuccessfulFrees;
551 pxHeapStats->xMinimumEverFreeBytesRemaining = xMinimumEverFreeBytesRemaining;
552 }
553 taskEXIT_CRITICAL();
554 }
555 /*-----------------------------------------------------------*/
556