aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-François Milants <jf@codingfield.com>2023-04-08 15:30:14 +0200
committerJF <JF002@users.noreply.github.com>2023-05-18 19:58:09 +0200
commite038703efe134b3f6162051734f0df2d8bc6bb4d (patch)
treeecb3737cd2447c5191db03907dd19f8b202be954
parent611e0ff7684818949f7f18546b540ff55de9b8ce (diff)
Refactor pvPortRealloc() to improve readability.
-rw-r--r--src/FreeRTOS/heap_4_infinitime.c87
1 files changed, 36 insertions, 51 deletions
diff --git a/src/FreeRTOS/heap_4_infinitime.c b/src/FreeRTOS/heap_4_infinitime.c
index 31f34602..14708471 100644
--- a/src/FreeRTOS/heap_4_infinitime.c
+++ b/src/FreeRTOS/heap_4_infinitime.c
@@ -448,70 +448,55 @@ 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)
- {
- // The memory being freed will have an BlockLink_t structure immediately before it.
- puc -= xHeapStructSize;
+ if (xWantedSize == 0) {
+ // Zero bytes requested, do nothing (according to libc, this behavior implementation defined)
+ return NULL;
+ }
- // This casting is to keep the compiler from issuing warnings.
- pxLink = (void*) puc;
+ if (pv == NULL) {
+ // pv points to NULL. Allocate a new buffer.
+ return pvPortMalloc(xWantedSize);
+ }
- // Check allocate block
- if ((pxLink->xBlockSize & xBlockAllocatedBit) != 0)
- {
- // The block is being returned to the heap - it is no longer allocated.
- block_size = (pxLink->xBlockSize & ~xBlockAllocatedBit) - xHeapStructSize;
+ // The memory being freed will have an BlockLink_t structure immediately before it.
+ puc -= xHeapStructSize;
- // Allocate a new buffer
- pvReturn = pvPortMalloc(xWantedSize);
+ // This casting is to keep the compiler from issuing warnings.
+ pxLink = (void*) puc;
- // Check creation and determine the data size to be copied to the new buffer
- if (pvReturn != NULL)
- {
- if (block_size < xWantedSize)
- {
- move_size = block_size;
- }
- else
- {
- move_size = xWantedSize;
- }
+ // Check allocate block
+ if ((pxLink->xBlockSize & xBlockAllocatedBit) != 0) {
+ // The block is being returned to the heap - it is no longer allocated.
+ block_size = (pxLink->xBlockSize & ~xBlockAllocatedBit) - xHeapStructSize;
- // Copy the data from the old buffer to the new one
- memcpy(pvReturn, pv, move_size);
+ // Allocate a new buffer
+ pvReturn = pvPortMalloc(xWantedSize);
- // Free the old buffer
- vPortFree(pv);
- }
+ // Check creation and determine the data size to be copied to the new buffer
+ if (pvReturn != NULL) {
+ if (block_size < xWantedSize) {
+ move_size = block_size;
+ } else {
+ move_size = xWantedSize;
}
- 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);
+
+ // Copy the data from the old buffer to the new one
+ memcpy(pvReturn, pv, move_size);
+
+ // Free the old buffer
+ vPortFree(pv);
}
- }
- else
- {
- // Zero bytes requested, do nothing (according to libc, this behavior implementation defined)
- pvReturn = NULL;
+ } else {
+ // pv does not point to a valid memory buffer. Allocate a new one
+ pvReturn = pvPortMalloc(xWantedSize);
}
- // Exit with memory block
- return pvReturn;
-}
+ return pvReturn;
+} \ No newline at end of file