From 4c2c6575e5528aa4974ebc4dd8d0237e357e9594 Mon Sep 17 00:00:00 2001 From: Matt Keeter Date: Wed, 24 Jan 2024 11:29:23 -0500 Subject: [PATCH] Use static assertion for MAX_CHUNK_SIZE --- Cargo.lock | 1 + Cargo.toml | 1 + pantry/Cargo.toml | 1 + pantry/src/pantry.rs | 22 ++++++++++++++-------- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3588f2fe5..4c67fe55c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1064,6 +1064,7 @@ dependencies = [ "serde_json", "sha2", "slog", + "static_assertions", "subprocess", "tokio", "uuid", diff --git a/Cargo.toml b/Cargo.toml index aa658a82d..fcfdcbb8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -87,6 +87,7 @@ slog-async = { version = "2.8" } slog-bunyan = "2.4.0" slog-dtrace = "0.2" slog-term = { version = "2.9" } +static_assertions = "1.1.0" statistical = "1.0.0" subprocess = "0.2.9" tempfile = "3" diff --git a/pantry/Cargo.toml b/pantry/Cargo.toml index fd6b3167d..29ed49e2f 100644 --- a/pantry/Cargo.toml +++ b/pantry/Cargo.toml @@ -17,6 +17,7 @@ schemars.workspace = true serde.workspace = true serde_json.workspace = true slog.workspace = true +static_assertions.workspace = true crucible.workspace = true crucible-common.workspace = true crucible-smf.workspace = true diff --git a/pantry/src/pantry.rs b/pantry/src/pantry.rs index 60d638e8c..7e4801d67 100644 --- a/pantry/src/pantry.rs +++ b/pantry/src/pantry.rs @@ -87,6 +87,18 @@ where } } +// Static assertions to ensure that MAX_CHUNK_SIZE is divisible into blocks. +// +// Block size is always a power of two, so if we're divisible by the largest +// possible block, then we're also divisible by all others. +static_assertions::const_assert_eq!( + PantryEntry::MAX_CHUNK_SIZE % crucible::MAX_BLOCK_SIZE, + 0 +); +static_assertions::const_assert!( + PantryEntry::MAX_CHUNK_SIZE >= crucible::MAX_BLOCK_SIZE, +); + impl PantryEntry { pub const MAX_CHUNK_SIZE: usize = 512 * 1024; @@ -332,14 +344,8 @@ impl PantryEntry { block_size, ); } - if Self::MAX_CHUNK_SIZE % block_size as usize != 0 { - crucible_bail!( - InvalidNumberOfBlocks, - "max chunk size {} not divisible by block size {}!", - Self::MAX_CHUNK_SIZE, - block_size, - ); - } + // This is checked by static assertions above + assert_eq!(Self::MAX_CHUNK_SIZE % block_size as usize, 0); let mut data = crucible::Buffer::with_capacity( Self::MAX_CHUNK_SIZE / block_size as usize,