0

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.

Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
  • I've been toying with designing such a thing on the ARM. The libraries I've seen aren't really designed for systems with 8K or less of RAM. If speed isn't critical, I would expect a garbage-collected string/array class could in many usage scenarios end up requiring less than half the RAM of the commonplace libraries (e.g. outside references would be `uint16_t` index into a managed array of references; small arrays of references or raw bytes would have an overhead of two bytes and references would take two bytes each; larger arrays would have about 4% extra overhead). – supercat Mar 02 '15 at 18:31
  • Does such a design seem like a reasonable idea? Do you know if it's been done? – supercat Mar 02 '15 at 18:31

1 Answers1

1

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
  • 31,405
  • 40
  • 123
  • 242