Hacker News new | ask | show | jobs
by mitulvohra 1182 days ago
I get that it is a specific allocator for niche use cases but i think it would be better that free throws some exception if it is done out of order rather than just being a no-op and making the programmer figure it out.
5 comments

Bump allocators of this type are meant to be reset to 0 at a known time when it's safe to do so. Its perfectly legal to not free in order. You really shouldn't care about freeing in order with them. Instead, one normally pays attention to when reset() is supposed to be called to not break anything.

For games it's easy to find a safe spot (end of frame). For a server, you might have have a pool of bump allocators (1 per connection), and you'd reset them after every http request. etc.

I think the out of order bit is fine, in most cases it’s not an issue and may even be necessary. Your proposal would be straight up incompatible with most collections, and greatly limiting to the rest.

But it should be clearly spelled out.

When your type is already called

    FixedBufferAllocator
You can probably extend it to

    FixedBufferBumpAllocator
And now the implications are more searchable and clearer.
Zig is low level programming language with explicit allocation controls.

This allocator is useful to write extremely efficient algorithms in certain scenarios.

If you wanted to throw on double free, you'd have to track what was freed and this tracking doesn't come for free.

Documentation has section on choosing allocator [0].

[0] https://ziglang.org/documentation/master/#Choosing-an-Alloca...

The point of these kinds of allocators is to not ever free their allocations. Unfortunately not having a free breaks generic code, so having a no-op free is the only real solution.
No. You definitely want it to not cause an error/panic (zig does not have exceptions).

1. Frees are never supposed to error

2. You want code to be interchangeable so that you can try out different allocators.

3. Eventually when someone writes a lifetime analysis tool for zig, you'll want to signify the memory as freed

4. If your program is long-running (the bumping is part of a subtask) you probably want the bump allocator to be itself freed on its own lifetime, so you'll eventually free that memory.