Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a warning when testing without the allocator_api cargo feature #245

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion tests/all/allocator_api.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
#![cfg(feature = "allocator_api")]

use crate::quickcheck;
use crate::quickcheck::arbitrary_layout;
use bumpalo::Bump;
use std::alloc::{AllocError, Allocator, Layout};
use std::ptr::NonNull;
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};

/// Map an arbitrary `x` to a power of 2 that is less than or equal to `max`,
/// but with as little bias as possible (eg rounding `min(x, max)` to the
/// nearest power of 2 is unacceptable because it would majorly bias `max` for
/// small values of `max`).
fn clamp_to_pow2_in_range(x: usize, max: usize) -> usize {
let log_x = max.ilog2() as usize;
if log_x == 0 {
return 1;
}
let divisor = usize::MAX / log_x;
let y = 1_usize << (x / divisor);
assert!(y.is_power_of_two(), "{y} is not a power of two");
assert!(y <= max, "{y} is larger than {max}");
y
}

/// Helper to turn a pair of arbitrary `usize`s into a valid `Layout` of
/// reasonable size for use with quickchecks.
pub fn arbitrary_layout(size: usize, align: usize) -> Layout {
const MAX_ALIGN: usize = 64;
const MAX_SIZE: usize = 1024;

let align = clamp_to_pow2_in_range(align, MAX_ALIGN);

let size = size % (MAX_SIZE + 1);
let size = size.next_multiple_of(align);

Layout::from_size_align(size, align).unwrap()
}

#[derive(Debug)]
struct AllocatorDebug {
bump: Bump,
Expand Down
32 changes: 0 additions & 32 deletions tests/all/quickcheck.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::alloc::Layout;

/// A redefinition/wrapper macro of `quickcheck::quickcheck!` that supports
/// limiting the number of test iterations to one when we are running under
/// MIRI.
Expand Down Expand Up @@ -43,33 +41,3 @@ macro_rules! quickcheck {
)*
};
}

/// Map an arbitrary `x` to a power of 2 that is less than or equal to `max`,
/// but with as little bias as possible (eg rounding `min(x, max)` to the
/// nearest power of 2 is unacceptable because it would majorly bias `max` for
/// small values of `max`).
fn clamp_to_pow2_in_range(x: usize, max: usize) -> usize {
let log_x = max.ilog2() as usize;
if log_x == 0 {
return 1;
}
let divisor = usize::MAX / log_x;
let y = 1_usize << (x / divisor);
assert!(y.is_power_of_two(), "{y} is not a power of two");
assert!(y <= max, "{y} is larger than {max}");
y
}

/// Helper to turn a pair of arbitrary `usize`s into a valid `Layout` of
/// reasonable size for use with quickchecks.
pub fn arbitrary_layout(size: usize, align: usize) -> Layout {
const MAX_ALIGN: usize = 64;
const MAX_SIZE: usize = 1024;

let align = clamp_to_pow2_in_range(align, MAX_ALIGN);

let size = size % (MAX_SIZE + 1);
let size = size.next_multiple_of(align);

Layout::from_size_align(size, align).unwrap()
}