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