Skip to content

Conversation

@skb666
Copy link

@skb666 skb666 commented Dec 26, 2025

Description

This PR introduces a new API pvPortRealloc to FreeRTOS, providing dynamic memory reallocation similar to the standard C library's realloc. With configSUPPORT_HEAP_REALLOC enabled, applications can now resize heap-allocated memory blocks at runtime, either shrinking, expanding in place, or reallocating with data migration when needed.

Key Changes:

  • Added the pvPortRealloc API in portable/MemMang/heap_4.c and heap_5.c.
  • Added the configuration macro configSUPPORT_HEAP_REALLOC (default 0, must be 1 to enable realloc support).
  • Updated FreeRTOSConfig.h and checked for correct configuration use.
  • Critical sections and heap integrity checks are enforced throughout realloc logic.
  • Follows realloc semantics:
    • NULL pointer behaves like malloc.
    • Size 0 behaves like free.
    • Tries in-place expand or shrink when possible; otherwise allocates a new block, copies data, and frees the old one.

Test Steps

  1. Enable configSUPPORT_HEAP_REALLOC and ensure dynamic allocation is also enabled.
  2. Build with either heap_4.c or heap_5.c.
  3. Call pvPortRealloc to:
    • Expand, shrink, and reduce memory blocks.
    • Pass NULL as input.
    • Pass size 0.
    • Test fragmentation and edge cases (failing allocation, invalid pointers).
  4. Validate heap stats and application logic remain correct.

Checklist:

  • I have tested my changes. No regression in existing tests.
  • I have modified and/or added unit-tests to cover the code changes in this Pull Request.

Related Issue

#1315

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant