Skip to content

Commit

Permalink
Use static assertion for MAX_CHUNK_SIZE
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeeter committed Jan 24, 2024
1 parent 8831101 commit 4c2c657
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions pantry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 14 additions & 8 deletions pantry/src/pantry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 4c2c657

Please sign in to comment.