Skip to content

Commit f928e58

Browse files
committed
Modify heap 1.c so AdjustedHeapSize is computed more accurately
1 parent 65e6297 commit f928e58

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

portable/MemMang/heap_1.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@
5050
#error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
5151
#endif
5252

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-
5653
/* Max value that fits in a size_t type. */
5754
#define heapSIZE_MAX ( ~( ( size_t ) 0 ) )
5855

@@ -71,6 +68,12 @@
7168
static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
7269
#endif /* configAPPLICATION_ALLOCATED_HEAP */
7370

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+
7477
/* Index into the ucHeap array. */
7578
static size_t xNextFreeByte = ( size_t ) 0U;
7679

@@ -79,7 +82,6 @@ static size_t xNextFreeByte = ( size_t ) 0U;
7982
void * pvPortMalloc( size_t xWantedSize )
8083
{
8184
void * pvReturn = NULL;
82-
static uint8_t * pucAlignedHeap = NULL;
8385

8486
/* Ensure that blocks are always aligned. */
8587
#if ( portBYTE_ALIGNMENT != 1 )
@@ -110,12 +112,13 @@ void * pvPortMalloc( size_t xWantedSize )
110112
/* Ensure the heap starts on a correctly aligned boundary. */
111113
pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &( ucHeap[ portBYTE_ALIGNMENT - 1 ] ) ) &
112114
( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
115+
xAdjustedHeapSize = configTOTAL_HEAP_SIZE - (( portPOINTER_SIZE_TYPE ) &ucHeap[0] - ( portPOINTER_SIZE_TYPE ) &pucAlignedHeap);
113116
}
114117

115118
/* Check there is enough room left for the allocation. */
116119
if( ( xWantedSize > 0 ) &&
117120
( heapADD_WILL_OVERFLOW( xNextFreeByte, xWantedSize ) == 0 ) &&
118-
( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) )
121+
( ( xNextFreeByte + xWantedSize ) < xAdjustedHeapSize ) )
119122
{
120123
/* Return the next free byte then increment the index past this
121124
* block. */
@@ -161,7 +164,7 @@ void vPortInitialiseBlocks( void )
161164

162165
size_t xPortGetFreeHeapSize( void )
163166
{
164-
return( configADJUSTED_HEAP_SIZE - xNextFreeByte );
167+
return( xAdjustedHeapSize - xNextFreeByte );
165168
}
166169

167170
/*-----------------------------------------------------------*/

0 commit comments

Comments
 (0)