I had a similar idea a while ago for arena allocation, but I was thinking of just using the normal Go make to allocate a byte slice of a particular size. Then the user would “allocate” from the slice by getting the unsafe.Pointer for the tail of the buffer and convert the unsafe.Pointer to a pointer of their desired type.
I actually found your post while searching for some info about the behavior of memory in Go while making a prototype of such an arena allocator. Last time I did any research into this, I think the only thing that came up was some people doing “arenas” of homogeneous types–and I wanted to do heterogeneous types. I see your post is pretty recent!
It does work, but I can’t get Go to free the arena. In my simple test, I created and operated on an arena inside a function, figuring the GC would see that the arena was freeable when the function returned. I even did a call to I just tried setting the buffer to runtime.GC() to attempt to force a garbage collection, but just couldn’t get the memory back.nil and then calling runtime.GC() in the arena.free() func, and that does appear to get the GC to release the memory.
Your method of calling into CGo works really nicely though, and doesn’t rely on the GC to reclaim the memory whenever it wants.