Skip to content

Commit

Permalink
Removing allocator-api support
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulstrackx committed Nov 5, 2020
1 parent b99a307 commit 4786050
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 69 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,3 @@ jobs:
run: rustup update stable && rustup default stable && rustup target add wasm32-unknown-unknown
- run: cargo build --target wasm32-unknown-unknown
- run: cargo build --target wasm32-unknown-unknown --release

alloc_api:
name: Allocator API
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Install Rust
run: rustup update nightly && rustup default nightly
- run: cargo test --features 'allocator-api global'

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,4 @@ global = []
# Enable very expensive debug checks in this crate
debug = []

# Enable experimental support for the standard library's unstable allocator API.
allocator-api = []
rustc-dep-of-std = ['core', 'compiler_builtins/rustc-dep-of-std']
22 changes: 0 additions & 22 deletions src/global.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#[cfg(feature = "allocator-api")]
use core::alloc::{AllocErr, AllocRef};
use core::alloc::{GlobalAlloc, Layout};
use core::ops::{Deref, DerefMut};
#[cfg(feature = "allocator-api")]
use core::ptr::NonNull;

use Dlmalloc;

Expand Down Expand Up @@ -35,24 +31,6 @@ unsafe impl GlobalAlloc for GlobalDlmalloc {
}
}

#[cfg(feature = "allocator-api")]
unsafe impl AllocRef for GlobalDlmalloc {
#[inline]
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
unsafe { get().alloc(layout) }
}

#[inline]
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
get().dealloc(ptr, layout)
}

#[inline]
fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
unsafe { get().alloc_zeroed(layout) }
}
}

static mut DLMALLOC: Dlmalloc = Dlmalloc::new();

struct Instance;
Expand Down
35 changes: 3 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@
//! Support for other platforms is largely untested and unused, but is used when
//! testing this crate.

#![cfg_attr(feature = "allocator-api", feature(allocator_api))]
#![cfg_attr(not(feature = "allocator-api"), allow(dead_code))]
#![allow(dead_code)]
#![no_std]
#![deny(missing_docs)]

#[cfg(feature = "allocator-api")]
use core::alloc::{AllocErr, AllocRef, Layout};
use core::cmp;
use core::ptr;
use sys::System;

#[cfg(all(feature = "global", not(test)))]
#[cfg(feature = "global")]
pub use self::global::GlobalDlmalloc;

mod dlmalloc;
#[cfg(all(feature = "global", not(test)))]
#[cfg(feature = "global")]
mod global;

/// In order for this crate to efficiently manage memory, it needs a way to communicate with the
Expand Down Expand Up @@ -174,29 +171,3 @@ impl<A: Allocator> Dlmalloc<A> {
}
}
}

#[cfg(feature = "allocator-api")]
unsafe impl AllocRef for Dlmalloc {
#[inline]
fn alloc(&mut self, layout: Layout) -> Result<ptr::NonNull<[u8]>, AllocErr> {
unsafe {
let ptr = <Dlmalloc>::malloc(self, layout.size(), layout.align());
let ptr = ptr::slice_from_raw_parts(ptr, layout.size());
ptr::NonNull::new(ptr as _).ok_or(AllocErr)
}
}

#[inline]
unsafe fn dealloc(&mut self, ptr: ptr::NonNull<u8>, layout: Layout) {
<Dlmalloc>::free(self, ptr.as_ptr(), layout.size(), layout.align())
}

#[inline]
fn alloc_zeroed(&mut self, layout: Layout) -> Result<ptr::NonNull<[u8]>, AllocErr> {
unsafe {
let ptr = <Dlmalloc>::calloc(self, layout.size(), layout.align());
let ptr = ptr::slice_from_raw_parts(ptr, layout.size());
ptr::NonNull::new(ptr as _).ok_or(AllocErr)
}
}
}
4 changes: 1 addition & 3 deletions tests/global.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#![cfg(all(feature = "allocator-api", feature = "global"))]
#![feature(global_allocator)]

extern crate dlmalloc;

use std::collections::HashMap;
use std::thread;

#[global_allocator]
#[cfg(feature = "global")]
static A: dlmalloc::GlobalDlmalloc = dlmalloc::GlobalDlmalloc;

#[test]
Expand Down

0 comments on commit 4786050

Please sign in to comment.