50
50
#error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
51
51
#endif
52
52
53
- /* A few bytes might be lost to byte aligning the heap start address. */
54
- #define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
55
-
56
53
/* Max value that fits in a size_t type. */
57
54
#define heapSIZE_MAX ( ~( ( size_t ) 0 ) )
58
55
71
68
static uint8_t ucHeap [ configTOTAL_HEAP_SIZE ];
72
69
#endif /* configAPPLICATION_ALLOCATED_HEAP */
73
70
71
+ /* A few bytes might be lost to byte aligning the heap start address. */
72
+ static size_t xAdjustedHeapSize = ( size_t ) ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT );
73
+
74
+ /* UcHeap array starts on a correctly aligned boundary. */
75
+ static uint8_t * pucAlignedHeap = NULL ;
76
+
74
77
/* Index into the ucHeap array. */
75
78
static size_t xNextFreeByte = ( size_t ) 0U ;
76
79
@@ -79,7 +82,6 @@ static size_t xNextFreeByte = ( size_t ) 0U;
79
82
void * pvPortMalloc ( size_t xWantedSize )
80
83
{
81
84
void * pvReturn = NULL ;
82
- static uint8_t * pucAlignedHeap = NULL ;
83
85
84
86
/* Ensure that blocks are always aligned. */
85
87
#if ( portBYTE_ALIGNMENT != 1 )
@@ -110,12 +112,13 @@ void * pvPortMalloc( size_t xWantedSize )
110
112
/* Ensure the heap starts on a correctly aligned boundary. */
111
113
pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) & ( ucHeap [ portBYTE_ALIGNMENT - 1 ] ) ) &
112
114
( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
115
+ xAdjustedHeapSize = configTOTAL_HEAP_SIZE - (( portPOINTER_SIZE_TYPE ) & ucHeap [0 ] - ( portPOINTER_SIZE_TYPE ) & pucAlignedHeap );
113
116
}
114
117
115
118
/* Check there is enough room left for the allocation. */
116
119
if ( ( xWantedSize > 0 ) &&
117
120
( heapADD_WILL_OVERFLOW ( xNextFreeByte , xWantedSize ) == 0 ) &&
118
- ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) )
121
+ ( ( xNextFreeByte + xWantedSize ) < xAdjustedHeapSize ) )
119
122
{
120
123
/* Return the next free byte then increment the index past this
121
124
* block. */
@@ -161,7 +164,7 @@ void vPortInitialiseBlocks( void )
161
164
162
165
size_t xPortGetFreeHeapSize ( void )
163
166
{
164
- return ( configADJUSTED_HEAP_SIZE - xNextFreeByte );
167
+ return ( xAdjustedHeapSize - xNextFreeByte );
165
168
}
166
169
167
170
/*-----------------------------------------------------------*/
0 commit comments