Custom allocator

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
GhostedGaming
Member
Member
Posts: 35
Joined: Wed Jul 02, 2025 11:46 pm
Libera.chat IRC: GhostedGaming

Custom allocator

Post by GhostedGaming »

Screenshot 2026-03-24 8.57.04 AM.png
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.
Last edited by GhostedGaming on Tue Mar 24, 2026 11:00 am, edited 1 time in total.
GhostedGaming
Member
Member
Posts: 35
Joined: Wed Jul 02, 2025 11:46 pm
Libera.chat IRC: GhostedGaming

Re: Custom allocator

Post by GhostedGaming »

And yes I know there's 3 used frames I just forgot to give the second one a page
GhostedGaming
Member
Member
Posts: 35
Joined: Wed Jul 02, 2025 11:46 pm
Libera.chat IRC: GhostedGaming

Re: Custom allocator

Post 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
Octocontrabass
Member
Member
Posts: 6275
Joined: Mon Mar 25, 2013 7:01 pm

Re: Custom allocator

Post 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.
User avatar
BenLunt
Member
Member
Posts: 1039
Joined: Sat Nov 22, 2014 6:33 pm
Location: USA
Contact:

Re: Custom allocator

Post 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
GNUDimarik
Posts: 1
Joined: Tue Oct 11, 2022 9:00 pm

Re: Custom allocator

Post by GNUDimarik »

One more solution: https://github.com/GNUDimarik/small_allocator/ I implemented it for my OS.
Another approach
Post Reply