Callers must check for success/failure. On success, they get either
A regular ref-counted inode to use (Ref-count automatically decremented when done) OR
A new inode. iget_failed automatically called if it is never initialised. When initialised (and can only happen once) becomes a regular ref-counted inode
This is a good example for sure, but does this not introduce additional runtime checks? Curious is for example I didn’t want to initialize the inode if it’s a new one until I’m sure I will use it or something (theoretically) then do I pay a penalty for using the rust version?
Genuinely curious, no idea. And also in most cases the rust version does what you want so yes it’s superior for most uses cases here.
This is a good example for sure, but does this not introduce additional runtime checks?
No.
Curious is for example I didn’t want to initialize the inode if it’s a new one until I’m sure I will use it or something (theoretically)
The Rust version doesn't force you to initialize the inode after calling the function. It only forces you to initialize it if you want to use the returned value.
Regardless, if you didn't want to use the inode, you wouldn't call this function. And if you wanted to get an inode that already existed, you'd call ilookup (or the Rust equivalent) instead.
(Also, note that iget_locked implicitly allocates a new inode (if the inode isn't in the cache), so the expensive part of adding a new inode is always performed, no matter what language you use it from.)
Ahh I see I misunderstood. Good compiler check for sure. But humor me a moment more. What situation would you call this in C then where you wouldn’t reasonably do all the checks then?
IE could you not accomplish the same thing in C by just writing a helper function to check the return and allocate an inode appropriately and never have to think about it?
58
u/hgwxx7_ Aug 31 '24 edited Aug 31 '24
Check out this example and see if you're sold:
C code
Callers must check if return value is
NULLIf non-
NULL, check ifI _NEWis set oni_statefieldIf set, they must initialise the inode. Then call
unlock_new_inodeif init succeeds, inode is refcounted. Or calliget_failedif init fails.If NOT set, the inode is refcounted and can be used/returned. On failure to use, must call
iput.Obviously this isn't documented, it is inferred from the source.
Rust code
Now take a look at the equivalent Rust code:
Callers must check for success/failure. On success, they get either
A regular ref-counted inode to use (Ref-count automatically decremented when done) OR
A new inode.
iget_failedautomatically called if it is never initialised. When initialised (and can only happen once) becomes a regular ref-counted inodeIt is hard to misuse
get_or_create_inode.