Page 1 of 1
Custom allocator
Posted: Tue Mar 24, 2026 7:13 am
by GhostedGaming
So I want to make a custom allocator and I was wondering if this was viable or not I know its a pretty bad diagram but you should get the gist of it. What should happen is when you allocate something it grows upwards and when you free something the top gets free which I'm kind of realizing that this is pretty bad design but I just want other opinions. I',m going to make a frame allocator and I want it to kind of grow upwards and I want to be able to free the pointer I want, now I haven't made a stack allocator before but I'm pretty sure it is how it sounds where it grows down.
Re: Custom allocator
Posted: Tue Mar 24, 2026 7:14 am
by GhostedGaming
And yes I know there's 3 used frames I just forgot to give the second one a page
Re: Custom allocator
Posted: Tue Mar 24, 2026 9:10 am
by GhostedGaming
Also realizing I shouldn't' do LIFO I should have it find the ptr I want to free and free it that way
Re: Custom allocator
Posted: Tue Mar 24, 2026 10:53 am
by Octocontrabass
I don't think you'll get useful feedback if you don't explain how your allocator works or how you intend to use it.
Re: Custom allocator
Posted: Wed Mar 25, 2026 9:06 am
by BenLunt
As Octocontrabass mentioned, without some documentation, there isn't going to be much feedback on your implementation.
However, I created a
simple memory allocator a while back that is easy to implement and targets the projects that are just starting out. You might want to have a look.
Here are some ideas that you might want to take into mind.
- For an OS, you will need to manage Virtual addresses and Physical addresses.
- Depending on your OS's design, you may need to manage multiple heaps, one or more per task, and one or more for the kernel.
- A majority of allocations will be allocated and then shortly freed. These allocations may be very similar in size. Therefore, as my example shows, save the last freed area so that when the next request is made, using this information, you might be able to quickly allocate the request and return. No need to search for a new available area.
- Not included with my example, but you might want to include a "trigger" page at the end of each allocation. When that page is accessed, your Page Handler can throw an exception indicating a buffer overrun. Quite useful when you allocate a stack for any given task.
- If you have a multi-processor/tasking/threading environment, you will need some kind of spinlock to make sure the memory's metadata remains in sync.
There are many other items/suggestions, all depending on preference and implementation of your memory allocator. If I was to mention only one thing that you would need to focus on, I would focus on speed. Memory allocation can be quite a bottleneck if not done correctly.
Ben
Re: Custom allocator
Posted: Thu Jun 04, 2026 10:52 am
by GNUDimarik
One more solution:
https://github.com/GNUDimarik/small_allocator/ I implemented it for my OS.
Another approach