Is there a data structure for known size but with allocation on demand if usage exceed the known size?

I have a file format that I am parsing into a Zig structure. 99% of the time, in practice, a particular portion of the file would always have n size, and for this, I can parsed this into an array of n size.

But there is no restriction preventing the n to actually be n + x.

So I am looking for an approach that always parses the first n without needing to allocating, but if it encounters a file that has that portion set to n + x, then allocation kicks in to allocate the extra size required.

I am thinking I can have a struct that has both the array and an uninitialized ArrayList, and manually have the logic to only allocate the ArrayList when the n is filled up.

Before doing something like this, I was wondering if there is something in the std that combines working with known size with the option to also allocate on demand when needed.

Try StackFallbackAllocator

7 Likes

One thing you can do here is preallocate an array list with the size you expect and call append/addOne normally. The allocations won’t fail for the first N calls, but it will automatically start to allocate after you hit the initial capacity.
So you have one initial allocation at the begining that will handle 99% of your size, but then the fallback of kicking in later if you go over that size.
In Zig 0.14.0

var array_list: ArrayListUnmanaged(Type) = .initCapacity(alloc, n);
// If you have a buffer already
var array_list: ArrayListUnmanaged(Type) = .initBuffer(buf);
defer array_list.deinit(alloc);
// Use array_list normally
3 Likes