title: What data type to use for the buffer of a custom stack-based allocator?
body: <p>I want to create my own game engine so I bought a few books one being <em>Game Engine Architecture Second Edition</em> by <em>Jason Gregory</em> and in it he suggests implementing a few custom allocators. One type of allocator the book talked about was a stack-based allocator, but I got confused when reading it. How do you store data in it? What data type do you use? For example, do you use a <code>void*</code>, <code>void**</code>, an array of <code>char[]</code>? The book says you're meant to allocate one big block of memory using malloc in the begining and free it in the end, and "allocate" memory by incrementing a pointer. If you could help explain this more that would be great because I can't seem to find a tutorial that doesn't use std::allocator. I also thought this might help others interested in a custom allocator so I posted the question here.</p>
  
  <p>This is the header file example they give in the book:</p>
  
  <pre><code>class StackAllocator
  {
   public:
      // Represents the current top of the stack.
      // You can only roll back to the marker not to arbitrary locations within the stack
      typedef U32 Marker;
  
      explicit StackAllocator(U32 stackSize_bytes);
  
      void* alloc(U32 size_bytes); // Allocates a new block of the given size from stack top
      Marker getMarker(); // Returns a Marker to the current stack top
      void freeToMarker(Marker marker); // Rolls the stack back to a previous marker
      void clear(); // Clears the entire stack(rolls the stack back to zero)
  
  private:
      // ...
  }
  </code></pre>
  
  <p><strong>EDIT:</strong>
  After a while I got this working but I don't know if I'm doing it right</p>
  
  <p><strong>Header File</strong></p>
  
  <pre><code>typedef std::uint32_t U32;
  
  struct Marker {
      size_t currentSize;
  };
  
  class StackAllocator
  {
  private:
      void* m_buffer; // Buffer of memory
      size_t m_currSize = 0;
      size_t m_maxSize;
  
  public:
      void init(size_t stackSize_bytes); // allocates size of memory
      void shutDown();
  
      void* allocUnaligned(U32 size_bytes);
  
      Marker getMarker();
      void freeToMarker(Marker marker);
  
      void clear();
  };
  </code></pre>
  
  <p><strong>.cpp File</strong></p>
  
  <pre><code>void StackAllocator::init(size_t stackSize_bytes) {
      this-&gt;m_buffer = malloc(stackSize_bytes);
      this-&gt;m_maxSize = stackSize_bytes;
  }
  
  void StackAllocator::shutDown() {
      this-&gt;clear();
  
      free(m_buffer);
      m_buffer = nullptr;
  }
  
  void* StackAllocator::allocUnaligned(U32 size_bytes) {
      assert(m_maxSize - m_currSize &gt;= size_bytes);
  
      m_buffer = static_cast&lt;char*&gt;(m_buffer) + size_bytes;
      m_currSize += size_bytes;
      return m_buffer;
  }
  
  Marker StackAllocator::getMarker() {
      Marker marker;
      marker.currentSize = m_currSize;
      return marker;
  }
  
  void StackAllocator::freeToMarker(Marker marker) {
      U32 difference = m_currSize - marker.currentSize;
      m_currSize -= difference;
      m_buffer = static_cast&lt;char*&gt;(m_buffer) - difference;
  }
  
  void StackAllocator::clear() {
      m_buffer = static_cast&lt;char*&gt;(m_buffer) - m_currSize;
  }
  </code></pre>