An Interest In:
Web News this Week
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
Advanced C: Arena Allocation
Hi Everyone, I'm going to starting a mini series of articles about Advanced C++ Programming concepts that are used in a lot of real projects. Today's topic is: Arena Allocation.
Code snippets and images posted in this article are under the MIT License
Memory management is a pain, isn't it?
When working in garbage collected languages such as Java or Go, you may be mostly free from dealing closely with memory but in languages like C and C++, memory usually causes a lot of problems, especially since you have a lot of power to manipulate it.
So what's the best allocator?
There is no number 1 best allocator in every scenario, rather, if you wanted the best allocator, the programmer is the best allocator because they know exactly what the program will do and thus know the best way to allocate memory.
Arena Allocation
Instead of allocating pointers using malloc
or new
, we can create our own allocator known as the arena allocator.
This kind of allocation involves allocating a large chunk of memory before the logic of your program executes, for example, 20 GiB of memory. Wait, hold up, this sound completely unreasonable right? Yes, it is, but the operating system knows this too, so it allows overcommitting memory.
Linux Overcommit
Mac Overcommit
Windows Overcommit
NOTE: Windows doesn't have the same ability to overcommit memory, rather large amounts of memory can be reserved and then requested
When you have your large memory allocated, what do you do with it?
When to use arena allocation
Each specific case cannot be listed as it's different for each program but here are some reasons I use it:
- Program run-time is short and I want fast allocation
- Create an optimized allocator that saves on performance and flexibility
Create an allocator
Here is the way I set up my arena allocator for my language Jet,
.
The way this is setup is pretty generic, it allows me to create multiple allocators for different parts of my program, which is general pretty common when using arena allocation. For example,
- Part A of Program 5 GiB
- Part B of Program 10 GiB
- Part C of Program 5 GiB
This is how the large pool of memory can be distributed and is useful if you know which part allocates more memory.
Conclusion
Arena allocation is just another tool in the box that will help you advance your knowledge of low-level programming in C++. Understanding allocators behind the scenes will help you in general for any kind of endeavor.
Original Link: https://dev.to/aboss123/advanced-c-arena-allocation-3580
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To