sebbbi / OffsetAllocator

Fast O(1) offset allocator with minimal fragmentation
MIT License
754 stars 36 forks source link

Allocation tracking (and maxAllocs) has an off-by-one (or off-by-two?) error #3

Open paulbartrum opened 1 year ago

paulbartrum commented 1 year ago

Should the maxAllocs constructor parameter correspond to the maximum number of times allocate can be called? 'Cause if so, it's a bit off I think.

Here's a test case:

OffsetAllocator::Allocator allocator(36864, 2);
OffsetAllocator::Allocation a = allocator.allocate(32);
REQUIRE(a.offset == 0);
OffsetAllocator::Allocation b = allocator.allocate(32);
REQUIRE(a.offset == 0);
OffsetAllocator::Allocation c = allocator.allocate(32);
REQUIRE(a.offset == OffsetAllocator::Allocation::NO_SPACE);
allocator.free(a);
allocator.free(b);
allocator.free(c);

It fails on the first REQUIRE.

Thanks for open-sourcing this code by the way. I'm testing it out in my C# homebrew engine, it seems to work well so far :-)

sebbbi commented 1 year ago

This is true. It fails on allocation N-1. The test is wrong by one. I need to push a fix.

JustAndreww commented 1 year ago

Hello, no fix yet? :)

rkevingibson commented 2 months ago

Strictly as an FYI because I came across this issue when writing a C version of this allocator, there are two easy-to-fix issues here: