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
31 * The simplest possible implementation of pvPortMalloc(). Note that this
\r
32 * implementation does NOT allow allocated memory to be freed again.
\r
34 * See heap_2.c, heap_3.c and heap_4.c for alternative implementations, and the
\r
35 * memory management pages of https://www.FreeRTOS.org for more information.
\r
39 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
40 * all the API functions to use the MPU wrappers. That should only be done when
\r
41 * task.h is included from an application file. */
\r
42 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
44 #include "FreeRTOS.h"
\r
47 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
49 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
\r
50 #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
\r
53 /* A few bytes might be lost to byte aligning the heap start address. */
\r
54 #define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
\r
56 /* Allocate the memory for the heap. */
\r
57 #if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
\r
59 /* The application writer has already defined the array used for the RTOS
\r
60 * heap - probably so it can be placed in a special segment or address. */
\r
61 extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
63 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
\r
64 #endif /* configAPPLICATION_ALLOCATED_HEAP */
\r
66 /* Index into the ucHeap array. */
\r
67 static size_t xNextFreeByte = ( size_t ) 0;
\r
69 /*-----------------------------------------------------------*/
\r
71 void * pvPortMalloc( size_t xWantedSize )
\r
73 void * pvReturn = NULL;
\r
74 static uint8_t * pucAlignedHeap = NULL;
\r
76 /* Ensure that blocks are always aligned. */
\r
77 #if ( portBYTE_ALIGNMENT != 1 )
\r
79 if( xWantedSize & portBYTE_ALIGNMENT_MASK )
\r
81 /* Byte alignment required. Check for overflow. */
\r
82 if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) ) > xWantedSize )
\r
84 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
\r
92 #endif /* if ( portBYTE_ALIGNMENT != 1 ) */
\r
96 if( pucAlignedHeap == NULL )
\r
98 /* Ensure the heap starts on a correctly aligned boundary. */
\r
99 pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) & ucHeap[ portBYTE_ALIGNMENT - 1 ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
\r
102 /* Check there is enough room left for the allocation and. */
\r
103 if( ( xWantedSize > 0 ) && /* valid size */
\r
104 ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) &&
\r
105 ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte ) ) /* Check for overflow. */
\r
107 /* Return the next free byte then increment the index past this
\r
109 pvReturn = pucAlignedHeap + xNextFreeByte;
\r
110 xNextFreeByte += xWantedSize;
\r
113 traceMALLOC( pvReturn, xWantedSize );
\r
115 ( void ) xTaskResumeAll();
\r
117 #if ( configUSE_MALLOC_FAILED_HOOK == 1 )
\r
119 if( pvReturn == NULL )
\r
121 vApplicationMallocFailedHook();
\r
128 /*-----------------------------------------------------------*/
\r
130 void vPortFree( void * pv )
\r
132 /* Memory cannot be freed using this scheme. See heap_2.c, heap_3.c and
\r
133 * heap_4.c for alternative implementations, and the memory management pages of
\r
134 * https://www.FreeRTOS.org for more information. */
\r
137 /* Force an assert as it is invalid to call this function. */
\r
138 configASSERT( pv == NULL );
\r
140 /*-----------------------------------------------------------*/
\r
142 void vPortInitialiseBlocks( void )
\r
144 /* Only required when static memory is not cleared. */
\r
145 xNextFreeByte = ( size_t ) 0;
\r
147 /*-----------------------------------------------------------*/
\r
149 size_t xPortGetFreeHeapSize( void )
\r
151 return( configADJUSTED_HEAP_SIZE - xNextFreeByte );
\r