trunk: STL container changes

Discussion area about developing or extending OGRE, adding plugins for it or building applications on it. No newbie questions please, use the Help forum for that.
Post Reply
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19265
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 66
Contact:

trunk: STL container changes

Post by sinbad »

Hey guys,

Just a heads up that from now on in trunk, all STL containers are now using the configured custom memory allocators. This means that all containers need to have an allocator specified. There are some template helpers for this so that it's not so ugly (thanks to iloseall and Berserker for the help), so for example instead of this:

Code: Select all

typedef std::vector<Foo> FooList;
you would do this:

Code: Select all

typedef vector<Foo>::type FooList;
This template effectively expands to this:

Code: Select all

typedef std::vector<Foo, STLAllocator<GeneralAllocPolicy> > FooList;
There are expansions for vector, list, set, map, multimap and deque, which covers the ones we use. If you want to change the alloc policy, the shorthand version looks like this:

Code: Select all

typedef vector<Foo, ResourceAllocPolicy>::type FooList;
which expands to this:

Code: Select all

typedef std::vector<Foo, STLAllocator<ResourceAllocPolicy> > FooList;
I found that in one place (within an existing template definition) I had to use the expanded version rather than the shorthand version, but most of the time you won't need to. So if you're adding a new container, the easy way to be compliant is to remove the 'std::' prefix and add a '::type' postfix and hey presto, you've got the allocator-enabled version.

I've updated the coding standards to mention this in the memory management section.

User avatar
nullsquared
Old One
Posts: 3245
Joined: Tue Apr 24, 2007 8:23 pm
Location: NY, NY, USA
x 9

Re: trunk: STL container changes

Post by nullsquared »

Interesting. I really haven't followed much of Ogre's memory stuff, but what are the reasons for using it even for the STL containers? I'd think that STL containers would know how to manage their memory efficiently and won't leak it. Is this purely for statistical "memory usage" information? Will this definitely affect speed neutrally or positivity and not negatively?

Just curious.

User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Re: trunk: STL container changes

Post by xavier »

You may want to budget memory, and with STL there is no way to enforce, for example, how much memory your strings are using. So instead you override the STL allocators to hook in your memory management system.

It's very common in game code (especially on consoles where you get N bytes of memory and *that's all* ;) .
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.

User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19265
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 66
Contact:

Re: trunk: STL container changes

Post by sinbad »

Even if you don't need to customise it, this change will make your code faster. That's because all the allocations for container space will now go via nedmalloc, which is faster than the standard malloc.

This isn't about leaking at all in fact, as you say STL code should have been tested well enough to rule that out (except std:iostream on VStudio 2005 pre SP1 - doh!).

ven
Kobold
Posts: 35
Joined: Mon Sep 10, 2007 11:03 am
Location: St. Petersburg, Russia
x 1

Re: trunk: STL container changes

Post by ven »

How to (for reference)

Code: Select all

// only once in config file
template <typename T>
struct vector{
    typedef std::vector<T, std::allocator<T> > type;
};

// in code
typedef vector<Foo>::type FooVector;
typedef vector<Moo>::type MooVector;
If you want to replace allocator or even implementation of container you need change only one line

Code: Select all

template <typename T>
struct vector{
    typedef any::vector<T, any::allocator<T> > type;
};
In addition to "vector", "list", "set", "map", "multimap" and "deque", well it would be to replace allocators for all "string", "(io)stream", "(io)stringstream", etc.

// example for string typedef

Code: Select all

typedef std::basic_string<char, std::char_traits<char>, std::allocator<char> > string;
Thank developers c++ language and STL for a great opportunity

Post Reply