Merge pull request #6 from kareem-wolfssl/realloc

Update example pvPortRealloc example.
master
David Garske 2023-07-20 09:48:55 -07:00 committed by GitHub
commit 37c7afab75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 24 deletions

View File

@ -310,21 +310,20 @@ BlockLink_t *pxLink;
void *pvPortRealloc( void *pv, size_t xWantedSize ) void *pvPortRealloc( void *pv, size_t xWantedSize )
{ {
void *pvReturn = NULL; void *pvReturn = NULL;
if(pv) if (xWantedSize == 0)
{ {
BlockLink_t *pxLink = (BlockLink_t *)((char*)pv - xHeapStructSize); if (pv)
if(pxLink->xBlockSize & xBlockAllocatedBit) vPortFree(pv);
}
else if (pv)
{
pvReturn = pvPortMalloc(xWantedSize);
if(pvReturn)
{ {
uint32_t blockSize = (pxLink->xBlockSize & ~xBlockAllocatedBit); memcpy(pvReturn, pv, xWantedSize);
blockSize -= xHeapStructSize; vPortFree(pv);
pvReturn = pvPortMalloc(xWantedSize);
if(pvReturn)
{
memcpy(pvReturn, pv, blockSize);
vPortFree(pv);
}
} }
} }
else { else {

View File

@ -319,21 +319,20 @@ BlockLink_t *pxLink;
void *pvPortRealloc( void *pv, size_t xWantedSize ) void *pvPortRealloc( void *pv, size_t xWantedSize )
{ {
void *pvReturn = NULL; void *pvReturn = NULL;
if(pv) if (xWantedSize == 0)
{ {
BlockLink_t *pxLink = (BlockLink_t *)((char*)pv - uxHeapStructSize); if (pv)
if(pxLink->xBlockSize & xBlockAllocatedBit) vPortFree(pv);
}
else if (pv)
{
pvReturn = pvPortMalloc(xWantedSize);
if(pvReturn)
{ {
uint32_t blockSize = (pxLink->xBlockSize & ~xBlockAllocatedBit); memcpy(pvReturn, pv, xWantedSize);
blockSize -= uxHeapStructSize; vPortFree(pv);
pvReturn = pvPortMalloc(xWantedSize);
if(pvReturn)
{
memcpy(pvReturn, pv, blockSize);
vPortFree(pv);
}
} }
} }
else { else {