Specifically right now lets say we have some kind of Registry of allocations
const Allocation = struct {
object:*anyopaque,
typeAlign: usize,
typeSize: usize,
allocator: std.mem.Allocator,
};
const Registry = struct {
allocated: ArrayList(Allocation),
pub fn emergencyShutdown(self: *@This()) void
{
for(self.allocated) |allocation|
{
// How would one destroy all of these allocations from here?
// in C we can just free() on the pointer but allocator.destroy()
// requires a typed pointer
}
}
};
My current solution is to keep a function pointer to a teardown function for each of the allocated objects. But Was wondering if it’s at all possible to map to the c style functionality
This is a bit tricky. The preferred functions, free and destroy, take the alignment at compile-time through the type information.
I guess the safest option would be to use rawFree which takes the alignment at runtime.
However it comes with a warning in the doc comment:
/// This function is not intended to be called except from within the
/// implementation of an Allocator
pub inline fn rawFree(self: Allocator, buf: []u8, log2_buf_align: u8, ret_addr: usize) void {
But it could be argued that you made something resembling parts of an “allocator” with your registry there. So I think it is alright to use this here:
Another option is to create a []u8 with the information you have and pass that to free. I know that the allocator documentation says that you should free the exact slice that you were give from alloc, but I’ve looked at a few implementations of allocators and, in all that I’ve looked, this would work.
This works well however it seems to piss off the GPA. GPA is expecting a free of alignment 8 for a larger struct, but []u8 has an alignment of 1.
Wonder how i can convince it that it’s ok. @alignCast requires a destination type to cast to in 0.11.0 and beyond. If this was 0.10 I could just specify a custom alignment.
Hmm interesting I’m trying to figure out how I can make that work for runtime. As the the alignment if I understand correctly can’t be changed, it’s part of the type.
Oh I suppose I can just allocate everything as if they were align 8. Like c style malloc behaviour. For release builds I just use malloc/free for this specific operation.
My current solution which satisfies the GPA is a comptime which properly casts it to the right type and frees it.