2 * FreeRTOS Kernel V11.2.0
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
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:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
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.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
31 * The simplest possible implementation of pvPortMalloc(). Note that this
32 * implementation does NOT allow allocated memory to be freed again.
34 * See heap_2.c, heap_3.c and heap_4.c for alternative implementations, and the
35 * memory management pages of https://www.FreeRTOS.org for more information.
39 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
40 * all the API functions to use the MPU wrappers. That should only be done when
41 * task.h is included from an application file. */
42 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
47 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
49 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
50 #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
53 /* A few bytes might be lost to byte aligning the heap start address. */
54 #define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
56 /* Max value that fits in a size_t type. */
57 #define heapSIZE_MAX ( ~( ( size_t ) 0 ) )
59 /* Check if adding a and b will result in overflow. */
60 #define heapADD_WILL_OVERFLOW( a, b ) ( ( a ) > ( heapSIZE_MAX - ( b ) ) )
62 /*-----------------------------------------------------------*/
64 /* Allocate the memory for the heap. */
65 #if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
67 /* The application writer has already defined the array used for the RTOS
68 * heap - probably so it can be placed in a special segment or address. */
69 extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
71 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
72 #endif /* configAPPLICATION_ALLOCATED_HEAP */
74 /* Index into the ucHeap array. */
75 static size_t xNextFreeByte = ( size_t ) 0U;
77 /*-----------------------------------------------------------*/
79 void * pvPortMalloc( size_t xWantedSize )
81 void * pvReturn = NULL;
82 static uint8_t * pucAlignedHeap = NULL;
84 /* Ensure that blocks are always aligned. */
85 #if ( portBYTE_ALIGNMENT != 1 )
87 size_t xAdditionalRequiredSize;
89 if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
91 /* Byte alignment required. */
92 xAdditionalRequiredSize = portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK );
94 if( heapADD_WILL_OVERFLOW( xWantedSize, xAdditionalRequiredSize ) == 0 )
96 xWantedSize += xAdditionalRequiredSize;
104 #endif /* if ( portBYTE_ALIGNMENT != 1 ) */
108 if( pucAlignedHeap == NULL )
110 /* Ensure the heap starts on a correctly aligned boundary. */
111 pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &( ucHeap[ portBYTE_ALIGNMENT - 1 ] ) ) &
112 ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
115 /* Check there is enough room left for the allocation. */
116 if( ( xWantedSize > 0 ) &&
117 ( heapADD_WILL_OVERFLOW( xNextFreeByte, xWantedSize ) == 0 ) &&
118 ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) )
120 /* Return the next free byte then increment the index past this
122 pvReturn = pucAlignedHeap + xNextFreeByte;
123 xNextFreeByte += xWantedSize;
126 traceMALLOC( pvReturn, xWantedSize );
128 ( void ) xTaskResumeAll();
130 #if ( configUSE_MALLOC_FAILED_HOOK == 1 )
132 if( pvReturn == NULL )
134 vApplicationMallocFailedHook();
141 /*-----------------------------------------------------------*/
143 void vPortFree( void * pv )
145 /* Memory cannot be freed using this scheme. See heap_2.c, heap_3.c and
146 * heap_4.c for alternative implementations, and the memory management pages of
147 * https://www.FreeRTOS.org for more information. */
150 /* Force an assert as it is invalid to call this function. */
151 configASSERT( pv == NULL );
153 /*-----------------------------------------------------------*/
155 void vPortInitialiseBlocks( void )
157 /* Only required when static memory is not cleared. */
158 xNextFreeByte = ( size_t ) 0;
160 /*-----------------------------------------------------------*/
162 size_t xPortGetFreeHeapSize( void )
164 return( configADJUSTED_HEAP_SIZE - xNextFreeByte );
167 /*-----------------------------------------------------------*/
170 * Reset the state in this file. This state is normally initialized at start up.
171 * This function must be called by the application before restarting the
174 void vPortHeapResetState( void )
176 xNextFreeByte = ( size_t ) 0U;
178 /*-----------------------------------------------------------*/