Skip to content

[alloc+atomic] Fix missing cascade_discard_free_tail after non-tail dealloc/realloc#10

Open
williamwutq wants to merge 6 commits into
masterfrom
first-fit-fix
Open

[alloc+atomic] Fix missing cascade_discard_free_tail after non-tail dealloc/realloc#10
williamwutq wants to merge 6 commits into
masterfrom
first-fit-fix

Conversation

@williamwutq
Copy link
Copy Markdown
Owner

Description: cascade_discard_free_tail was only called from the explicit tail-discard path in dealloc. When coalescing in add_to_free_list produced a merged block ending at the stack tail, that block was never reclaimed — leaving the arena non-empty after all allocations were freed. Fixed by calling cascade after every add_to_free_list in both dealloc and realloc (Rust + C). This is purely a space reclamation bug — blocks are correctly marked free and coalesced, they just aren't discarded from the tail. Existing allocated blocks are never touched, and the free list remains structurally valid throughout. The only effect is the file grows larger than necessary and never shrinks back to the header size.

Important Feature: No
Type: Allocator - Correction; Allocator - Concurrent
Tests: Included
Feature Flags: alloc + set + atomic
Breaking change: No
New Types: None
Rust Only: No
Fuzz: Yes
Safety Review: Needed: Crash Safety, Invariants, Thread Safety

@williamwutq williamwutq self-assigned this Jun 2, 2026
@williamwutq williamwutq marked this pull request as ready for review June 2, 2026 23:09
@williamwutq williamwutq requested a review from Copilot June 2, 2026 23:09
Copy link
Copy Markdown
Contributor

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 a space-reclamation bug in the first-fit allocator where coalescing during dealloc/realloc could create a free block at the arena tail that was never discarded, leaving the backing file larger than necessary after all allocations were freed.

Changes:

  • Call cascade_discard_free_tail after every add_to_free_list in Rust dealloc/realloc to ensure tail free blocks are reclaimed.
  • Mirror the same tail-cascade calls in the C allocator implementation.
  • Document the behavior change (allocator comments + changelog entry).

Reviewed changes

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

File Description
src/alloc/first_fit.rs Adds tail-cascade calls after add_to_free_list and updates comments clarifying tail reclamation responsibility.
c/bstack_alloc.c Adds tail-cascade calls after alff_add_to_free_list in dealloc/realloc.
CHANGELOG.md Adds an Unreleased entry documenting the tail reclamation fix.

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

Comment thread src/alloc/first_fit.rs
Comment thread src/alloc/first_fit.rs
Comment thread CHANGELOG.md Outdated
williamwutq and others added 2 commits June 2, 2026 16:13
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.

2 participants