Refactor pvPortRealloc() to improve readability.

This commit is contained in:
Jean-François Milants 2023-04-08 15:30:14 +02:00 committed by JF
parent 611e0ff768
commit e038703efe

View File

@ -448,18 +448,23 @@ static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )
/*-----------------------------------------------------------*/
void *pvPortRealloc( void *pv, size_t xWantedSize )
{
void* pvPortRealloc(void* pv, size_t xWantedSize) {
size_t move_size;
size_t block_size;
BlockLink_t* pxLink;
void* pvReturn = NULL;
uint8_t* puc = (uint8_t*) pv;
if (xWantedSize > 0)
{
if (pv != NULL)
{
if (xWantedSize == 0) {
// Zero bytes requested, do nothing (according to libc, this behavior implementation defined)
return NULL;
}
if (pv == NULL) {
// pv points to NULL. Allocate a new buffer.
return pvPortMalloc(xWantedSize);
}
// The memory being freed will have an BlockLink_t structure immediately before it.
puc -= xHeapStructSize;
@ -467,8 +472,7 @@ void *pvPortRealloc( void *pv, size_t xWantedSize )
pxLink = (void*) puc;
// Check allocate block
if ((pxLink->xBlockSize & xBlockAllocatedBit) != 0)
{
if ((pxLink->xBlockSize & xBlockAllocatedBit) != 0) {
// The block is being returned to the heap - it is no longer allocated.
block_size = (pxLink->xBlockSize & ~xBlockAllocatedBit) - xHeapStructSize;
@ -476,14 +480,10 @@ void *pvPortRealloc( void *pv, size_t xWantedSize )
pvReturn = pvPortMalloc(xWantedSize);
// Check creation and determine the data size to be copied to the new buffer
if (pvReturn != NULL)
{
if (block_size < xWantedSize)
{
if (pvReturn != NULL) {
if (block_size < xWantedSize) {
move_size = block_size;
}
else
{
} else {
move_size = xWantedSize;
}
@ -493,25 +493,10 @@ void *pvPortRealloc( void *pv, size_t xWantedSize )
// Free the old buffer
vPortFree(pv);
}
}
else
{
} else {
// pv does not point to a valid memory buffer. Allocate a new one
pvReturn = pvPortMalloc(xWantedSize);
}
}
else
{
// pv points to NULL. Allocate a new buffer.
pvReturn = pvPortMalloc(xWantedSize);
}
}
else
{
// Zero bytes requested, do nothing (according to libc, this behavior implementation defined)
pvReturn = NULL;
}
// Exit with memory block
return pvReturn;
}