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.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of things

things

Photo of mistakes

mistakes

Photo of arena allocators

arena allocators

Related news:

News photo

EA reminds Battlefield players, "things taking place in Labs may or may not make their way to the final" game

News photo

The Story Behind “100 Go Mistakes and How to Avoid Them”

News photo

Incident.io raises $62M at a $400M valuation to help IT teams move fast when things break