[unordered] memory questions

I'm on a platform where no memory can be dynamically allocated before main and all dynamically allocated memory must be released before main returns. Is there a way to ensure this behavior with boost's unordered containers if they have static duration? I.e. does the default constructor allocate memory and does clear or default construct + swap release all memory? Thanks, Michael Marcin

On 07/02/2008, Michael Marcin <mmarcin@method-solutions.com> wrote:
I'm on a platform where no memory can be dynamically allocated before main and all dynamically allocated memory must be released before main returns. Is there a way to ensure this behavior with boost's unordered containers if they have static duration?
No, there isn't, which is pretty much what you'd expect from the standard. Although, I think it is possible to do without breaking the standard. I'm not sure if 'clear' can free all memory though, as I'm sure if it can change the bucket count. I'm not sure about this. I initially started to write a fairly negative reply, but as I thought it through I realised that it might be pretty cheap to implement and might be desirable on other platforms as well. Is this a common requirement? Of course, it's also possible to use the preprocessor to only support this on certain platforms, but that could cause maintenance problems so I wouldn't want to do that. I'll think about it some more. Daniel

Michael: If you are allocating your container before main, you must know how much memory is needed at compile time, don't you? Can't you reserve that memory statically (e.g. a char buffer[SIZE]) and use an arena allocator for your statically created container? This goes for any container, not just the unordered ones. Just my $.02, -- Hervé Brönnimann hervebronnimann@mac.com On Feb 7, 2008, at 7:10 PM, Daniel James wrote:
On 07/02/2008, Michael Marcin <mmarcin@method-solutions.com> wrote:
I'm on a platform where no memory can be dynamically allocated before main and all dynamically allocated memory must be released before main returns. Is there a way to ensure this behavior with boost's unordered containers if they have static duration?
No, there isn't, which is pretty much what you'd expect from the standard. Although, I think it is possible to do without breaking the standard. I'm not sure if 'clear' can free all memory though, as I'm sure if it can change the bucket count.
I'm not sure about this. I initially started to write a fairly negative reply, but as I thought it through I realised that it might be pretty cheap to implement and might be desirable on other platforms as well. Is this a common requirement?
Of course, it's also possible to use the preprocessor to only support this on certain platforms, but that could cause maintenance problems so I wouldn't want to do that.
I'll think about it some more.
Daniel _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/ listinfo.cgi/boost

Hervé Brönnimann wrote:
Michael: If you are allocating your container before main, you must know how much memory is needed at compile time, don't you? Can't you reserve that memory statically (e.g. a char buffer[SIZE]) and use an arena allocator for your statically created container? This goes for any container, not just the unordered ones. Just my $.02,
Or you could use operator::new to construct the container at the start of main, and free it before main returns? Another 2c, John.

John Maddock wrote:
Hervé Brönnimann wrote:
Michael: If you are allocating your container before main, you must know how much memory is needed at compile time, don't you? Can't you reserve that memory statically (e.g. a char buffer[SIZE]) and use an arena allocator for your statically created container? This goes for any container, not just the unordered ones. Just my $.02,
Or you could use operator::new to construct the container at the start of main, and free it before main returns?
Another 2c, John.
That is possible but does carry a bit more overhead and is the fallback solution. I'm just wondering if, while using the Boost implementation of unordered containers something like: template< typename V, typename H, typename P, typename A > void purge_memory( unordered_set<V,H,P,A>& c ) { unordered_set<V,H,P,A>().swap( c ); } would release all the memory. Thanks, Michael Marcin

Hervé Brönnimann wrote:
Michael: If you are allocating your container before main, you must know how much memory is needed at compile time, don't you? Can't you reserve that memory statically (e.g. a char buffer[SIZE]) and use an arena allocator for your statically created container? This goes for any container, not just the unordered ones. Just my $.02,
The usage I was thinking of was the container stored in an object that was used as a Meyer's singleton (i.e. function local static) that would not be accessed before main. - Michael Marcin
participants (4)
-
Daniel James
-
Hervé Brönnimann
-
John Maddock
-
Michael Marcin