这是indexloc提供的服务,不要输入任何密码
Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/inflate_chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ fn deflate_ng(data: &[u8], window_bits: i32) -> Vec<u8> {
total_out: 0,
msg: std::ptr::null_mut(),
state: std::ptr::null_mut(),
zalloc: ::zlib_rs::allocate::Allocator::C.zalloc,
zfree: ::zlib_rs::allocate::Allocator::C.zfree,
zalloc: ::zlib_rs::allocate::C.zalloc,
zfree: ::zlib_rs::allocate::C.zfree,
opaque: std::ptr::null_mut(),
data_type: 0,
adler: 0,
Expand Down
5 changes: 2 additions & 3 deletions libz-rs-sys-cdylib/src/gz.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use zlib_rs::allocate::*;
pub use zlib_rs::c_api::*;

use crate::gz::GzMode::GZ_READ;
Expand Down Expand Up @@ -174,11 +173,11 @@ enum How {
const GZBUFSIZE: usize = 128 * 1024;

#[cfg(feature = "rust-allocator")]
const ALLOCATOR: &Allocator = &Allocator::RUST;
use zlib_rs::allocate::RUST as ALLOCATOR;

#[cfg(not(feature = "rust-allocator"))]
#[cfg(feature = "c-allocator")]
const ALLOCATOR: &Allocator = &Allocator::C;
use zlib_rs::allocate::C as ALLOCATOR;

#[cfg(not(feature = "rust-allocator"))]
#[cfg(not(feature = "c-allocator"))]
Expand Down
8 changes: 4 additions & 4 deletions test-libz-rs-sys/examples/blogpost-compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ fn compress_rs(
total_out: 0,
msg: std::ptr::null_mut(),
state: std::ptr::null_mut(),
zalloc: Some(zlib_rs::allocate::Allocator::C.zalloc),
zfree: Some(zlib_rs::allocate::Allocator::C.zfree),
zalloc: Some(zlib_rs::allocate::C.zalloc),
zfree: Some(zlib_rs::allocate::C.zfree),
opaque: std::ptr::null_mut(),
data_type: 0,
adler: 0,
Expand Down Expand Up @@ -139,8 +139,8 @@ fn compress_ng(
total_out: 0,
msg: std::ptr::null_mut(),
state: std::ptr::null_mut(),
zalloc: zlib_rs::allocate::Allocator::C.zalloc,
zfree: zlib_rs::allocate::Allocator::C.zfree,
zalloc: zlib_rs::allocate::C.zalloc,
zfree: zlib_rs::allocate::C.zfree,
opaque: std::ptr::null_mut(),
data_type: 0,
adler: 0,
Expand Down
8 changes: 4 additions & 4 deletions test-libz-rs-sys/examples/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ fn compress_rs(
total_out: 0,
msg: std::ptr::null_mut(),
state: std::ptr::null_mut(),
zalloc: Some(zlib_rs::allocate::Allocator::C.zalloc),
zfree: Some(zlib_rs::allocate::Allocator::C.zfree),
zalloc: Some(zlib_rs::allocate::C.zalloc),
zfree: Some(zlib_rs::allocate::C.zfree),
opaque: std::ptr::null_mut(),
data_type: 0,
adler: 0,
Expand Down Expand Up @@ -213,8 +213,8 @@ fn compress_ng(
total_out: 0,
msg: std::ptr::null_mut(),
state: std::ptr::null_mut(),
zalloc: zlib_rs::allocate::Allocator::C.zalloc,
zfree: zlib_rs::allocate::Allocator::C.zfree,
zalloc: zlib_rs::allocate::C.zalloc,
zfree: zlib_rs::allocate::C.zfree,
opaque: std::ptr::null_mut(),
data_type: 0,
adler: 0,
Expand Down
8 changes: 4 additions & 4 deletions test-libz-rs-sys/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub fn compress_slice_with_flush_ng<'a>(
total_out: 0,
msg: core::ptr::null_mut(),
state: core::ptr::null_mut(),
zalloc: zlib_rs::allocate::Allocator::C.zalloc,
zfree: zlib_rs::allocate::Allocator::C.zfree,
zalloc: zlib_rs::allocate::C.zalloc,
zfree: zlib_rs::allocate::C.zfree,
opaque: core::ptr::null_mut(),
data_type: 0,
adler: 0,
Expand Down Expand Up @@ -105,8 +105,8 @@ pub fn uncompress_slice_ng<'a>(
total_out: 0,
msg: std::ptr::null_mut(),
state: std::ptr::null_mut(),
zalloc: zlib_rs::allocate::Allocator::C.zalloc,
zfree: zlib_rs::allocate::Allocator::C.zfree,
zalloc: zlib_rs::allocate::C.zalloc,
zfree: zlib_rs::allocate::C.zfree,
opaque: std::ptr::null_mut(),
data_type: 0,
adler: 0,
Expand Down
12 changes: 6 additions & 6 deletions test-libz-rs-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,12 +1121,12 @@ mod null {

core::ptr::write(
core::ptr::addr_of_mut!((*strm.as_mut_ptr()).zalloc).cast(),
zlib_rs::allocate::Allocator::C.zalloc,
zlib_rs::allocate::C.zalloc,
);

core::ptr::write(
core::ptr::addr_of_mut!((*strm.as_mut_ptr()).zfree).cast(),
zlib_rs::allocate::Allocator::C.zfree,
zlib_rs::allocate::C.zfree,
);

core::ptr::write(
Expand Down Expand Up @@ -1158,12 +1158,12 @@ mod null {

core::ptr::write(
core::ptr::addr_of_mut!((*strm.as_mut_ptr()).zalloc).cast(),
zlib_rs::allocate::Allocator::C.zalloc,
zlib_rs::allocate::C.zalloc,
);

core::ptr::write(
core::ptr::addr_of_mut!((*strm.as_mut_ptr()).zfree).cast(),
zlib_rs::allocate::Allocator::C.zfree,
zlib_rs::allocate::C.zfree,
);

core::ptr::write(
Expand Down Expand Up @@ -1294,12 +1294,12 @@ mod coverage {

core::ptr::write(
core::ptr::addr_of_mut!((*strm.as_mut_ptr()).zalloc).cast(),
zlib_rs::allocate::Allocator::C.zalloc,
zlib_rs::allocate::C.zalloc,
);

core::ptr::write(
core::ptr::addr_of_mut!((*strm.as_mut_ptr()).zfree).cast(),
zlib_rs::allocate::Allocator::C.zfree,
zlib_rs::allocate::C.zfree,
);

let err = deflateInit2_(
Expand Down
84 changes: 42 additions & 42 deletions zlib-rs/src/allocate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type size_t = usize;

const ALIGN: u8 = 64;
// posix_memalign requires that the alignment be a power of two and a multiple of sizeof(void*).
const _: () = assert!(crate::allocate::ALIGN.count_ones() == 1);
const _: () = assert!(crate::allocate::ALIGN as usize % mem::size_of::<*mut c_void>() == 0);
const _: () = assert!(ALIGN.count_ones() == 1);
const _: () = assert!(ALIGN as usize % mem::size_of::<*mut c_void>() == 0);

/// # Safety
///
Expand Down Expand Up @@ -162,40 +162,40 @@ pub struct Allocator<'a> {
pub _marker: PhantomData<&'a ()>,
}

impl Allocator<'static> {
#[cfg(feature = "rust-allocator")]
pub const RUST: Self = Self {
zalloc: zalloc_rust,
zfree: zfree_rust,
opaque: core::ptr::null_mut(),
_marker: PhantomData,
};

#[cfg(feature = "c-allocator")]
pub const C: Self = Self {
zalloc: zalloc_c,
zfree: zfree_c,
opaque: core::ptr::null_mut(),
_marker: PhantomData,
};

#[cfg(test)]
const FAIL: Self = Self {
zalloc: zalloc_fail,
zfree: zfree_fail,
opaque: core::ptr::null_mut(),
_marker: PhantomData,
};
}
unsafe impl Sync for Allocator<'static> {}

#[cfg(feature = "rust-allocator")]
pub static RUST: Allocator<'static> = Allocator {
zalloc: zalloc_rust,
zfree: zfree_rust,
opaque: core::ptr::null_mut(),
_marker: PhantomData,
};

#[cfg(feature = "c-allocator")]
pub static C: Allocator<'static> = Allocator {
zalloc: zalloc_c,
zfree: zfree_c,
opaque: core::ptr::null_mut(),
_marker: PhantomData,
};

#[cfg(test)]
static FAIL: Allocator<'static> = Allocator {
zalloc: zalloc_fail,
zfree: zfree_fail,
opaque: core::ptr::null_mut(),
_marker: PhantomData,
};

impl Allocator<'_> {
fn allocate_layout(&self, layout: Layout) -> *mut c_void {
assert!(layout.align() <= ALIGN.into());

// Special case for the Rust `alloc` backed allocator
#[cfg(feature = "rust-allocator")]
if self.zalloc == Allocator::RUST.zalloc {
let ptr = unsafe { (Allocator::RUST.zalloc)(self.opaque, layout.size() as _, 1) };
if self.zalloc == RUST.zalloc {
let ptr = unsafe { (RUST.zalloc)(self.opaque, layout.size() as _, 1) };

debug_assert_eq!(ptr as usize % layout.align(), 0);

Expand Down Expand Up @@ -279,7 +279,7 @@ impl Allocator<'_> {
assert!(layout.align() <= ALIGN.into());

#[cfg(feature = "rust-allocator")]
if self.zalloc == Allocator::RUST.zalloc {
if self.zalloc == RUST.zalloc {
let ptr = unsafe { zalloc_rust_calloc(self.opaque, layout.size() as _, 1) };

debug_assert_eq!(ptr as usize % layout.align(), 0);
Expand All @@ -288,7 +288,7 @@ impl Allocator<'_> {
}

#[cfg(feature = "c-allocator")]
if self.zalloc == Allocator::C.zalloc {
if self.zalloc == C.zalloc {
let alloc = Allocator {
zalloc: zalloc_c_calloc,
zfree: zfree_c,
Expand Down Expand Up @@ -340,10 +340,10 @@ impl Allocator<'_> {
if !ptr.is_null() {
// Special case for the Rust `alloc` backed allocator
#[cfg(feature = "rust-allocator")]
if self.zfree == Allocator::RUST.zfree {
if self.zfree == RUST.zfree {
assert_ne!(len, 0, "invalid size for {ptr:?}");
let mut size = core::mem::size_of::<T>() * len;
return (Allocator::RUST.zfree)(&mut size as *mut usize as *mut c_void, ptr.cast());
return (RUST.zfree)(&mut size as *mut usize as *mut c_void, ptr.cast());
}

// General case for c-style allocation
Expand Down Expand Up @@ -460,12 +460,12 @@ mod tests {
#[test]
fn test_allocate_zeroed() {
#[cfg(feature = "rust-allocator")]
test_allocate_zeroed_help(Allocator::RUST);
test_allocate_zeroed_help(RUST);

#[cfg(feature = "c-allocator")]
test_allocate_zeroed_help(Allocator::C);
test_allocate_zeroed_help(C);

assert!(Allocator::FAIL.allocate_raw::<u128>().is_none());
assert!(FAIL.allocate_raw::<u128>().is_none());
}

fn test_allocate_zeroed_buffer_help(allocator: Allocator) {
Expand All @@ -484,24 +484,24 @@ mod tests {
#[test]
fn test_allocate_buffer_zeroed() {
#[cfg(feature = "rust-allocator")]
test_allocate_zeroed_buffer_help(Allocator::RUST);
test_allocate_zeroed_buffer_help(RUST);

#[cfg(feature = "c-allocator")]
test_allocate_zeroed_buffer_help(Allocator::C);
test_allocate_zeroed_buffer_help(C);

test_allocate_zeroed_buffer_help(Allocator::FAIL);
test_allocate_zeroed_buffer_help(FAIL);
}

#[test]
fn test_deallocate_null() {
unsafe {
#[cfg(feature = "rust-allocator")]
(Allocator::RUST.zfree)(core::ptr::null_mut(), core::ptr::null_mut());
(RUST.zfree)(core::ptr::null_mut(), core::ptr::null_mut());

#[cfg(feature = "c-allocator")]
(Allocator::C.zfree)(core::ptr::null_mut(), core::ptr::null_mut());
(C.zfree)(core::ptr::null_mut(), core::ptr::null_mut());

(Allocator::FAIL.zfree)(core::ptr::null_mut(), core::ptr::null_mut());
(FAIL.zfree)(core::ptr::null_mut(), core::ptr::null_mut());
}
}
}
4 changes: 2 additions & 2 deletions zlib-rs/src/c_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ impl z_stream {

#[cfg(feature = "rust-allocator")]
pub fn configure_default_rust_allocator(&mut self) {
self.configure_allocator(Allocator::RUST)
self.configure_allocator(crate::allocate::RUST)
}

#[cfg(feature = "c-allocator")]
pub fn configure_default_c_allocator(&mut self) {
self.configure_allocator(Allocator::C)
self.configure_allocator(crate::allocate::C)
}
}

Expand Down
14 changes: 7 additions & 7 deletions zlib-rs/src/deflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3319,7 +3319,7 @@ mod test {
// must use the C allocator internally because (de)allocation is based on function
// pointer values and because we don't use the rust allocator directly, the allocation
// logic will store the pointer to the start at the start of the allocation.
unsafe { (crate::allocate::Allocator::C.zalloc)(opaque, items, size) }
unsafe { (crate::allocate::C.zalloc)(opaque, items, size) }
} else {
core::ptr::null_mut()
}
Expand All @@ -3331,7 +3331,7 @@ mod test {
let atomic = AtomicUsize::new(0);
let mut stream = z_stream {
zalloc: Some(fail_nth_allocation::<0>),
zfree: Some(crate::allocate::Allocator::C.zfree),
zfree: Some(crate::allocate::C.zfree),
opaque: &atomic as *const _ as *const core::ffi::c_void as *mut _,
..z_stream::default()
};
Expand All @@ -3345,7 +3345,7 @@ mod test {
let atomic = AtomicUsize::new(0);
let mut stream = z_stream {
zalloc: Some(fail_nth_allocation::<3>),
zfree: Some(crate::allocate::Allocator::C.zfree),
zfree: Some(crate::allocate::C.zfree),
opaque: &atomic as *const _ as *const core::ffi::c_void as *mut _,
..z_stream::default()
};
Expand All @@ -3359,7 +3359,7 @@ mod test {
let atomic = AtomicUsize::new(0);
let mut stream = z_stream {
zalloc: Some(fail_nth_allocation::<5>),
zfree: Some(crate::allocate::Allocator::C.zfree),
zfree: Some(crate::allocate::C.zfree),
opaque: &atomic as *const _ as *const core::ffi::c_void as *mut _,
..z_stream::default()
};
Expand All @@ -3380,7 +3380,7 @@ mod test {
let atomic = AtomicUsize::new(0);
stream.opaque = &atomic as *const _ as *const core::ffi::c_void as *mut _;
stream.zalloc = Some(fail_nth_allocation::<6>);
stream.zfree = Some(crate::allocate::Allocator::C.zfree);
stream.zfree = Some(crate::allocate::C.zfree);

// init performs 6 allocations; we don't want those to fail
assert_eq!(init(&mut stream, DeflateConfig::default()), ReturnCode::Ok);
Expand All @@ -3402,7 +3402,7 @@ mod test {

let atomic = AtomicUsize::new(0);
stream.zalloc = Some(fail_nth_allocation::<{ 6 + 3 }>);
stream.zfree = Some(crate::allocate::Allocator::C.zfree);
stream.zfree = Some(crate::allocate::C.zfree);
stream.opaque = &atomic as *const _ as *const core::ffi::c_void as *mut _;

// init performs 6 allocations; we don't want those to fail
Expand All @@ -3425,7 +3425,7 @@ mod test {

let atomic = AtomicUsize::new(0);
stream.zalloc = Some(fail_nth_allocation::<{ 6 + 5 }>);
stream.zfree = Some(crate::allocate::Allocator::C.zfree);
stream.zfree = Some(crate::allocate::C.zfree);
stream.opaque = &atomic as *const _ as *const core::ffi::c_void as *mut _;

// init performs 6 allocations; we don't want those to fail
Expand Down
Loading
Loading