flatbuffers: DetachedBuffer cannot be rewrapped [C++, clang 6.0, OSX 10.11.6, master]
affected language: cpp compiler version: clang 6.0 operating system version: macOs 10.11.6 flatBuffers version: current
DetachedBuffer automatically manages the memory, which is good. However, it is kind of hard (rather impossible) to permanently release the memory from the DetachedBuffer.
I would like a functionality to get the pointers _cur and the size _reserved out from the detached buffer such that I can rewrap this memory somewhere else (without having to use type from the library). I have given FlatBufferBuilder a proxy object which forwards to my special allocator.
Something along the line:
AllocatorProxyFlatBuffer allocator(mySpecialAllocator);
flatbuffers::FlatBufferBuilder builder(1024, &allocator);
....
auto detachedBuffer = builder.Release();
uint8_t data = detachedBuffer.data() // pointer to the start of the data.
std:size_t size = detachedBuffer.size() // size of the data.
std::pair<uint8_t*, std::size_t> memory = detachedBuffer.Release();
// now we are responsible to deallocate `memory.first` with `memory.second` bytes.
// Obvisouly we gonna do this with our `mySpecialAllocator`... somewhen...
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 24 (18 by maintainers)
This is getting kinda multi-layered though, we created DetachedBuffer so people could hold on to the memory inside a FlatBufferBuilder. This made sense at at the time since FlatBuffer Builder stored extra memory for book-keeping, but since then that book-keeping has been removed, so now a FlatBufferBuilder and a DetachedBuffer use a similar amount of memory (one dynamic alloc each)!
So rather than creating 3 layers of buffer, I’d say if this functionality is needed, we should simply add a FlatBufferBuilder::ReleaseRaw instead.
We could almost remove DetachedBuffer at this point, but I guess people rely on it.