ScratchAllocator returns memory from internal buffer if allocated size is larger than buffer size

Issue #7 new
Johannes Spohr
created an issue

The ScratchAllocator in memory.cpp fails to resort to its backing allocator if used like this:

uint32_t size = 4096;
memory_globals::init(size);
void * p = memory_globals::default_scratch_allocator().allocate(size+1);

The result is pointing to the allocator's ring buffer, which is smaller than the requested size. My suggestion is to add a check at the beginning of allocate():

if ((int)(size + sizeof(Header) + align-1) > (_end - _begin)) {
  // too large for ring buffer
  return _backing.allocate(size, align);
}

deallocate() should work as before, because it checks if the pointer is inside the ring buffer's bounds.

Comments (0)

  1. Log in to comment