Rust Smart Pointers: Box, Rc, and Arc
Rust's three foundational smart pointers each solve a distinct ownership/allocation problem while sharing the core property of behaving like a pointer with additional owned behavior: Box provides sin…
Several parts of a driver often need to hold the same state at once: the probe function, an interrupt handler, an open device file. The kernel's own reference-counted pointer lets each of them hold a handle, counts the handles safely across processors, and frees the state exactly when the last one goes away. It differs from the standard-library version in two ways that matter: creating it can fail, because it allocates, and it is built to work with pinned contents. Getting this wrong by hand in C is how drivers end up using freed memory after a device is unplugged.
Rust's three foundational smart pointers each solve a distinct ownership/allocation problem while sharing the core property of behaving like a pointer with additional owned behavior: Box provides sin…