Skip to content

Commit d6bccb1

Browse files
authored
Fix heap address calculation issue (#781)
Signed-off-by: Gaurav Aggarwal <[email protected]>
1 parent 231278e commit d6bccb1

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

portable/MemMang/heap_4.c

+13-13
Original file line numberDiff line numberDiff line change
@@ -444,17 +444,17 @@ void * pvPortCalloc( size_t xNum,
444444
static void prvHeapInit( void ) /* PRIVILEGED_FUNCTION */
445445
{
446446
BlockLink_t * pxFirstFreeBlock;
447-
portPOINTER_SIZE_TYPE uxAddress;
447+
portPOINTER_SIZE_TYPE uxStartAddress, uxEndAddress;
448448
size_t xTotalHeapSize = configTOTAL_HEAP_SIZE;
449449

450450
/* Ensure the heap starts on a correctly aligned boundary. */
451-
uxAddress = ( portPOINTER_SIZE_TYPE ) ucHeap;
451+
uxStartAddress = ( portPOINTER_SIZE_TYPE ) ucHeap;
452452

453-
if( ( uxAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
453+
if( ( uxStartAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
454454
{
455-
uxAddress += ( portBYTE_ALIGNMENT - 1 );
456-
uxAddress &= ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK );
457-
xTotalHeapSize -= ( size_t ) ( uxAddress - ( portPOINTER_SIZE_TYPE ) ucHeap );
455+
uxStartAddress += ( portBYTE_ALIGNMENT - 1 );
456+
uxStartAddress &= ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK );
457+
xTotalHeapSize -= ( size_t ) ( uxStartAddress - ( portPOINTER_SIZE_TYPE ) ucHeap );
458458
}
459459

460460
#if ( configENABLE_HEAP_PROTECTOR == 1 )
@@ -465,22 +465,22 @@ static void prvHeapInit( void ) /* PRIVILEGED_FUNCTION */
465465

466466
/* xStart is used to hold a pointer to the first item in the list of free
467467
* blocks. The void cast is used to prevent compiler warnings. */
468-
xStart.pxNextFreeBlock = ( void * ) heapPROTECT_BLOCK_POINTER( uxAddress );
468+
xStart.pxNextFreeBlock = ( void * ) heapPROTECT_BLOCK_POINTER( uxStartAddress );
469469
xStart.xBlockSize = ( size_t ) 0;
470470

471471
/* pxEnd is used to mark the end of the list of free blocks and is inserted
472472
* at the end of the heap space. */
473-
uxAddress = ( portPOINTER_SIZE_TYPE ) ( uxAddress + xTotalHeapSize );
474-
uxAddress -= ( portPOINTER_SIZE_TYPE ) xHeapStructSize;
475-
uxAddress &= ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK );
476-
pxEnd = ( BlockLink_t * ) uxAddress;
473+
uxEndAddress = uxStartAddress + ( portPOINTER_SIZE_TYPE ) xTotalHeapSize;
474+
uxEndAddress -= ( portPOINTER_SIZE_TYPE ) xHeapStructSize;
475+
uxEndAddress &= ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK );
476+
pxEnd = ( BlockLink_t * ) uxEndAddress;
477477
pxEnd->xBlockSize = 0;
478478
pxEnd->pxNextFreeBlock = heapPROTECT_BLOCK_POINTER( NULL );
479479

480480
/* To start with there is a single free block that is sized to take up the
481481
* entire heap space, minus the space taken by pxEnd. */
482-
pxFirstFreeBlock = ( BlockLink_t * ) uxAddress;
483-
pxFirstFreeBlock->xBlockSize = ( size_t ) ( uxAddress - ( portPOINTER_SIZE_TYPE ) pxFirstFreeBlock );
482+
pxFirstFreeBlock = ( BlockLink_t * ) uxStartAddress;
483+
pxFirstFreeBlock->xBlockSize = ( size_t ) ( uxEndAddress - ( portPOINTER_SIZE_TYPE ) pxFirstFreeBlock );
484484
pxFirstFreeBlock->pxNextFreeBlock = heapPROTECT_BLOCK_POINTER( pxEnd );
485485

486486
/* Only one block exists - and it covers the entire usable heap space. */

0 commit comments

Comments
 (0)