#Freeing raw pointer allocated by Zig allocator

1 messages · Page 1 of 1 (latest)

cyan atlas
#

is it possible to free the amount of bytes allocated without having a slice?

I have to interop with C and I have provided a malloc function that uses wasm_allocator, but I have to return a raw ptr rather than the slice. how can I free that raw pointer? do I just need a separate map of pointer -> amount allocated?

or can I just lie and even do like @as([*]u8, @ptrCast(rawPtr))[0..1] and the allocator will still free everything?

dusky roost
#

passing the wrong length slice to free is a violation of the allocator iterface, however, some allocator implementations may handle it correctly (and others wont); so it really depends on what the allocator does

the correct way to handle this is to store the length metadata somewhere (potentially before the pointer) and then reconstruct the slice when you free it

cyan atlas
#

that makes sense, I imagine I can just allocate +4 bytes than required, assign the length to that 4 byte margin and return the rest, then assume those 4 bytes exist when freeing to get the amount allocated

#

or 8 bytes I guess