1 /*
2 * FreeRTOS Kernel V10.3.1
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * http://www.FreeRTOS.org
24 * http://aws.amazon.com/freertos
25 *
26 * 1 tab == 4 spaces!
27 */
28
29 /*
30 * The simplest possible implementation of pvPortMalloc(). Note that this
31 * implementation does NOT allow allocated memory to be freed again.
32 *
33 * See heap_2.c, heap_3.c and heap_4.c for alternative implementations, and the
34 * memory management pages of http://www.FreeRTOS.org for more information.
35 */
36 #include <stdlib.h>
37
38 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
39 all the API functions to use the MPU wrappers. That should only be done when
40 task.h is included from an application file. */
41 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
42
43 #include "FreeRTOS.h"
44 #include "task.h"
45
46 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
47
48 #if (configSUPPORT_DYNAMIC_ALLOCATION == 0)
49 # error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
50 #endif
51
52 /* A few bytes might be lost to byte aligning the heap start address. */
53 #define configADJUSTED_HEAP_SIZE (configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT)
54
55 /* Allocate the memory for the heap. */
56 #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];
60 #else
61 static uint8_t ucHeap[configTOTAL_HEAP_SIZE];
62 #endif /* configAPPLICATION_ALLOCATED_HEAP */
63
64 /* Index into the ucHeap array. */
65 static size_t xNextFreeByte = (size_t)0;
66
67 /*-----------------------------------------------------------*/
68
pvPortMalloc(size_t xWantedSize)69 void *pvPortMalloc(size_t xWantedSize)
70 {
71 void *pvReturn = NULL;
72 static uint8_t *pucAlignedHeap = NULL;
73
74 /* Ensure that blocks are always aligned to the required number of bytes. */
75 #if (portBYTE_ALIGNMENT != 1)
76 {
77 if (xWantedSize & portBYTE_ALIGNMENT_MASK) {
78 /* Byte alignment required. */
79 xWantedSize +=
80 (portBYTE_ALIGNMENT - (xWantedSize & portBYTE_ALIGNMENT_MASK));
81 }
82 }
83 #endif
84
85 vTaskSuspendAll();
86 {
87 if (pucAlignedHeap == NULL) {
88 /* Ensure the heap starts on a correctly aligned boundary. */
89 pucAlignedHeap =
90 (uint8_t
91 *)(((portPOINTER_SIZE_TYPE)&ucHeap[portBYTE_ALIGNMENT]) & (~((portPOINTER_SIZE_TYPE)portBYTE_ALIGNMENT_MASK)));
92 }
93
94 /* Check there is enough room left for the allocation. */
95 if (((xNextFreeByte + xWantedSize) < configADJUSTED_HEAP_SIZE) &&
96 ((xNextFreeByte + xWantedSize) >
97 xNextFreeByte)) /* Check for overflow. */
98 {
99 /* Return the next free byte then increment the index past this
100 block. */
101 pvReturn = pucAlignedHeap + xNextFreeByte;
102 xNextFreeByte += xWantedSize;
103 }
104
105 traceMALLOC(pvReturn, xWantedSize);
106 }
107 (void)xTaskResumeAll();
108
109 #if (configUSE_MALLOC_FAILED_HOOK == 1)
110 {
111 if (pvReturn == NULL) {
112 extern void vApplicationMallocFailedHook(void);
113 vApplicationMallocFailedHook();
114 }
115 }
116 #endif
117
118 return pvReturn;
119 }
120 /*-----------------------------------------------------------*/
121
vPortFree(void * pv)122 void vPortFree(void *pv)
123 {
124 /* Memory cannot be freed using this scheme. See heap_2.c, heap_3.c and
125 heap_4.c for alternative implementations, and the memory management pages of
126 http://www.FreeRTOS.org for more information. */
127 (void)pv;
128
129 /* Force an assert as it is invalid to call this function. */
130 configASSERT(pv == NULL);
131 }
132 /*-----------------------------------------------------------*/
133
vPortInitialiseBlocks(void)134 void vPortInitialiseBlocks(void)
135 {
136 /* Only required when static memory is not cleared. */
137 xNextFreeByte = (size_t)0;
138 }
139 /*-----------------------------------------------------------*/
140
xPortGetFreeHeapSize(void)141 size_t xPortGetFreeHeapSize(void)
142 {
143 return (configADJUSTED_HEAP_SIZE - xNextFreeByte);
144 }
145