🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Creating a custom stack allocator for game engine

Started by
20 comments, last by JoshuaManton 1 year, 10 months ago

I'm currently reading through Game Engine Architecture Vol. 3 by Jason Gregory, and I've gotten to the section of memory management. I want to try and implement the custom stack allocator, but doesn't give any implementation details, and I'm not sure how to implement it. I would say I'm a beginner C++ programmer, so I know there's a lot I haven't learned yet. Where are some good places to go to learn more about C++ memory management?

Advertisement

That's because the book is aimed for experienced programmers so it only shows an overview how the dependencies work rather than the details.

Fortunately this forum has an article posted about custom memory allocators, including the stack allocator you asked for: https://www.gamedev.net/tutorials/programming/general-and-gameplay-programming/c-custom-memory-allocation-r3010/

I used this to get an idea of how memory management could work: https://blog.molecular-matters.com/2011/07/05/memory-system-part-1/

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

{moderator hat on} I've hidden several completely unrelated posts talking about Unity. {moderator hat off}

Both of those links are good references. It is common for games to implement custom allocators, but they're tricky to get right for beginners. Alignment issues, dealing with holes and fragmentation, dealing with leaks and overruns and tracking, dealing with multithreaded race conditions and reentrant conditions, they're all beyond where I would put typical beginner topics.

You might also look at the various allocators found in Boost, each with their own purposes. Various types of pool allocators, shared memory allocators for interprocess communication, single thread and multithread versions, etc.

While allocations from the system are somewhat slow, they are potentially doing quite a lot of work that needs to be done at least once. Worst case it needs to prepare the data for your local process which includes wiping out the old contents, can require globally locking pages of memory, and so on. In the best case you've already got a chunk of memory free in your process and it can immediately assign it. For small projects it's usually not an issue since small allocations are typically quick. For large games it's better to take a large performance hit up front for the system allocation, and then pay the smaller local hit within your own allocators.

@undefined I figured, whats the way I can get to the experience level needed? What are some topics I should learn before revisiting the book?

Study it and find out what you can. Some you will understand, some you won't. Over time you will understand more. There will also be elements that you see and think you understand, but discover later that you misunderstood or missed elements that you did not see before.

Reading is a good way to learn, and you are doing it. Trying to build it and encountering issues is another way to learn, so go for it. After you encounter issues and struggle with them a bit, the other implementations might be more understandable, or at least, you might recognize the solution embedded in their code and comments.

So basically, do what you are already doing.

This topic is closed to new replies.

Advertisement