2 * FreeRTOS Kernel V10.5.1
\r
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * SPDX-License-Identifier: MIT
\r
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
8 * this software and associated documentation files (the "Software"), to deal in
\r
9 * the Software without restriction, including without limitation the rights to
\r
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
11 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
12 * subject to the following conditions:
\r
14 * The above copyright notice and this permission notice shall be included in all
\r
15 * copies or substantial portions of the Software.
\r
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * https://www.FreeRTOS.org
\r
25 * https://github.com/FreeRTOS
\r
29 /* Standard includes. */
\r
32 /* Secure context heap includes. */
\r
33 #include "secure_heap.h"
\r
35 /* Secure port macros. */
\r
36 #include "secure_port_macros.h"
\r
39 * @brief Total heap size.
\r
41 #ifndef secureconfigTOTAL_HEAP_SIZE
\r
42 #define secureconfigTOTAL_HEAP_SIZE ( ( ( size_t ) ( 10 * 1024 ) ) )
\r
45 /* No test marker by default. */
\r
46 #ifndef mtCOVERAGE_TEST_MARKER
\r
47 #define mtCOVERAGE_TEST_MARKER()
\r
50 /* No tracing by default. */
\r
52 #define traceMALLOC( pvReturn, xWantedSize )
\r
55 /* No tracing by default. */
\r
57 #define traceFREE( pv, xBlockSize )
\r
60 /* Block sizes must not get too small. */
\r
61 #define secureheapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize << 1 ) )
\r
63 /* Assumes 8bit bytes! */
\r
64 #define secureheapBITS_PER_BYTE ( ( size_t ) 8 )
\r
65 /*-----------------------------------------------------------*/
\r
67 /* Allocate the memory for the heap. */
\r
68 #if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
\r
70 /* The application writer has already defined the array used for the RTOS
\r
71 * heap - probably so it can be placed in a special segment or address. */
\r
72 extern uint8_t ucHeap[ secureconfigTOTAL_HEAP_SIZE ];
\r
73 #else /* configAPPLICATION_ALLOCATED_HEAP */
\r
74 static uint8_t ucHeap[ secureconfigTOTAL_HEAP_SIZE ];
\r
75 #endif /* configAPPLICATION_ALLOCATED_HEAP */
\r
78 * @brief The linked list structure.
\r
80 * This is used to link free blocks in order of their memory address.
\r
82 typedef struct A_BLOCK_LINK
\r
84 struct A_BLOCK_LINK * pxNextFreeBlock; /**< The next free block in the list. */
\r
85 size_t xBlockSize; /**< The size of the free block. */
\r
87 /*-----------------------------------------------------------*/
\r
90 * @brief Called automatically to setup the required heap structures the first
\r
91 * time pvPortMalloc() is called.
\r
93 static void prvHeapInit( void );
\r
96 * @brief Inserts a block of memory that is being freed into the correct
\r
97 * position in the list of free memory blocks.
\r
99 * The block being freed will be merged with the block in front it and/or the
\r
100 * block behind it if the memory blocks are adjacent to each other.
\r
102 * @param[in] pxBlockToInsert The block being freed.
\r
104 static void prvInsertBlockIntoFreeList( BlockLink_t * pxBlockToInsert );
\r
105 /*-----------------------------------------------------------*/
\r
108 * @brief The size of the structure placed at the beginning of each allocated
\r
109 * memory block must by correctly byte aligned.
\r
111 static const size_t xHeapStructSize = ( sizeof( BlockLink_t ) + ( ( size_t ) ( secureportBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) secureportBYTE_ALIGNMENT_MASK );
\r
114 * @brief Create a couple of list links to mark the start and end of the list.
\r
116 static BlockLink_t xStart;
\r
117 static BlockLink_t * pxEnd = NULL;
\r
120 * @brief Keeps track of the number of free bytes remaining, but says nothing
\r
121 * about fragmentation.
\r
123 static size_t xFreeBytesRemaining = 0U;
\r
124 static size_t xMinimumEverFreeBytesRemaining = 0U;
\r
127 * @brief Gets set to the top bit of an size_t type.
\r
129 * When this bit in the xBlockSize member of an BlockLink_t structure is set
\r
130 * then the block belongs to the application. When the bit is free the block is
\r
131 * still part of the free heap space.
\r
133 static size_t xBlockAllocatedBit = 0;
\r
134 /*-----------------------------------------------------------*/
\r
136 static void prvHeapInit( void )
\r
138 BlockLink_t * pxFirstFreeBlock;
\r
139 uint8_t * pucAlignedHeap;
\r
141 size_t xTotalHeapSize = secureconfigTOTAL_HEAP_SIZE;
\r
143 /* Ensure the heap starts on a correctly aligned boundary. */
\r
144 uxAddress = ( size_t ) ucHeap;
\r
146 if( ( uxAddress & secureportBYTE_ALIGNMENT_MASK ) != 0 )
\r
148 uxAddress += ( secureportBYTE_ALIGNMENT - 1 );
\r
149 uxAddress &= ~( ( size_t ) secureportBYTE_ALIGNMENT_MASK );
\r
150 xTotalHeapSize -= uxAddress - ( size_t ) ucHeap;
\r
153 pucAlignedHeap = ( uint8_t * ) uxAddress;
\r
155 /* xStart is used to hold a pointer to the first item in the list of free
\r
156 * blocks. The void cast is used to prevent compiler warnings. */
\r
157 xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
\r
158 xStart.xBlockSize = ( size_t ) 0;
\r
160 /* pxEnd is used to mark the end of the list of free blocks and is inserted
\r
161 * at the end of the heap space. */
\r
162 uxAddress = ( ( size_t ) pucAlignedHeap ) + xTotalHeapSize;
\r
163 uxAddress -= xHeapStructSize;
\r
164 uxAddress &= ~( ( size_t ) secureportBYTE_ALIGNMENT_MASK );
\r
165 pxEnd = ( void * ) uxAddress;
\r
166 pxEnd->xBlockSize = 0;
\r
167 pxEnd->pxNextFreeBlock = NULL;
\r
169 /* To start with there is a single free block that is sized to take up the
\r
170 * entire heap space, minus the space taken by pxEnd. */
\r
171 pxFirstFreeBlock = ( void * ) pucAlignedHeap;
\r
172 pxFirstFreeBlock->xBlockSize = uxAddress - ( size_t ) pxFirstFreeBlock;
\r
173 pxFirstFreeBlock->pxNextFreeBlock = pxEnd;
\r
175 /* Only one block exists - and it covers the entire usable heap space. */
\r
176 xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
\r
177 xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
\r
179 /* Work out the position of the top bit in a size_t variable. */
\r
180 xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * secureheapBITS_PER_BYTE ) - 1 );
\r
182 /*-----------------------------------------------------------*/
\r
184 static void prvInsertBlockIntoFreeList( BlockLink_t * pxBlockToInsert )
\r
186 BlockLink_t * pxIterator;
\r
189 /* Iterate through the list until a block is found that has a higher address
\r
190 * than the block being inserted. */
\r
191 for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
\r
193 /* Nothing to do here, just iterate to the right position. */
\r
196 /* Do the block being inserted, and the block it is being inserted after
\r
197 * make a contiguous block of memory? */
\r
198 puc = ( uint8_t * ) pxIterator;
\r
200 if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )
\r
202 pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
\r
203 pxBlockToInsert = pxIterator;
\r
207 mtCOVERAGE_TEST_MARKER();
\r
210 /* Do the block being inserted, and the block it is being inserted before
\r
211 * make a contiguous block of memory? */
\r
212 puc = ( uint8_t * ) pxBlockToInsert;
\r
214 if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )
\r
216 if( pxIterator->pxNextFreeBlock != pxEnd )
\r
218 /* Form one big block from the two blocks. */
\r
219 pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
\r
220 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
\r
224 pxBlockToInsert->pxNextFreeBlock = pxEnd;
\r
229 pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
\r
232 /* If the block being inserted plugged a gab, so was merged with the block
\r
233 * before and the block after, then it's pxNextFreeBlock pointer will have
\r
234 * already been set, and should not be set here as that would make it point
\r
236 if( pxIterator != pxBlockToInsert )
\r
238 pxIterator->pxNextFreeBlock = pxBlockToInsert;
\r
242 mtCOVERAGE_TEST_MARKER();
\r
245 /*-----------------------------------------------------------*/
\r
247 void * pvPortMalloc( size_t xWantedSize )
\r
249 BlockLink_t * pxBlock;
\r
250 BlockLink_t * pxPreviousBlock;
\r
251 BlockLink_t * pxNewBlockLink;
\r
252 void * pvReturn = NULL;
\r
254 /* If this is the first call to malloc then the heap will require
\r
255 * initialisation to setup the list of free blocks. */
\r
256 if( pxEnd == NULL )
\r
262 mtCOVERAGE_TEST_MARKER();
\r
265 /* Check the requested block size is not so large that the top bit is set.
\r
266 * The top bit of the block size member of the BlockLink_t structure is used
\r
267 * to determine who owns the block - the application or the kernel, so it
\r
269 if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
\r
271 /* The wanted size is increased so it can contain a BlockLink_t
\r
272 * structure in addition to the requested amount of bytes. */
\r
273 if( xWantedSize > 0 )
\r
275 xWantedSize += xHeapStructSize;
\r
277 /* Ensure that blocks are always aligned to the required number of
\r
279 if( ( xWantedSize & secureportBYTE_ALIGNMENT_MASK ) != 0x00 )
\r
281 /* Byte alignment required. */
\r
282 xWantedSize += ( secureportBYTE_ALIGNMENT - ( xWantedSize & secureportBYTE_ALIGNMENT_MASK ) );
\r
283 secureportASSERT( ( xWantedSize & secureportBYTE_ALIGNMENT_MASK ) == 0 );
\r
287 mtCOVERAGE_TEST_MARKER();
\r
292 mtCOVERAGE_TEST_MARKER();
\r
295 if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
\r
297 /* Traverse the list from the start (lowest address) block until
\r
298 * one of adequate size is found. */
\r
299 pxPreviousBlock = &xStart;
\r
300 pxBlock = xStart.pxNextFreeBlock;
\r
302 while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
\r
304 pxPreviousBlock = pxBlock;
\r
305 pxBlock = pxBlock->pxNextFreeBlock;
\r
308 /* If the end marker was reached then a block of adequate size was
\r
310 if( pxBlock != pxEnd )
\r
312 /* Return the memory space pointed to - jumping over the
\r
313 * BlockLink_t structure at its start. */
\r
314 pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );
\r
316 /* This block is being returned for use so must be taken out
\r
317 * of the list of free blocks. */
\r
318 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
\r
320 /* If the block is larger than required it can be split into
\r
322 if( ( pxBlock->xBlockSize - xWantedSize ) > secureheapMINIMUM_BLOCK_SIZE )
\r
324 /* This block is to be split into two. Create a new
\r
325 * block following the number of bytes requested. The void
\r
326 * cast is used to prevent byte alignment warnings from the
\r
328 pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
\r
329 secureportASSERT( ( ( ( size_t ) pxNewBlockLink ) & secureportBYTE_ALIGNMENT_MASK ) == 0 );
\r
331 /* Calculate the sizes of two blocks split from the single
\r
333 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
\r
334 pxBlock->xBlockSize = xWantedSize;
\r
336 /* Insert the new block into the list of free blocks. */
\r
337 prvInsertBlockIntoFreeList( pxNewBlockLink );
\r
341 mtCOVERAGE_TEST_MARKER();
\r
344 xFreeBytesRemaining -= pxBlock->xBlockSize;
\r
346 if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
\r
348 xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
\r
352 mtCOVERAGE_TEST_MARKER();
\r
355 /* The block is being returned - it is allocated and owned by
\r
356 * the application and has no "next" block. */
\r
357 pxBlock->xBlockSize |= xBlockAllocatedBit;
\r
358 pxBlock->pxNextFreeBlock = NULL;
\r
362 mtCOVERAGE_TEST_MARKER();
\r
367 mtCOVERAGE_TEST_MARKER();
\r
372 mtCOVERAGE_TEST_MARKER();
\r
375 traceMALLOC( pvReturn, xWantedSize );
\r
377 #if ( secureconfigUSE_MALLOC_FAILED_HOOK == 1 )
\r
379 if( pvReturn == NULL )
\r
381 extern void vApplicationMallocFailedHook( void );
\r
382 vApplicationMallocFailedHook();
\r
386 mtCOVERAGE_TEST_MARKER();
\r
389 #endif /* if ( secureconfigUSE_MALLOC_FAILED_HOOK == 1 ) */
\r
391 secureportASSERT( ( ( ( size_t ) pvReturn ) & ( size_t ) secureportBYTE_ALIGNMENT_MASK ) == 0 );
\r
394 /*-----------------------------------------------------------*/
\r
396 void vPortFree( void * pv )
\r
398 uint8_t * puc = ( uint8_t * ) pv;
\r
399 BlockLink_t * pxLink;
\r
403 /* The memory being freed will have an BlockLink_t structure immediately
\r
405 puc -= xHeapStructSize;
\r
407 /* This casting is to keep the compiler from issuing warnings. */
\r
408 pxLink = ( void * ) puc;
\r
410 /* Check the block is actually allocated. */
\r
411 secureportASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
\r
412 secureportASSERT( pxLink->pxNextFreeBlock == NULL );
\r
414 if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )
\r
416 if( pxLink->pxNextFreeBlock == NULL )
\r
418 /* The block is being returned to the heap - it is no longer
\r
420 pxLink->xBlockSize &= ~xBlockAllocatedBit;
\r
422 secureportDISABLE_NON_SECURE_INTERRUPTS();
\r
424 /* Add this block to the list of free blocks. */
\r
425 xFreeBytesRemaining += pxLink->xBlockSize;
\r
426 traceFREE( pv, pxLink->xBlockSize );
\r
427 prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
\r
429 secureportENABLE_NON_SECURE_INTERRUPTS();
\r
433 mtCOVERAGE_TEST_MARKER();
\r
438 mtCOVERAGE_TEST_MARKER();
\r
442 /*-----------------------------------------------------------*/
\r
444 size_t xPortGetFreeHeapSize( void )
\r
446 return xFreeBytesRemaining;
\r
448 /*-----------------------------------------------------------*/
\r
450 size_t xPortGetMinimumEverFreeHeapSize( void )
\r
452 return xMinimumEverFreeBytesRemaining;
\r
454 /*-----------------------------------------------------------*/
\r