I have a struct that uses an arena, because everything it creates needs to be destroyed at the same time, and this makes the destory step much much faster:
/// Create a `Resources` object that holds all its
/// objects in its own arena.
pub fn create(gpa: Allocator) error{OutOfMemory}!*Resources {
var arena = try gpa.create(std.heap.ArenaAllocator);
errdefer gpa.destroy(arena);
arena.* = std.heap.ArenaAllocator.init(gpa);
const arena_allocator = arena.allocator();
const resources = try gpa.create(Resources);
resources.* = Resources{
.arena = arena,
.arena_allocator = arena_allocator,
.folder = "",
.used_resources = null,
};
return resources;
}
Im not sure if this is how I should be setting up an arena. This was one of the very first times I tried to use an arena, so its probably odd 🙂
