2 * FreeRTOS SMP Kernel V202110.00
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
29 * The simplest possible implementation of pvPortMalloc(). Note that this
30 * implementation does NOT allow allocated memory to be freed again.
32 * See heap_2.c, heap_3.c and heap_4.c for alternative implementations, and the
33 * memory management pages of https://www.FreeRTOS.org for more information.
37 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
38 * all the API functions to use the MPU wrappers. That should only be done when
39 * task.h is included from an application file. */
40 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
45 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
47 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
48 #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
51 /* A few bytes might be lost to byte aligning the heap start address. */
52 #define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
54 /* Allocate the memory for the heap. */
55 #if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
57 /* The application writer has already defined the array used for the RTOS
58 * heap - probably so it can be placed in a special segment or address. */
59 extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
61 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
62 #endif /* configAPPLICATION_ALLOCATED_HEAP */
64 /* Index into the ucHeap array. */
65 static size_t xNextFreeByte = ( size_t ) 0;
67 /*-----------------------------------------------------------*/
69 void * pvPortMalloc( size_t xWantedSize )
71 void * pvReturn = NULL;
72 static uint8_t * pucAlignedHeap = NULL;
74 /* Ensure that blocks are always aligned. */
75 #if ( portBYTE_ALIGNMENT != 1 )
77 if( xWantedSize & portBYTE_ALIGNMENT_MASK )
79 /* Byte alignment required. Check for overflow. */
80 if ( (xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) )) > xWantedSize )
82 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
94 if( pucAlignedHeap == NULL )
96 /* Ensure the heap starts on a correctly aligned boundary. */
97 pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) & ucHeap[ portBYTE_ALIGNMENT ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
100 /* Check there is enough room left for the allocation and. */
101 if( ( xWantedSize > 0 ) && /* valid size */
102 ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) &&
103 ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte ) ) /* Check for overflow. */
105 /* Return the next free byte then increment the index past this
107 pvReturn = pucAlignedHeap + xNextFreeByte;
108 xNextFreeByte += xWantedSize;
111 traceMALLOC( pvReturn, xWantedSize );
113 ( void ) xTaskResumeAll();
115 #if ( configUSE_MALLOC_FAILED_HOOK == 1 )
117 if( pvReturn == NULL )
119 extern void vApplicationMallocFailedHook( void );
120 vApplicationMallocFailedHook();
127 /*-----------------------------------------------------------*/
129 void vPortFree( void * pv )
131 /* Memory cannot be freed using this scheme. See heap_2.c, heap_3.c and
132 * heap_4.c for alternative implementations, and the memory management pages of
133 * https://www.FreeRTOS.org for more information. */
136 /* Force an assert as it is invalid to call this function. */
137 configASSERT( pv == NULL );
139 /*-----------------------------------------------------------*/
141 void vPortInitialiseBlocks( void )
143 /* Only required when static memory is not cleared. */
144 xNextFreeByte = ( size_t ) 0;
146 /*-----------------------------------------------------------*/
148 size_t xPortGetFreeHeapSize( void )
150 return( configADJUSTED_HEAP_SIZE - xNextFreeByte );