Skip to content

Commit 9e808a6

Browse files
committed
Add pvPortRealloc() in heap_4_infinitime.c. An implementation of realloc() is needed by NimBLE.
1 parent 1df3d1a commit 9e808a6

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

src/FreeRTOS/heap_4_infinitime.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@
3131
* (coalescences) adjacent memory blocks as they are freed, and in so doing
3232
* limits memory fragmentation.
3333
*
34+
* This implementation is based on heap_4.c and add the function pvPortRealloc()
35+
* to the original implementation.
36+
*
3437
* See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the
3538
* memory management pages of http://www.FreeRTOS.org for more information.
3639
*/
3740
#include <stdlib.h>
41+
#include <string.h>
3842

3943
/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
4044
all the API functions to use the MPU wrappers. That should only be done when
@@ -442,3 +446,72 @@ static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )
442446
}
443447
}
444448

449+
/*-----------------------------------------------------------*/
450+
451+
void *pvPortRealloc( void *pv, size_t xWantedSize )
452+
{
453+
size_t move_size;
454+
size_t block_size;
455+
BlockLink_t* pxLink;
456+
void* pvReturn = NULL;
457+
uint8_t* puc = (uint8_t*) pv;
458+
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;
465+
466+
// This casting is to keep the compiler from issuing warnings.
467+
pxLink = (void*) puc;
468+
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;
474+
475+
// Allocate a new buffer
476+
pvReturn = pvPortMalloc(xWantedSize);
477+
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+
}
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);
495+
}
496+
}
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);
507+
}
508+
}
509+
else
510+
{
511+
// Zero bytes requested, do nothing (according to libc, this behavior implementation defined)
512+
pvReturn = NULL;
513+
}
514+
515+
// Exit with memory block
516+
return pvReturn;
517+
}

0 commit comments

Comments
 (0)