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