From a037770df6f7465d35e491c1ceacfe8d54c7b904 Mon Sep 17 00:00:00 2001 From: Brad Dunbar Date: Thu, 18 Jan 2024 08:20:09 -0500 Subject: [PATCH] BytesMut: Assert alignment of Shared Back in #362, an assertion was added to ensure that the alignment of bytes::Shared is even so we can use the least significant bit as a flag. bytes_mut::Shared uses the same technique but has no such assertion so I've added one here. --- src/bytes_mut.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/bytes_mut.rs b/src/bytes_mut.rs index 57fd33e4b..dd4ff5031 100644 --- a/src/bytes_mut.rs +++ b/src/bytes_mut.rs @@ -80,6 +80,12 @@ struct Shared { ref_count: AtomicUsize, } +// Assert that the alignment of `Shared` is divisible by 2. +// This is a necessary invariant since we depend on allocating `Shared` a +// shared object to implicitly carry the `KIND_ARC` flag in its pointer. +// This flag is set when the LSB is 0. +const _: [(); 0 - mem::align_of::() % 2] = []; // Assert that the alignment of `Shared` is divisible by 2. + // Buffer storage strategy flags. const KIND_ARC: usize = 0b0; const KIND_VEC: usize = 0b1;