3 ms·
> How would you know whether to call free or delete on it? By making ownership part of the API (and ABI). Sadly C is unable to express this, and thus so are F
by masklinn 4y ago
> How would you know whether to call free or delete on it?
By making ownership part of the API (and ABI).
Sadly C is unable to express this, and thus so are FFI layers.
- legalcorrection 4y ago
- olliej 4y agoI'm unsure why you're saying there's rust spam here? "lifetime" is not a rust only concept, syntactic lifetimes might be, but the idea of C APIs specifying the lifetime of a returned value is not new, novel, or rust specific. Many C APIs have SomeLibraryCreateObject(...), SomeLibraryRetainObject(...) and SomeLibraryReleaseObject(...) - or a more basic but less flexible SomeLibraryCreateFoo() SomeLibraryFreeFoo(...). The important thing is that the API specifies the lifetime of the returned value, idiomatic APIs do stuff like "SomeLibraryGet..." does not transfer ownership, "SomeLibraryCreate..." "SomeLibraryCopy..." etc do. Generally this works more robustly with some variation of refcounting, but you can be copy centric and say "if you want to keep this data call SomeLibraryCopy(..)".
- KerrAvon 4y agoC is unable to express this as a machine-readable attribute, but you can certainly document it as part of the API contract and teach the FFI layer about it. This doesn’t scale, but an FFI layer rarely translates directly into the language’s idiom without some manual effort.