views:

45

answers:

1

On an embedded type system, I have created a Small Object Allocator that piggy backs on top of a standard memory allocation system. This allocator is a Boost::simple_segregated_storage<> class and it does exactly what I need - O(1) alloc/dealloc time on small objects at the cost of a touch of internal fragmentation. My question is how best to declare it. Right now, it's scope static declared in our mem code module, which is probably fine, but it feels a bit exposed there and is also now linked to that module forever. Normally, I declare it as a monostate or a singleton, but this uses the dynamic memory allocator (where this is located.) Furthermore, our dynamic memory allocator is being initialized and used before static object initialization occurs on our system (as again, the memory manager is pretty much the most fundamental component of an engine.) To get around this catch 22, I added an extra 'if the small memory allocator exists' to see if the small object allocator exists yet. That if that now must be run on every small object allocation. In the scheme of things, this is nearly negligable, but it still bothers me.

So the question is, is there a better way to declare this portion of the memory manager that helps decouple it from the memory module and perhaps not costing that extra isinitialized() if statement? If this method uses dynamic memory, please explain how to get around lack of initialization of the small object portion of the manager.

+1  A: 

A good guideline is: say what you mean, unless there is good reason to do otherwise. This allocator is a global static object, and should be declared as such. Now if its state needs initializing, I would do that in the code that initializes the dynamic memory allocator -- since this is in fact part of the work of initializing the memory allocation system, this again falls under the heading of saying what you mean. That would avoid the inelegant conditional check on every call.

rwallace
Therer was a reason I couldn't do that, but I don't remember what. I'll double check my code and get back to you.
Michael Dorgan