3 ms·
Given how much research there is into this topic, I am sure that I just don't understand the complexity of it. But to me it seems like you could have a function
by celeritascelery 3y ago
Given how much research there is into this topic, I am sure that I just don't understand the complexity of it. But to me it seems like you could have a function that associates one value with another in the GC. Something like `gc_borrows_from`. You would than write the problematic code like this:
function blob_contents(blob)
local len_out = ffi.new('unsigned int')
local contents = gc_borrows_from(blob, hb.hb_blob_get_data(blob, len_out))
local len = len_out[0];
return ffi.string(contents, len)
end
This would tell the GC that the data returned by `hb_blob_get_data` is borrowed from blob, and it can't collect `blob` until `contents` is also unreachable. How to implement that would be up to the runtime, but it seems reasonable to have a wrapper type that holds a traceable reference back to blob.
- kevingadd 3y ago.NET calls this concept a 'dependent handle' if I understand you correctly, though allowing direct use of it is somewhat new: https://learn.microsoft.com/en-us/dotnet/api/system.runtime.dependenthandle https://learn.microsoft.com/en-us/dotnet/api/system.runtime.... And there's an older API that wraps them which I've found quite handy: https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.conditionalweaktable-2 https://learn.microsoft.com/en-us/dotnet/api/system.runtime.... Essentially, if you want to (externally - in code you wrote) associate two objects with each other without being able to modify the code of either of those objects, dependent handles are the ticket. It only creates a one-way liveness relationship, though, which may not be sufficient for every use case...
- planede 3y agoIn C++ that would be an "aliasing shared_ptr", for a reference counting example of the same idea.
- pizlonator 3y agoCute, but not a complete solution. Eventually you’ll want an object graph cycle that traverses through the non-GC heap and then you’re really screwed.