Jump to content
  • Advertisement
Sign in to follow this  
allingm

STL Allocator Per Class Instance

This topic is 2639 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any way to create an STL class (for example Vector) and set the allocator per class instance? I can see a lot of stuff online that talks about global or class replacement of new and delete, but nothing replaces the allocation in one instance of a class. I wanted to provide the allocator every time I create the class, but when I try to use the allocator template parameter it doesn't seem to work correctly. Any tips? Is this even possible?

Share this post


Link to post
Share on other sites
Advertisement
I'm creating a framework for future projects/games. I was creating an allocator class that I wanted to pass to every object that needs to allocate. This way I can track allocations and/or change allocators to improve performance. I wanted to pass the allocator a class would use through its Init function. Currently I was implementing a Vector class, but instead of reinventing the wheel I figured I would wrap std::vector, so that later if I decided I wanted to implement my own I can. So, I was to wanting allocate vector's elements from my class instance specific allocator. The project is currently just starting, so I'm open to any suggestions. (I was also having similar problems with std::string.) For the most part I'm just blocking out the framework for the project.

I read about the allocators here:
http://bitsquid.blogspot.com/2010/09/custom-memory-allocation-in-c.html

Share this post


Link to post
Share on other sites
As it says in the bitsquid blog, STL allocators are only good for switching from one global allocation strategy (e.g. from [font="Courier New"]operator new[/font] / [font="Courier New"]operator delete[/font]) to another global allocation strategy (e.g. [font="Courier New"]malloc[/font] / [font="Courier New"]free[/font]). Trying to give state to an allocator instance doesn't work out very well in STL...

You could try using RDESTL instead of the standard C++ library (aka STL). You can see it's vector and allocator types are similar enough to the STL to be familiar, but are written in a readable style and are less retarded wink.gif

Share this post


Link to post
Share on other sites
Yeah, you're right. I was just hacking away hoping I could find a way. However, the RDESTL looks like it would be a great fit to my problem. The performance looks great along with the licensing terms. I'll try integrating it to see how it is.

Thanks Hodgman.

Share this post


Link to post
Share on other sites
If all you need is a way to divide up your SC++L containers into different categories, there's an easy solution:

  • Create an enum with an entry for each memory category
  • Create a templated allocator with a template parameter of that enum type
  • Use template typedefs to make a "tracked_container" typedef that lets you specify what category a container should allocate from, e.g. foolib::tracked_vector<int, CATEGORY_GENERAL>::type
  • You can then use another set of typedefs to further simplify this if you like, to e.g. TrackedScoreVector

    I've used this approach several times in large projects with good results.

    There are a number of other tricks involving RTTI (or custom reflection frameworks if you have one) and so on that can be used to give you very accurate breakdowns of how memory is used, all without requiring any changes to your code, so e.g. no evil NEW() macros.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!