1 /*
2 * FreeRTOS Kernel V10.6.2
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 /* Allocate the memory for the heap. */
57 #if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
58
59 /* The application writer has already defined the array used for the RTOS
60 * heap - probably so it can be placed in a special segment or address. */
61 extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
62 #else
63 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
64 #endif /* configAPPLICATION_ALLOCATED_HEAP */
65
66 /* Index into the ucHeap array. */
67 static size_t xNextFreeByte = ( size_t ) 0;
68
69 /*-----------------------------------------------------------*/
70
pvPortMalloc(size_t xWantedSize)71 void * pvPortMalloc( size_t xWantedSize )
72 {
73 void * pvReturn = NULL;
74 static uint8_t * pucAlignedHeap = NULL;
75
76 /* Ensure that blocks are always aligned. */
77 #if ( portBYTE_ALIGNMENT != 1 )
78 {
79 if( xWantedSize & portBYTE_ALIGNMENT_MASK )
80 {
81 /* Byte alignment required. Check for overflow. */
82 if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) ) > xWantedSize )
83 {
84 xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
85 }
86 else
87 {
88 xWantedSize = 0;
89 }
90 }
91 }
92 #endif /* if ( portBYTE_ALIGNMENT != 1 ) */
93
94 vTaskSuspendAll();
95 {
96 if( pucAlignedHeap == NULL )
97 {
98 /* Ensure the heap starts on a correctly aligned boundary. */
99 pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) & ucHeap[ portBYTE_ALIGNMENT - 1 ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
100 }
101
102 /* Check there is enough room left for the allocation and. */
103 if( ( xWantedSize > 0 ) && /* valid size */
104 ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) &&
105 ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte ) ) /* Check for overflow. */
106 {
107 /* Return the next free byte then increment the index past this
108 * block. */
109 pvReturn = pucAlignedHeap + xNextFreeByte;
110 xNextFreeByte += xWantedSize;
111 }
112
113 traceMALLOC( pvReturn, xWantedSize );
114 }
115 ( void ) xTaskResumeAll();
116
117 #if ( configUSE_MALLOC_FAILED_HOOK == 1 )
118 {
119 if( pvReturn == NULL )
120 {
121 vApplicationMallocFailedHook();
122 }
123 }
124 #endif
125
126 return pvReturn;
127 }
128 /*-----------------------------------------------------------*/
129
vPortFree(void * pv)130 void vPortFree( void * pv )
131 {
132 /* Memory cannot be freed using this scheme. See heap_2.c, heap_3.c and
133 * heap_4.c for alternative implementations, and the memory management pages of
134 * https://www.FreeRTOS.org for more information. */
135 ( void ) pv;
136
137 /* Force an assert as it is invalid to call this function. */
138 configASSERT( pv == NULL );
139 }
140 /*-----------------------------------------------------------*/
141
vPortInitialiseBlocks(void)142 void vPortInitialiseBlocks( void )
143 {
144 /* Only required when static memory is not cleared. */
145 xNextFreeByte = ( size_t ) 0;
146 }
147 /*-----------------------------------------------------------*/
148
xPortGetFreeHeapSize(void)149 size_t xPortGetFreeHeapSize( void )
150 {
151 return( configADJUSTED_HEAP_SIZE - xNextFreeByte );
152 }
153