]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MemMang/heap_1.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Source / portable / MemMang / heap_1.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\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 http://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 /* Allocate the memory for the heap. */\r
58 #if( configAPPLICATION_ALLOCATED_HEAP == 1 )\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 to the required number of bytes. */\r
77         #if( portBYTE_ALIGNMENT != 1 )\r
78         {\r
79                 if( xWantedSize & portBYTE_ALIGNMENT_MASK )\r
80                 {\r
81                         /* Byte alignment required. */\r
82                         xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );\r
83                 }\r
84         }\r
85         #endif\r
86 \r
87         vTaskSuspendAll();\r
88         {\r
89                 if( pucAlignedHeap == NULL )\r
90                 {\r
91                         /* Ensure the heap starts on a correctly aligned boundary. */\r
92                         pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );\r
93                 }\r
94 \r
95                 /* Check there is enough room left for the allocation. */\r
96                 if( ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) &&\r
97                         ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte )     )/* Check for overflow. */\r
98                 {\r
99                         /* Return the next free byte then increment the index past this\r
100                         block. */\r
101                         pvReturn = pucAlignedHeap + xNextFreeByte;\r
102                         xNextFreeByte += xWantedSize;\r
103                 }\r
104 \r
105                 traceMALLOC( pvReturn, xWantedSize );\r
106         }\r
107         ( void ) xTaskResumeAll();\r
108 \r
109         #if( configUSE_MALLOC_FAILED_HOOK == 1 )\r
110         {\r
111                 if( pvReturn == NULL )\r
112                 {\r
113                         extern void vApplicationMallocFailedHook( void );\r
114                         vApplicationMallocFailedHook();\r
115                 }\r
116         }\r
117         #endif\r
118 \r
119         return pvReturn;\r
120 }\r
121 /*-----------------------------------------------------------*/\r
122 \r
123 void vPortFree( void *pv )\r
124 {\r
125         /* Memory cannot be freed using this scheme.  See heap_2.c, heap_3.c and\r
126         heap_4.c for alternative implementations, and the memory management pages of\r
127         http://www.FreeRTOS.org for more information. */\r
128         ( void ) pv;\r
129 \r
130         /* Force an assert as it is invalid to call this function. */\r
131         configASSERT( pv == NULL );\r
132 }\r
133 /*-----------------------------------------------------------*/\r
134 \r
135 void vPortInitialiseBlocks( void )\r
136 {\r
137         /* Only required when static memory is not cleared. */\r
138         xNextFreeByte = ( size_t ) 0;\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 size_t xPortGetFreeHeapSize( void )\r
143 {\r
144         return ( configADJUSTED_HEAP_SIZE - xNextFreeByte );\r
145 }\r
146 \r
147 \r
148 \r