Skip to content

Fix heap free-list ordering when freeing the highest-address block - #26

Open
LinusU wants to merge 1 commit into
evmar:mainfrom
LinusU:lu-free-list-insertion
Open

LinusU wants to merge 1 commit into
evmar:mainfrom
LinusU:lu-free-list-insertion

Conversation

@LinusU

@LinusU LinusU commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

A current bug that ChatGPT 6 Pro noticed when reviewing some other code. The code in this PR was produced by DeepSeek v4 Flash 0731, running locally on my computer 🎉

When a freed region belongs after every existing free-list node, the insertion search left insert_index at 0, so the region was inserted at the front of the list, breaking ascending-address ordering and preventing coalescing with the preceding neighbor. Adjacent free regions could then remain unmerged, causing allocations to fail despite sufficient contiguous free memory.

Initialize the fallback insertion position to nodes.len() so appending at the end works. Adds regression tests covering the deterministic A/C/B free order, all six freeing permutations with invariant checks, and repeated-free behavior.

When a freed region belongs after every existing free-list node, the
insertion search left insert_index at 0, so the region was inserted at
the front of the list, breaking ascending-address ordering and preventing
coalescing with the preceding neighbor. Adjacent free regions could then
remain unmerged, causing allocations to fail despite sufficient contiguous
free memory.

Initialize the fallback insertion position to nodes.len() so appending at
the end works. Adds regression tests covering the deterministic A/C/B free
order, all six freeing permutations with invariant checks, and
repeated-free behavior.

This branch has not been deployed

No deployments
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