Sign in to follow this  
skwee

Custom memory management and Allocators.

Recommended Posts

Hey there, this topic is interesting for me on its theoretical and practical sides. I would like to know more on the following topic: 1. Custom allocators for STL in C++. How to write them? How they work? 2. Making something like the STL's custom allocators for my defined structures, like it will use the default new/delete if no allocator was provided (think of it as rewriting STL). 3. Custom memory management. I heard and read about memory pools, how to work with them. I would like to know more about this topic (memory pools and another methods if any) for RT programs, what is the best way to manage memory, algorithms and etc. Thanks a lot for help!

Share this post


Link to post
Share on other sites
1) checkout rip-off's link. But make one BIG note. The allocator itself has to be stateless. The functions you call for memory allocation must hold their own internal state about the memory being allocated since the allocator cannot.
This means that writing custom allocators for the most part ammounts to calling "custom_malloc" and placement new along side "custom_free" and the destructor.

2) Seems complicated. If you are already making custom structures, why not just overload operator new for them? If your going to write something as generic as STL, just use STL. If you are going to write something custom, just write it custom.

3) Look for stuff like:
Memory Pools - pool of like-sized items for fast alloc/dealloc (when i profiled this was only ~2x faster than new/delete). Good because it avoids fragmentation (in the same profile new/delete eventually failed). Pool allocator could be designed to round-up random allocation sizes to the next pool size, thus wasting memory but eliminating fragmentation.

Arena Allocators - Alloc once from a stack. No item-wise free. Instead the entire arena is just reset (fast alloc, and VERY fast free). Can't be used with items that need auto-magic destruction (you MUST call arena_free on any item that needs its destructor called)

Memory Alignment - Alignment matters! expecially on consoles and and PC that you intend to use vector operations on (PSP's VFPU, PS3's CELL SPU, XBox/PC SSE).

Share this post


Link to post
Share on other sites
Thank you both for the comments.

KulSeran
About 2, yes I can just override the new/delete operators, but it will limit my code to once specific allocation method, for example allocating from memory pool, and the code won't be portable without the memory pool, or in case if Ill decide to use another method for allocation.
3. Thanks Ill check it out.

If someone have something else to add, please!

Thanks a lot!

Share this post


Link to post
Share on other sites
Quote:
Original post by s.kwee
KulSeran
About 2, yes I can just override the new/delete operators, but it will limit my code to once specific allocation method, for example allocating from memory pool, and the code won't be portable without the memory pool, or in case if Ill decide to use another method for allocation.
If you need flexible containers with optional custom allocators, use the std::library containers. Writing your own containers is error prone, and almost never necessary.

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
Quote:
Original post by s.kwee
KulSeran
About 2, yes I can just override the new/delete operators, but it will limit my code to once specific allocation method, for example allocating from memory pool, and the code won't be portable without the memory pool, or in case if Ill decide to use another method for allocation.
If you need flexible containers with optional custom allocators, use the std::library containers. Writing your own containers is error prone, and almost never necessary.


I didn't talk about containers, and before Ill start to rewrite the functionality STL gives me Ill be 100% sure that STL is the bottle-neck and I need to write my own.
What I do want to write is custom allocation for specific objects (textures, meshes, entities), or better to say: to leave the option to implement custom allocators for my objects later.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this