Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

worst that can happen if i don't obey the stateless custom allocators dogma?

I need to create a custom allocator for std:: objects (particularly and initially for std::vector) but it might eventually come to use others

The reason I need to create a custom allocator is that I need to track allocated (heap & stack) resources by individual components of the application (this is an inherent feature of the application). I will need the custom allocator to monitor the heap portion of the resources, so it is essential that I'm able to pass to the std::vector constructor something like

trackerId idToTrackUsage;
myAlloca<int> allocator(idToTrackUsage);
vector<int> Foo( allocator );

However, after reading a bit I found this little bomb about the STL / C++ standard (see references) saying that all allocator instances of a given type should be equivalent (that is that == should return true for any two instances) and, most terminal; any allocator should be able to deallocate memory allocated by any other instance (that is, without having a way to know what that other instance might be). In short, allocators cannot have state.

So I'm trying to find the best way around this. Any clever ideas? I really really REALLY don't want to have to keep a custom version of std::vector around.

EDIT: i read about scoped allocators for c++0x on http://www2.research.att.com/~bs/C++0xFAQ.html#scoped-allocator but i couldn't really get far into understanding how this applies to my problem. If anyone thinks c++0x alleviates this problem, please comment

References:

Allocator C++ article in Wikipedia

Some random further reading courtesy of Google

like image 279
lurscher Avatar asked Nov 05 '10 21:11

lurscher


1 Answers

Aside from the obvious answer ("if you violate any requirement, that's undefined behavior, good night and thanks for playing"), I imagine the worst that would likely happen, is that the vector implementation can rely on the requirement that "all instances of the allocator class are interchangeable" in the obvious way:

vector(const Allocator &youralloc = Allocator()) {
    const Allocator hawhaw;
    // use hawhaw and ignore youralloc. 
    // They're interchangeable, remember?
}

Looking at the source, GCC's vector implementation (which I think is based eventually on SGI's original STL implementation) does sort-of store a copy of the allocator object passed into that constructor, so there's some hope that this won't happen.

I'd say try it and see, and document what you've done very carefully, so that anyone attempting to use your code on an implementation that you haven't checked, knows what's going on. Implementers are encouraged in the standard to relax the restrictions on allocators, so it would be a dirty trick to make it look as though they're relaxed when really they aren't. Which doesn't mean it won't happen.

If you're really lucky, there's some documentation for your container implementation that talks about allocators.

like image 186
Steve Jessop Avatar answered Sep 21 '22 16:09

Steve Jessop