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 the rust allocator when std is not used #67

Merged
merged 2 commits into from
Dec 11, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ jobs:
run: cargo rustc --target ${{matrix.target}} -p libbz2-rs-sys --lib --no-default-features --crate-type rlib
env:
RUSTFLAGS: -Aunused_variables -Aunused_assignments
- name: cargo build (no_std + rust_allocator)
run: cargo rustc --target ${{matrix.target}} -p libbz2-rs-sys --lib --no-default-features --features rust-allocator --crate-type rlib
env:
RUSTFLAGS: -Aunused_variables -Aunused_assignments
- name: cargo nextest # reports segfaults in a helpful way
run: cargo nextest run --target ${{matrix.target}} ${{ matrix.features }} --no-fail-fast --workspace
env:
Expand Down
8 changes: 6 additions & 2 deletions libbz2-rs-sys/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
//! the layout of an allocation to deallocate it, and C interfaces don't usually provide this
//! information. Luckily in the library we know in all cases how big the allocation was at the
//! point where we deallocate it.

#[cfg(feature = "rust-allocator")]
extern crate alloc;

use core::ffi::{c_int, c_void};

use crate::bzlib::{BzStream, StreamState};
Expand Down Expand Up @@ -148,7 +152,7 @@ impl Allocator {
#[cfg(feature = "rust-allocator")]
Allocator::Rust => {
let layout = core::alloc::Layout::array::<T>(count).unwrap();
let ptr = unsafe { std::alloc::alloc_zeroed(layout) };
let ptr = unsafe { alloc::alloc::alloc_zeroed(layout) };
(!ptr.is_null()).then_some(ptr.cast())
}
#[cfg(feature = "c-allocator")]
Expand Down Expand Up @@ -182,7 +186,7 @@ impl Allocator {
#[cfg(feature = "rust-allocator")]
Allocator::Rust => {
let layout = core::alloc::Layout::array::<T>(count).unwrap();
unsafe { std::alloc::dealloc(ptr.cast(), layout) }
unsafe { alloc::alloc::dealloc(ptr.cast(), layout) }
}
#[cfg(feature = "c-allocator")]
Allocator::C => {
Expand Down
Loading