Skip to content

Conversation

@timkaechele
Copy link
Contributor

@timkaechele timkaechele commented Oct 22, 2025

Based on the arena pages proposed in #708 by @marcoroth, this PR builds on the concept and adds a more refined reset behavior that merges obsolete pages into one big page.

This has the advantage that we won't have a bunch of small pages, and instead have more contiguous memory available.

How it works

# Initial state
| Page 1 (4kb) | - next > | Page 2 (4kb) | - next > | Page 1 (16kb) | 

# reset to  4kb
| Page 1 (4kb) | - next > | Page 2 (24kb) |

# reset to 0kb
| Page 1 (4kb) | - next > | Page 2 (24kb) |

For now the first page is never deallocated and merged into a bigger page for two reasons:

  1. It's an edge case that needs to be handled.
  2. If the allocation of a new arena page fails at least you still have the first page. 🤷‍♂️

@marcoroth marcoroth added feature New feature or request refactor c labels Oct 22, 2025
@timkaechele timkaechele force-pushed the merging-arena-pages branch 2 times, most recently from f31b7bb to 0255d2c Compare October 23, 2025 06:34
Comment on lines 103 to 106
if (allocator->tail->next != NULL && hb_arena_page_has_capacity(allocator->tail->next, required_size)) {
allocator->tail = allocator->tail->next;
} else {
bool allocated = hb_arena_append_page(allocator, required_size);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO: Potential memory leak.

In a situation where the tail has a next page, but the page does not have a sufficient capacity , we would just override the page next pointer and leak the existing page that didn't have enough capacity to hold the value

Reference: append_page implementation
https://github.com/marcoroth/herb/pull/719/files#diff-38530fcdce39968fc659335346b4b6c0d6be9af0f16e2ed1354ca3bb693b2461R78

Copy link
Owner

@marcoroth marcoroth Oct 23, 2025

Choose a reason for hiding this comment

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

@timkaechele timkaechele marked this pull request as draft October 23, 2025 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants