Can I do this even if I don’t have mutable access to the Arc? I explored various ways of dropping it but end up giving up since it’s a ‘ref’ and not ‘mut ref’… (I guess I could look up the answer myself but for sake of clarity for other readers I ask here)
drop_in_place takes a
*mut T, not an
&mut T (pointers and references are very different beasts and you can get a mutable pointer from a shared reference). As long as you can guarantee that there are no other existing references to that particular copy of the
Arc<...>, you should be fine. However, if you do it this way, you won’t be able to reinitialize the module’s state unless
MODULE somehow gets restored to its initial state (I don’t know how module loading works in freebsd).
OTH, when doing a Arc<Mutex<Option>> I can safely take() T and drop it while mutex locked and I will only leak the size of Mutex struct (a few bytes). Maybe that is acceptable since you generally don’t load and unload modules. Writing some nice accessors will reduce bloat also.
That should leak the the size of
T is just some
Box<U>, that’s not a big issue (just a pointer) but if
T is large, you’ll leak a lot of memory. Regardless, leaking heap memory in the kernel sounds like a bad idea anyways.
Given that the kernel will (I assume) call initialize/deinitialize functions on load/unload, you may want to drop the
lazy_static dependency entirely and implement this from scratch. Also, I’m pretty sure you don’t even need the
Mutex<Hello> should do) as you’re dealing with statics (unless
Hello needs to outlive your kernel module).