Arena is a grower now

This commit is contained in:
Talha Qamar 2025-02-01 06:19:29 +05:00
parent 724fd5ed4f
commit 720fd7f7ed

View file

@ -24,9 +24,13 @@ ArenaAllocator := struct {
log.debug("deinit: allocator")
}
alloc := fn(self: ^Self, $T: type, count: uint): ?^T {
if count * @sizeof(T) + self.allocated > self.size {
log.error("You allocated more memory on the arena than the arena had.");
die
if self.allocated + count * @sizeof(T) > self.size {
ptr := Target.realloc(self.ptr, self.size, self.size * 2)
if ptr == null {
log.error("Failed to grow arena");
die
}
self.ptr = @unwrap(ptr)
}
allocation := self.ptr + self.allocated
self.allocated = self.allocated + count * @sizeof(T)
@ -55,4 +59,4 @@ ArenaAllocator := struct {
}
return null
}
}
}