Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 16, 2021 10:33 pm GMT

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

Linux memory overcommit

Mac Overcommit

Mac memory 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

Windows memory reserve

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,
Allocator Data Structure.

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

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To