]> begriffs open source - freertos/blob - portable/MemMang/heap_1.c
[AUTO][RELEASE]: Bump file header version to "10.4.4"
[freertos] / portable / MemMang / heap_1.c
1 /*\r
2  * FreeRTOS Kernel V10.4.4\r
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * SPDX-License-Identifier: MIT
6  *
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
13  *\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
16  *\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
23  *\r
24  * https://www.FreeRTOS.org\r
25  * https://github.com/FreeRTOS\r
26  *\r
27  */\r
28 \r
29 \r
30 /*\r
31  * The simplest possible implementation of pvPortMalloc().  Note that this\r
32  * implementation does NOT allow allocated memory to be freed again.\r
33  *\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
36  */\r
37 #include <stdlib.h>\r
38 \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
43 \r
44 #include "FreeRTOS.h"\r
45 #include "task.h"\r
46 \r
47 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
48 \r
49 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 0 )\r
50     #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0\r
51 #endif\r
52 \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
55 \r
56 /* Allocate the memory for the heap. */\r
57 #if ( configAPPLICATION_ALLOCATED_HEAP == 1 )\r
58 \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
62 #else\r
63     static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
64 #endif /* configAPPLICATION_ALLOCATED_HEAP */\r
65 \r
66 /* Index into the ucHeap array. */\r
67 static size_t xNextFreeByte = ( size_t ) 0;\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 void * pvPortMalloc( size_t xWantedSize )\r
72 {\r
73     void * pvReturn = NULL;\r
74     static uint8_t * pucAlignedHeap = NULL;\r
75 \r
76     /* Ensure that blocks are always aligned. */\r
77     #if ( portBYTE_ALIGNMENT != 1 )\r
78         {\r
79             if( xWantedSize & portBYTE_ALIGNMENT_MASK )\r
80             {\r
81                 /* Byte alignment required. Check for overflow. */\r
82                 if ( (xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) )) > xWantedSize )\r
83                 {\r
84                     xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );\r
85                 }\r
86                 else\r
87                 {\r
88                     xWantedSize = 0;\r
89                 }\r
90             }\r
91         }\r
92     #endif\r
93 \r
94     vTaskSuspendAll();\r
95     {\r
96         if( pucAlignedHeap == NULL )\r
97         {\r
98             /* Ensure the heap starts on a correctly aligned boundary. */\r
99             pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) & ucHeap[ portBYTE_ALIGNMENT ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );\r
100         }\r
101 \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
106         {\r
107             /* Return the next free byte then increment the index past this\r
108              * block. */\r
109             pvReturn = pucAlignedHeap + xNextFreeByte;\r
110             xNextFreeByte += xWantedSize;\r
111         }\r
112 \r
113         traceMALLOC( pvReturn, xWantedSize );\r
114     }\r
115     ( void ) xTaskResumeAll();\r
116 \r
117     #if ( configUSE_MALLOC_FAILED_HOOK == 1 )\r
118         {\r
119             if( pvReturn == NULL )\r
120             {\r
121                 extern void vApplicationMallocFailedHook( void );\r
122                 vApplicationMallocFailedHook();\r
123             }\r
124         }\r
125     #endif\r
126 \r
127     return pvReturn;\r
128 }\r
129 /*-----------------------------------------------------------*/\r
130 \r
131 void vPortFree( void * pv )\r
132 {\r
133     /* Memory cannot be freed using this scheme.  See heap_2.c, heap_3.c and\r
134      * heap_4.c for alternative implementations, and the memory management pages of\r
135      * https://www.FreeRTOS.org for more information. */\r
136     ( void ) pv;\r
137 \r
138     /* Force an assert as it is invalid to call this function. */\r
139     configASSERT( pv == NULL );\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 void vPortInitialiseBlocks( void )\r
144 {\r
145     /* Only required when static memory is not cleared. */\r
146     xNextFreeByte = ( size_t ) 0;\r
147 }\r
148 /*-----------------------------------------------------------*/\r
149 \r
150 size_t xPortGetFreeHeapSize( void )\r
151 {\r
152     return( configADJUSTED_HEAP_SIZE - xNextFreeByte );\r
153 }\r