1 /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
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 /*
31  * The simplest possible implementation of pvPortMalloc().  Note that this
32  * implementation does NOT allow allocated memory to be freed again.
33  *
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.
36  */
37 #include <stdlib.h>
38 
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
43 
44 #include "FreeRTOS.h"
45 #include "task.h"
46 
47 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
48 
49 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
50     #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
51 #endif
52 
53 /* A few bytes might be lost to byte aligning the heap start address. */
54 #define configADJUSTED_HEAP_SIZE        ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
55 
56 /* Max value that fits in a size_t type. */
57 #define heapSIZE_MAX                    ( ~( ( size_t ) 0 ) )
58 
59 /* Check if adding a and b will result in overflow. */
60 #define heapADD_WILL_OVERFLOW( a, b )   ( ( a ) > ( heapSIZE_MAX - ( b ) ) )
61 
62 /*-----------------------------------------------------------*/
63 
64 /* Allocate the memory for the heap. */
65 #if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
66 
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 ];
70 #else
71     static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
72 #endif /* configAPPLICATION_ALLOCATED_HEAP */
73 
74 /* Index into the ucHeap array. */
75 static size_t xNextFreeByte = ( size_t ) 0U;
76 
77 /*-----------------------------------------------------------*/
78 
pvPortMalloc(size_t xWantedSize)79 void * pvPortMalloc( size_t xWantedSize )
80 {
81     void * pvReturn = NULL;
82     static uint8_t * pucAlignedHeap = NULL;
83 
84     /* Ensure that blocks are always aligned. */
85     #if ( portBYTE_ALIGNMENT != 1 )
86     {
87         size_t xAdditionalRequiredSize;
88 
89         if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
90         {
91             /* Byte alignment required. */
92             xAdditionalRequiredSize = portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK );
93 
94             if( heapADD_WILL_OVERFLOW( xWantedSize, xAdditionalRequiredSize ) == 0 )
95             {
96                 xWantedSize += xAdditionalRequiredSize;
97             }
98             else
99             {
100                 xWantedSize = 0;
101             }
102         }
103     }
104     #endif /* if ( portBYTE_ALIGNMENT != 1 ) */
105 
106     vTaskSuspendAll();
107     {
108         if( pucAlignedHeap == NULL )
109         {
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 ) ) );
113         }
114 
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 ) )
119         {
120             /* Return the next free byte then increment the index past this
121              * block. */
122             pvReturn = pucAlignedHeap + xNextFreeByte;
123             xNextFreeByte += xWantedSize;
124         }
125 
126         traceMALLOC( pvReturn, xWantedSize );
127     }
128     ( void ) xTaskResumeAll();
129 
130     #if ( configUSE_MALLOC_FAILED_HOOK == 1 )
131     {
132         if( pvReturn == NULL )
133         {
134             vApplicationMallocFailedHook();
135         }
136     }
137     #endif
138 
139     return pvReturn;
140 }
141 /*-----------------------------------------------------------*/
142 
vPortFree(void * pv)143 void vPortFree( void * pv )
144 {
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. */
148     ( void ) pv;
149 
150     /* Force an assert as it is invalid to call this function. */
151     configASSERT( pv == NULL );
152 }
153 /*-----------------------------------------------------------*/
154 
vPortInitialiseBlocks(void)155 void vPortInitialiseBlocks( void )
156 {
157     /* Only required when static memory is not cleared. */
158     xNextFreeByte = ( size_t ) 0;
159 }
160 /*-----------------------------------------------------------*/
161 
xPortGetFreeHeapSize(void)162 size_t xPortGetFreeHeapSize( void )
163 {
164     return( configADJUSTED_HEAP_SIZE - xNextFreeByte );
165 }
166 
167 /*-----------------------------------------------------------*/
168 
169 /*
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
172  * scheduler.
173  */
vPortHeapResetState(void)174 void vPortHeapResetState( void )
175 {
176     xNextFreeByte = ( size_t ) 0U;
177 }
178 /*-----------------------------------------------------------*/
179