1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- use std:: { borrow:: Borrow , cmp, fmt, hash, ops:: Deref } ;
15+ use std:: { borrow:: Borrow , cmp, fmt, hash, ops:: Deref , sync :: Arc } ;
1616
1717use bytes:: Bytes ;
1818
19- use super :: linked:: PinLease ;
19+ use super :: { linked:: SlicePinState , v5:: V5PinState } ;
20+
21+ /// What holds the memory a [`ShmBuf`] borrows in place for as long as the borrow lives.
22+ ///
23+ /// A lease is never read: it is a drop guard, and it is the last one dropped that hands the
24+ /// memory back. What that means differs by buffer — a V4 slice goes to the buffer manager it
25+ /// was allocated from, a V5 block goes onto the peer's release queue — so each buffer keeps
26+ /// its own state behind its own `Drop` and this only says which.
27+ #[ allow(
28+ dead_code,
29+ reason = "holding the state is the whole of a lease's job; nothing ever reads it"
30+ ) ]
31+ pub ( crate ) enum PinLease {
32+ /// One slice of a V4 [`LinkedBuffer`](super::LinkedBuffer).
33+ Slice ( Arc < SlicePinState > ) ,
34+ /// One block of the peer's V5 segment.
35+ Block ( Arc < V5PinState > ) ,
36+ }
2037
2138pub struct ShmBuf < ' shm > {
2239 buf : & ' shm [ u8 ] ,
@@ -35,8 +52,10 @@ impl<'shm> ShmBuf<'shm> {
3552 }
3653
3754 fn into_bytes ( self ) -> Bytes {
38- // The lease keeps both the mapping and the retired BufferSlice alive until the final
39- // Bytes clone is dropped, so erasing the borrow lifetime here is sound.
55+ // The lease keeps the mapping alive, along with whatever inside it the bytes were read
56+ // out of — a retired BufferSlice, or a block of the peer's segment that is kept off
57+ // the release queue — until the final Bytes clone is dropped, so erasing the borrow
58+ // lifetime here is sound.
4059 let owner: ShmBuf < ' static > = unsafe { std:: mem:: transmute ( self ) } ;
4160 Bytes :: from_owner ( owner)
4261 }
0 commit comments