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