New function "pvPortRealloc"

Hi,

I implemented a new function “pvPortRealloc” for reallocating memory not present in heap_x.c.

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)
		{
	        /* The memory being freed will have an BlockLink_t structure 
             * immediately before it. */
	        puc -= xHeapStructSize;

	        /* This casting is to keep the compiler from issuing warnings. */
	        pxLink = ( void * ) puc;

	        heapVALIDATE_BLOCK_POINTER( pxLink );
	        configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 );

			if( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 )
			{
				/* Extract allocation size with masking */
				block_size = (pxLink->xBlockSize & ~heapBLOCK_ALLOCATED_BITMASK) - xHeapStructSize;

				/* Allocate new free space */
				pvReturn = pvPortCalloc(1, xWantedSize);

				/* Check valid allocation */
				if (pvReturn != NULL)
				{
					/* Move data from source to new buffer with limit size */
					if (block_size < xWantedSize)
					{
						/* New free space is smaller */
						move_size = block_size;
					}
					else
					{
						/* New free space is greater */
						move_size = xWantedSize;
					}

					/* Copy data from source */
					memcpy(pvReturn, pv, move_size);

					/* Free block of previous allocation */
					vPortFree(pv);
				}
			}
			else
			{
				/* This block is not allocate, create new */
				pvReturn = pvPortCalloc(1, xWantedSize);
			}
		}
		else
		{
			/* Pointer is NULL, create new */
			pvReturn = pvPortCalloc(1, xWantedSize);
		}
	}
	else
	{
		/* Exit without memory block */
		pvReturn = NULL;
	}
	
	return pvReturn;
}

In your experience, what can I improve, mitigate, modify the function to make it fit the FreeRTOs standard ?

Could this be useful ?

Thanks very much.

moreasm

Hey, feel free to open a PR! This seems like it could be useful.

I see it is calling pvPortCalloc with 1; should it just call pvPortMalloc?

Hi,

thanks a lot for the suggestion. I chose pvPortCalloc instead of pvPortMalloc because this way the new buffer is initialized to zero.

A definition could be added to select the desired behavior.

moreasm

I’d stick to the definition/behaviour of the standard
https://en.cppreference.com/w/c/memory/realloc
and would not silently use calloc internally.

Hi hs2,

thanks for the suggestion, i’ll try to rewrite the routine following “realloc” guidelines.

moreasm