Zig for the Uninitated: The Page Allocator
By Tyler
The Page Allocator
Well the day has finally arrived. We now begin to discuss the page allocator. I want to start with the page allocator partly because it’s the most um simple allocator that is in the zig standard library but also because it’s one of the most basic allocators.
So what is the page allocator? Well simply put (but maybe not helpfully) the page allocator allocates pages. The question then is what is a page? For that we need to go back and talk about the process memory segments. If you remember from the very first post that I made in Zig for the Uninitiated I talked about processes and how their memory is laid out. Different parts of a process go into “segements” of memory such as code, the heap and the stack. There are other possible segments, but for right now we’re going to talk about these three.
When a process is created, the Operating System gives it it’s Own “virtual address space”. This allows compilers to generate addresses to known locations in the program (for example, the address of an embedded table of data). This, however, requires the Operating System to map the virtual memory of each process into the Physical Memory.
In order to make this simple, and prevent fragmentation (a discussion for a different time), operating systems and hardware choose to divide memory into “Pages”; fixed size units of memory. The size of pages varies from architectures and OSes, but 4 KB is fairly typical.
What the Page allocator does is request one of those pages from the OS. This is the big caveat with the page allocator. You should not use the page allocator directly. That doesn’t mean you won’t ever use it in your code but you should not be calling alloc directly on the page allocator, unless you really know what you’re doing. Otherwise you will be eating up all the pages of your memory very fast. The Page Allocator will make allocate a full page to your program, every time alloc is called, even if you only need 1 byte.
What the page allocator is good for, is being used as a backing allocator. This means that we can give the page allocator to another implemenation of the allocator interface that uses a more sophisticated algorithm for managing the full page of memory recieved.
In the next posts, we’ll go over some of these derivative allocators and how they work.
Announcement
I also want to announce that I am now the owner and administrator of ziggit.dev. ziggit is a community for people who want to learn or who are programming in Zig. There’s documentation, there’s questions and answers, and you can go and ask questions and get access to some of the best minds of Zig (I’m not counting myself in that group). People like Andrew Kelly the creator of Zig and many of the other core developers of Zig are there, and occasionally answer questions. It’s a great website and you’re able to do other things like get explanations in depth about different topics of Zig as well as showcase any projects that you’re working on.