Skip to content

Commit e038703

Browse files
committed
Refactor pvPortRealloc() to improve readability.
1 parent 611e0ff commit e038703

File tree

1 file changed

+36
-51
lines changed

1 file changed

+36
-51
lines changed

src/FreeRTOS/heap_4_infinitime.c

+36-51
Original file line numberDiff line numberDiff line change
@@ -448,70 +448,55 @@ static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )
448448

449449
/*-----------------------------------------------------------*/
450450

451-
void *pvPortRealloc( void *pv, size_t xWantedSize )
452-
{
451+
void* pvPortRealloc(void* pv, size_t xWantedSize) {
453452
size_t move_size;
454453
size_t block_size;
455454
BlockLink_t* pxLink;
456455
void* pvReturn = NULL;
457456
uint8_t* puc = (uint8_t*) pv;
458457

459-
if (xWantedSize > 0)
460-
{
461-
if (pv != NULL)
462-
{
463-
// The memory being freed will have an BlockLink_t structure immediately before it.
464-
puc -= xHeapStructSize;
458+
if (xWantedSize == 0) {
459+
// Zero bytes requested, do nothing (according to libc, this behavior implementation defined)
460+
return NULL;
461+
}
465462

466-
// This casting is to keep the compiler from issuing warnings.
467-
pxLink = (void*) puc;
463+
if (pv == NULL) {
464+
// pv points to NULL. Allocate a new buffer.
465+
return pvPortMalloc(xWantedSize);
466+
}
468467

469-
// Check allocate block
470-
if ((pxLink->xBlockSize & xBlockAllocatedBit) != 0)
471-
{
472-
// The block is being returned to the heap - it is no longer allocated.
473-
block_size = (pxLink->xBlockSize & ~xBlockAllocatedBit) - xHeapStructSize;
468+
// The memory being freed will have an BlockLink_t structure immediately before it.
469+
puc -= xHeapStructSize;
474470

475-
// Allocate a new buffer
476-
pvReturn = pvPortMalloc(xWantedSize);
471+
// This casting is to keep the compiler from issuing warnings.
472+
pxLink = (void*) puc;
477473

478-
// Check creation and determine the data size to be copied to the new buffer
479-
if (pvReturn != NULL)
480-
{
481-
if (block_size < xWantedSize)
482-
{
483-
move_size = block_size;
484-
}
485-
else
486-
{
487-
move_size = xWantedSize;
488-
}
474+
// Check allocate block
475+
if ((pxLink->xBlockSize & xBlockAllocatedBit) != 0) {
476+
// The block is being returned to the heap - it is no longer allocated.
477+
block_size = (pxLink->xBlockSize & ~xBlockAllocatedBit) - xHeapStructSize;
489478

490-
// Copy the data from the old buffer to the new one
491-
memcpy(pvReturn, pv, move_size);
479+
// Allocate a new buffer
480+
pvReturn = pvPortMalloc(xWantedSize);
492481

493-
// Free the old buffer
494-
vPortFree(pv);
495-
}
482+
// Check creation and determine the data size to be copied to the new buffer
483+
if (pvReturn != NULL) {
484+
if (block_size < xWantedSize) {
485+
move_size = block_size;
486+
} else {
487+
move_size = xWantedSize;
496488
}
497-
else
498-
{
499-
// pv does not point to a valid memory buffer. Allocate a new one
500-
pvReturn = pvPortMalloc(xWantedSize);
501-
}
502-
}
503-
else
504-
{
505-
// pv points to NULL. Allocate a new buffer.
506-
pvReturn = pvPortMalloc(xWantedSize);
489+
490+
// Copy the data from the old buffer to the new one
491+
memcpy(pvReturn, pv, move_size);
492+
493+
// Free the old buffer
494+
vPortFree(pv);
507495
}
508-
}
509-
else
510-
{
511-
// Zero bytes requested, do nothing (according to libc, this behavior implementation defined)
512-
pvReturn = NULL;
496+
} else {
497+
// pv does not point to a valid memory buffer. Allocate a new one
498+
pvReturn = pvPortMalloc(xWantedSize);
513499
}
514500

515-
// Exit with memory block
516-
return pvReturn;
517-
}
501+
return pvReturn;
502+
}

0 commit comments

Comments
 (0)