Skip to content

Commit

Permalink
Added default type parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulstrackx committed Oct 15, 2020
1 parent 9e9cd15 commit 63ba62b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
17 changes: 9 additions & 8 deletions src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use core::alloc::{GlobalAlloc, Layout};
use core::ops::{Deref, DerefMut};
#[cfg(feature = "allocator-api")]
use core::ptr::NonNull;
use DLMALLOC_INIT;

use Dlmalloc;

Expand All @@ -17,22 +18,22 @@ pub struct GlobalDlmalloc;
unsafe impl GlobalAlloc for GlobalDlmalloc {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
<Dlmalloc<Platform>>::malloc(&mut get(), layout.size(), layout.align())
<Dlmalloc>::malloc(&mut get(), layout.size(), layout.align())
}

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

#[inline]
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
<Dlmalloc<Platform>>::calloc(&mut get(), layout.size(), layout.align())
<Dlmalloc>::calloc(&mut get(), layout.size(), layout.align())
}

#[inline]
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
<Dlmalloc<Platform>>::realloc(&mut get(), ptr, layout.size(), layout.align(), new_size)
<Dlmalloc>::realloc(&mut get(), ptr, layout.size(), layout.align(), new_size)
}
}

Expand Down Expand Up @@ -64,7 +65,7 @@ unsafe impl Alloc for GlobalDlmalloc {
}
}

static mut DLMALLOC: Dlmalloc<Platform> = Dlmalloc::new();
static mut DLMALLOC: Dlmalloc = DLMALLOC_INIT;

struct Instance;

Expand All @@ -74,14 +75,14 @@ unsafe fn get() -> Instance {
}

impl Deref for Instance {
type Target = Dlmalloc<Platform>;
fn deref(&self) -> &Dlmalloc<Platform> {
type Target = Dlmalloc;
fn deref(&self) -> &Dlmalloc {
unsafe { &DLMALLOC }
}
}

impl DerefMut for Instance {
fn deref_mut(&mut self) -> &mut Dlmalloc<Platform> {
fn deref_mut(&mut self) -> &mut Dlmalloc {
unsafe { &mut DLMALLOC }
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use core::alloc::{Alloc, AllocErr, Layout};
use core::cmp;
use core::ptr;
#[cfg(any(target_os = "linux", target_os = "macos", target_arch = "wasm32"))]
pub use sys::Platform;
use sys::Platform;

#[cfg(all(feature = "global", not(test)))]
pub use self::global::GlobalDlmalloc;
Expand Down Expand Up @@ -69,6 +69,15 @@ pub trait GlobalSystem: System {
/// Instances of this type are used to allocate blocks of memory. For best
/// results only use one of these. Currently doesn't implement `Drop` to release
/// lingering memory back to the OS. That may happen eventually though!
#[cfg(any(target_os = "linux", target_arch = "wasm32", target_os = "macos"))]
pub struct Dlmalloc<S = Platform>(dlmalloc::Dlmalloc<S>);

/// An allocator instance
///
/// Instances of this type are used to allocate blocks of memory. For best
/// results only use one of these. Currently doesn't implement `Drop` to release
/// lingering memory back to the OS. That may happen eventually though!
#[cfg(not(any(target_os = "linux", target_arch = "wasm32", target_os = "macos")))]
pub struct Dlmalloc<S>(dlmalloc::Dlmalloc<S>);

/// Constant initializer for `Dlmalloc` structure.
Expand Down
6 changes: 3 additions & 3 deletions tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate dlmalloc;
extern crate rand;

#[cfg(any(target_os = "linux", target_arch = "wasm32", target_os = "macos"))]
use dlmalloc::{Dlmalloc, Platform};
use dlmalloc::Dlmalloc;
#[cfg(any(target_os = "linux", target_arch = "wasm32", target_os = "macos"))]
use rand::Rng;
#[cfg(any(target_os = "linux", target_arch = "wasm32", target_os = "macos"))]
Expand All @@ -11,7 +11,7 @@ use std::cmp;
#[test]
#[cfg(any(target_os = "linux", target_arch = "wasm32", target_os = "macos"))]
fn smoke() {
let mut a: Dlmalloc<Platform> = Dlmalloc::new();
let mut a: Dlmalloc = Dlmalloc::new();
unsafe {
let ptr = a.malloc(1, 1);
assert!(!ptr.is_null());
Expand All @@ -30,7 +30,7 @@ fn smoke() {
#[test]
#[cfg(any(target_os = "linux", target_arch = "wasm32", target_os = "macos"))]
fn stress() {
let mut a: Dlmalloc<Platform> = Dlmalloc::new();
let mut a: Dlmalloc = Dlmalloc::new();
let mut rng = rand::thread_rng();
let mut ptrs = Vec::new();
let max = if cfg!(test_lots) { 1_000_000 } else { 1_000 };
Expand Down

0 comments on commit 63ba62b

Please sign in to comment.