title: How would one implement a variable sized cache object to reduce memory allocations in C++?
body: <p>Before the performance people tear my head off: yes, I have done profiling before asking this :)</p>
  
  <p>I'm once again looking at my <a href="https://stackoverflow.com/questions/3221501/oneofatype-container-storing-one-each-of-a-given-type-in-a-container-am-i-o">one of a type container</a>, and while I have a solution that works, the performance is poor because each type of item that's cached results in a separate allocation on the heap (which is of course expensive).</p>
  
  <p>Based on static analysis of my program's input, I have figured out a way to know the total size required by all of the objects that might be put in my cache object that's getting passed around. Basically, I have a list of objects that may be constructed in a given cache object, so I know what the size of what I might have to cache is in advance, but not at compile time -- runtime only.</p>
  
  <p>Basically, what I'd like to do is what <code>boost::make_shared</code> does -- gets a single memory block, and constructs the <code>shared_ptr</code> bits as well as the controlled object in the same memory block.</p>
  
  <p>I don't have to worry about preserving copying behavior as the cache object is noncopyable and passed around by pointer by clients (it's usually stored in something like a <code>ptr_vector</code> or a <code>std::auto_ptr</code>).</p>
  
  <p>I'm not familiar however with how exactly one would implement such a container however, namely how one follows alignment restrictions and such.</p>
  
  <p>In pseudocode, what I'd like to do:</p>
  
  <pre><code>//I know a lot of what's in here is not portable -- I need to run only on x86
  //and x64 machines. Yes, this couple of classes looks hacky, but I'd rather
  //have one hacky class than a whole programfull :)
  
  class CacheRegistrar
  {
      //Blah blah
  public:
      //Figures out what objects will be in the cache, etc
      const std::vector&lt;std::size_t&gt;&amp; GetRequiredObjectSizes() const;
      //Other stuff...
      template &lt;typename T&gt;
      void RegisterCacheObject();
      template &lt;typename T&gt;
      std::size_t GetObjectIndex() const;
      // etc.
  };
  
  class CacheObject;
  
  std::auto_ptr&lt;CacheObject&gt; CacheObjectFactory(const CacheRegistrar&amp; registrar)
  {
      //Pretend this is in a CPP file and therefore CacheObject is defined...
      const std::vector&lt;size_t&gt;&amp; sizes(registrar.GetRequiredObjectSizes());
      std::size_t sumOfCache = std::accumulate(sizes.begin(), sizes.end());
      sumOfCache += sizeof(CacheObject);
      boost::scoped_array&lt;char&gt; buffer(new char[] sumOfCache);
      CacheObject *obj = new (reinterpret_cast&lt;void *&gt;(buffer.get())) CacheObject;
      buffer.release(); //PSEUDOCODE (boost::scoped_array has no release member);
      return std::auto_ptr&lt;CacheObject&gt;(obj); //Nothrow
  }
  
  class CacheObject
  {
      CacheRegistrar *registrar; //Set by my constructor
  public:
      template&lt;typename T&gt;
      T&amp; Get()
      {
          char * startOfCache = reinterpret_cast&lt;char *&gt;(this) + 
              sizeof(CacheObject);
          char * cacheItem = startOfCache + registrar-&gt;GetObjectIndex&lt;T&gt;();
          return *reinterpret_cast&lt;T*&gt;(cacheItem);
      }
  };
  </code></pre>
  
  <p>Is my general concept sound here? Is there are better way of accomplishing this?</p>