Skip to content

fix: preserve merged block when frame deallocation reaches the top order#46

Merged
jiegec merged 1 commit intorcore-os:masterfrom
mevinagrise:fix-frame-top-order-merge
Mar 26, 2026
Merged

fix: preserve merged block when frame deallocation reaches the top order#46
jiegec merged 1 commit intorcore-os:masterfrom
mevinagrise:fix-frame-top-order-merge

Conversation

@mevinagrise
Copy link
Copy Markdown
Contributor

This PR fixes a FrameAllocator deallocation edge case where a fully merged block could be lost when buddy merging reached the top order. It aligns the implementation with the robust "merge-then-insert" pattern used in Heap::dealloc.

Specific changes include:

  • Restricted buddy merging to valid higher orders only.
  • Ensured the final merged block is always inserted back into the free list after deallocation.
  • Added a regression test covering the case where two adjacent top-order blocks are freed and should remain reusable.

Why this is useful:

  • Without this fix, a deallocation at the merge boundary can silently lose reusable memory.
  • The new test makes the regression easy to catch in the future.
  • The change is limited to FrameAllocator deallocation behavior and does not alter the public API.

Testing:

  • Ran cargo test.
  • Ran cargo test --doc.

@jiegec
Copy link
Copy Markdown
Member

jiegec commented Mar 26, 2026

Please add a regression test.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an edge case in the buddy-style FrameAllocator deallocation logic where reaching the maximum order during buddy merging could cause the final merged block to never be reinserted into the free list (silently losing reusable memory). The change brings FrameAllocator deallocation in line with the “merge-then-insert” approach already used elsewhere (e.g., heap dealloc behavior).

Changes:

  • Prevent buddy-merge attempts beyond the highest valid order.
  • Always reinsert the final merged block into the appropriate free list after merging completes.
  • Add a regression test that reproduces the “top-order merge loses block” scenario.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/frame.rs Adjusts merge loop bounds and ensures the final merged block is inserted back into the free list.
src/test.rs Adds a regression test covering deallocation/merge behavior at the maximum order boundary.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mevinagrise
Copy link
Copy Markdown
Contributor Author

Added a regression test in test_frame_allocator_merge_final_order in src/test.rs.

It allocates two adjacent top-order blocks, deallocates both of them, and asserts that allocating the same size succeeds again, which reproduces the lost-free-block case this PR fixes.

@jiegec jiegec merged commit 0e44835 into rcore-os:master Mar 26, 2026
6 checks passed
@mevinagrise mevinagrise deleted the fix-frame-top-order-merge branch March 30, 2026 08:09
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.

3 participants