Get the latest tech news
Mistakes and cool things to do with arena allocators
When programming in Odin you can use arena allocators. If you use an arena allocator combined with a dynamic array, then there are a couple of pitfalls that may not be apparent at first. Let’s look at what arenas are, how you can run into trouble when naively using them with dynamic arrays and what you can do instead. What’s an arena? How does it work? Arenas and arena allocators are useful for grouping allocations that have the same lifetime.
Note the line dyn_arr.allocator = mem.panic_allocator() – This will make your program panic (crash on purpose) if the dynamic array tries to grow, as that would litter the arena in the ways we’ve talked about. An example is if you’re making a video editing software: Some users may use 10 megabytes of memory, while others may use 200 gigabytes, depending on their project sizes. Similarly to our previous example of using the panic_allocator, you could also use a static virtual arena to make it an error if the dynamic array tries to grow past the current block size.
Or read this on Hacker News