|
31 | 31 | * (coalescences) adjacent memory blocks as they are freed, and in so doing |
32 | 32 | * limits memory fragmentation. |
33 | 33 | * |
| 34 | +* This implementation is based on heap_4.c and add the function pvPortRealloc() |
| 35 | +* to the original implementation. |
| 36 | +* |
34 | 37 | * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the |
35 | 38 | * memory management pages of http://www.FreeRTOS.org for more information. |
36 | 39 | */ |
37 | 40 | #include <stdlib.h> |
| 41 | +#include <string.h> |
38 | 42 |
|
39 | 43 | /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining |
40 | 44 | 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 ) |
442 | 446 | } |
443 | 447 | } |
444 | 448 |
|
| 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