I have created a storage class to store arbitrary objects in linear memory. I think this is essentially a memory allocator but it stores a little information about the object allocated (size in bytes, number of objects)
This was originally meant to be the method of storing Components in an Entity-System.
Usually I hear that the STL is a better idea.
The obvious STL analogue is the std::vector. But the problem is how to store references to std::vector elements?
If you store a pointer to an element, that pointer may become invalid by re-sizing of the vector.
If you store an index, that index may also change.
The other problem is storing generic collections of std::vectors. Because it is a template you cannot have a homogenous collection of std::vectors: each one is a different type.
That is why this storage class is not a template: if it was a template I could not create a structure such as
Storage myStorsif Storage is a template<>
There are two supplemental classes that are to help manage the allocation and ownership of the elements: Range and Rangeset.
Range is just what it sounds like, it is a minimum and maximum value. A Rangeset is a sorted linked-list of Ranges so that ownership of elements need not always be 100% contiguous.
The Storage keeps track of which elements are available with it's own Rangeset, which should be disjoint with the Rangesets of the Entities that have ownership of the objects.
Again I have found that a situation such as this will be necessary to hold object Entities themselves if I want them to be contiguous in memory: they will require references to parents and children which can't be stored as pointers to std::vector elements because of the aforesaid considerations.
Please correct me if I am causing myself too much trouble!
Here is a link to the code (about 850 lines): http://dl.dropbox.com/u/78425/stor.h