diff --git a/src/dlmalloc.rs b/src/dlmalloc.rs index 94a7c8c..d16f523 100644 --- a/src/dlmalloc.rs +++ b/src/dlmalloc.rs @@ -4,13 +4,12 @@ // The original source was written by Doug Lea and released to the public domain use core::cmp; -use core::marker::PhantomData; use core::mem; use core::ptr; -use System; +use Allocator; -pub struct Dlmalloc { +pub struct Dlmalloc { smallmap: u32, treemap: u32, smallbins: [*mut Chunk; (NSMALLBINS + 1) * 2], @@ -25,9 +24,9 @@ pub struct Dlmalloc { trim_check: usize, least_addr: *mut u8, release_checks: usize, - _system_allocator: PhantomData, + system_allocator: A, } -unsafe impl Send for Dlmalloc {} +unsafe impl Send for Dlmalloc {} // TODO: document this const NSMALLBINS: usize = 32; @@ -87,8 +86,8 @@ fn leftshift_for_tree_index(x: u32) -> u32 { } } -impl Dlmalloc { - pub const fn init() -> Dlmalloc { +impl Dlmalloc { + pub const fn new(system_allocator: A) -> Dlmalloc { Dlmalloc { smallmap: 0, treemap: 0, @@ -109,12 +108,12 @@ impl Dlmalloc { trim_check: 0, least_addr: 0 as *mut _, release_checks: 0, - _system_allocator: PhantomData, + system_allocator, } } } -impl Dlmalloc { +impl Dlmalloc { // TODO: can we get rid of this? pub fn malloc_alignment(&self) -> usize { mem::size_of::() * 2 @@ -231,7 +230,7 @@ impl Dlmalloc { } pub unsafe fn calloc_must_clear(&self, ptr: *mut u8) -> bool { - !Sys::allocates_zeros() || !Chunk::mmapped(Chunk::from_mem(ptr)) + !self.system_allocator.allocates_zeros() || !Chunk::mmapped(Chunk::from_mem(ptr)) } pub unsafe fn malloc(&mut self, size: usize) -> *mut u8 { @@ -359,7 +358,7 @@ impl Dlmalloc { DEFAULT_GRANULARITY, ); - let (tbase, tsize, flags) = Sys::alloc(asize); + let (tbase, tsize, flags) = self.system_allocator.alloc(asize); if tbase.is_null() { return tbase; } @@ -540,7 +539,7 @@ impl Dlmalloc { let oldmmsize = oldsize + offset + self.mmap_foot_pad(); let newmmsize = self.mmap_align(nb + 6 * mem::size_of::() + self.malloc_alignment() - 1); - let ptr = Sys::remap( + let ptr = self.system_allocator.remap( (oldp as *mut u8).offset(-(offset as isize)), oldmmsize, newmmsize, @@ -562,7 +561,7 @@ impl Dlmalloc { } fn mmap_align(&self, a: usize) -> usize { - align_up(a, Sys::page_size()) + align_up(a, self.system_allocator.page_size()) } // Only call this with power-of-two alignment and alignment > @@ -638,7 +637,10 @@ impl Dlmalloc { let prevsize = (*p).prev_foot; if Chunk::mmapped(p) { psize += prevsize + self.mmap_foot_pad(); - if Sys::free((p as *mut u8).offset(-(prevsize as isize)), psize) { + if self + .system_allocator + .free((p as *mut u8).offset(-(prevsize as isize)), psize) + { self.footprint -= psize; } return; @@ -1168,7 +1170,10 @@ impl Dlmalloc { if Chunk::mmapped(p) { psize += prevsize + self.mmap_foot_pad(); - if Sys::free((p as *mut u8).offset(-(prevsize as isize)), psize) { + if self + .system_allocator + .free((p as *mut u8).offset(-(prevsize as isize)), psize) + { self.footprint -= psize; } return; @@ -1249,10 +1254,13 @@ impl Dlmalloc { debug_assert!(!sp.is_null()); if !Segment::is_extern(sp) { - if Segment::can_release_part::(sp) { + if Segment::can_release_part(&self.system_allocator, sp) { if (*sp).size >= extra && !self.has_segment_link(sp) { let newsize = (*sp).size - extra; - if Sys::free_part((*sp).base, (*sp).size, newsize) { + if self + .system_allocator + .free_part((*sp).base, (*sp).size, newsize) + { released = extra; } } @@ -1302,7 +1310,7 @@ impl Dlmalloc { let next = (*sp).next; nsegs += 1; - if Segment::can_release_part::(sp) && !Segment::is_extern(sp) { + if Segment::can_release_part(&self.system_allocator, sp) && !Segment::is_extern(sp) { let p = self.align_as_chunk(base); let psize = Chunk::size(p); // We can unmap if the first chunk holds the entire segment and @@ -1318,7 +1326,7 @@ impl Dlmalloc { } else { self.unlink_large_chunk(tp); } - if Sys::free(base, size) { + if self.system_allocator.free(base, size) { released += size; self.footprint -= size; // unlink our obsolete record @@ -1412,7 +1420,7 @@ impl Dlmalloc { ); debug_assert!(p as *mut u8 >= self.least_addr); debug_assert!(!self.is_small(sz)); - debug_assert_eq!(align_up(len, Sys::page_size()), len); + debug_assert_eq!(align_up(len, self.system_allocator.page_size()), len); debug_assert_eq!((*Chunk::plus_offset(p, sz)).head, Chunk::fencepost_head()); debug_assert_eq!( (*Chunk::plus_offset(p, sz + mem::size_of::())).head, @@ -1753,8 +1761,8 @@ impl Segment { (*seg).flags & EXTERN != 0 } - unsafe fn can_release_part(seg: *mut Segment) -> bool { - S::can_release_part((*seg).flags >> 1) + unsafe fn can_release_part(system_allocator: &A, seg: *mut Segment) -> bool { + system_allocator.can_release_part((*seg).flags >> 1) } unsafe fn sys_flags(seg: *mut Segment) -> u32 { @@ -1773,12 +1781,11 @@ impl Segment { #[cfg(test)] mod tests { use super::*; - #[cfg(any(target_os = "linux", target_arch = "wasm32", target_os = "macos"))] - use Platform; + use System; // Prime the allocator with some allocations such that there will be free // chunks in the treemap - unsafe fn setup_treemap(a: &mut Dlmalloc) { + unsafe fn setup_treemap(a: &mut Dlmalloc) { let large_request_size = NSMALLBINS * (1 << SMALLBIN_SHIFT); assert!(!a.is_small(large_request_size)); let large_request1 = a.malloc(large_request_size); @@ -1792,9 +1799,8 @@ mod tests { #[test] // Test allocating, with a non-empty treemap, a specific size that used to // trigger an integer overflow bug - #[cfg(any(target_os = "linux", target_arch = "wasm32", target_os = "macos"))] fn treemap_alloc_overflow_minimal() { - let mut a: Dlmalloc = Dlmalloc::init(); + let mut a = Dlmalloc::new(System::new()); unsafe { setup_treemap(&mut a); let min_idx31_size = (0xc000 << TREEBIN_SHIFT) - a.chunk_overhead() + 1; @@ -1803,10 +1809,9 @@ mod tests { } #[test] - #[cfg(any(target_os = "linux", target_arch = "wasm32", target_os = "macos"))] // Test allocating the maximum request size with a non-empty treemap fn treemap_alloc_max() { - let mut a: Dlmalloc = Dlmalloc::init(); + let mut a = Dlmalloc::new(System::new()); unsafe { setup_treemap(&mut a); let max_request_size = a.max_request() - 1; diff --git a/src/dummy.rs b/src/dummy.rs new file mode 100644 index 0000000..d6ca226 --- /dev/null +++ b/src/dummy.rs @@ -0,0 +1,42 @@ +use core::ptr; +use Allocator; + +pub struct System { + _priv: (), +} + +impl System { + const fn new() -> System { + System { _priv: () } + } +} + +unsafe impl Allocator for System { + fn alloc(&self, size: usize) -> (*mut u8, usize, u32) { + (ptr::null_mut(), 0, 0) + } + + fn remap(&self, ptr: *mut u8, oldsize: usize, newsize: usize, can_move: bool) -> *mut u8 { + ptr::null_mut() + } + + fn free_part(&self, ptr: *mut u8, oldsize: usize, newsize: usize) -> bool { + false + } + + fn free(&self, ptr: *mut u8, size: usize) -> bool { + false + } + + fn can_release_part(&self, flags: u32) -> bool { + false + } + + fn allocates_zeros(&self) -> bool { + false + } + + fn page_size(&self) -> usize { + 1 + } +} diff --git a/src/global.rs b/src/global.rs index 3bff8f3..bd4214b 100644 --- a/src/global.rs +++ b/src/global.rs @@ -1,11 +1,9 @@ -use crate::{GlobalSystem, Platform}; #[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_INIT; use Dlmalloc; @@ -55,12 +53,12 @@ unsafe impl AllocRef for GlobalDlmalloc { } } -static mut DLMALLOC: Dlmalloc = DLMALLOC_INIT; +static mut DLMALLOC: Dlmalloc = Dlmalloc::new(); struct Instance; unsafe fn get() -> Instance { - Platform::acquire_global_lock(); + ::sys::acquire_global_lock(); Instance } @@ -79,6 +77,6 @@ impl DerefMut for Instance { impl Drop for Instance { fn drop(&mut self) { - Platform::release_global_lock() + ::sys::release_global_lock() } } diff --git a/src/lib.rs b/src/lib.rs index 29c11cb..a743816 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,8 +20,7 @@ use core::alloc::{AllocErr, AllocRef, Layout}; use core::cmp; use core::ptr; -#[cfg(any(target_os = "linux", target_os = "macos", target_arch = "wasm32"))] -use sys::Platform; +use sys::System; #[cfg(all(feature = "global", not(test)))] pub use self::global::GlobalDlmalloc; @@ -30,59 +29,51 @@ mod dlmalloc; #[cfg(all(feature = "global", not(test)))] mod global; -/// A platform interface -pub trait System: Send { - /// Allocates a memory region of `size` bytes - unsafe fn alloc(size: usize) -> (*mut u8, usize, u32); - - /// Remaps a memory region - unsafe fn remap(ptr: *mut u8, oldsize: usize, newsize: usize, can_move: bool) -> *mut u8; - - /// Frees a part of a memory region - unsafe fn free_part(ptr: *mut u8, oldsize: usize, newsize: usize) -> bool; - - /// Frees an entire memory region - unsafe fn free(ptr: *mut u8, size: usize) -> bool; - - /// Indicates if the platform can release a part of memory - fn can_release_part(flags: u32) -> bool; - - /// Allocates a memory region of zeros - fn allocates_zeros() -> bool; - - /// Returns the page size - fn page_size() -> usize; +/// In order for this crate to efficiently manage memory, it needs a way to communicate with the +/// underlying platform. This `Allocator` trait provides an interface for this communication. +pub unsafe trait Allocator: Send { + /// Allocates system memory region of at least `size` bytes + /// Returns a triple of `(base, size, flags)` where `base` is a pointer to the beginning of the + /// allocated memory region. `size` is the actual size of the region while `flags` specifies + /// properties of the allocated region. If `EXTERN_BIT` (bit 0) set in flags, then we did not + /// allocate this segment and so should not try to deallocate or merge with others. + /// This function can return a `std::ptr::null_mut()` when allocation fails (other values of + /// the triple will be ignored). + fn alloc(&self, size: usize) -> (*mut u8, usize, u32); + + /// Remaps system memory region at `ptr` with size `oldsize` to a potential new location with + /// size `newsize`. `can_move` indicates if the location is allowed to move to a completely new + /// location, or that it is only allowed to change in size. Returns a pointer to the new + /// location in memory. + /// This function can return a `std::ptr::null_mut()` to signal an error. + fn remap(&self, ptr: *mut u8, oldsize: usize, newsize: usize, can_move: bool) -> *mut u8; + + /// Frees a part of a memory chunk. The original memory chunk starts at `ptr` with size `oldsize` + /// and is turned into a memory region starting at the same address but with `newsize` bytes. + /// Returns `true` iff the access memory region could be freed. + fn free_part(&self, ptr: *mut u8, oldsize: usize, newsize: usize) -> bool; + + /// Frees an entire memory region. Returns `true` iff the operation succeeded. When `false` is + /// returned, the `dlmalloc` may re-use the location on future allocation requests + fn free(&self, ptr: *mut u8, size: usize) -> bool; + + /// Indicates if the system can release a part of memory. For the `flags` argument, see + /// `Allocator::alloc` + fn can_release_part(&self, flags: u32) -> bool; + + /// Indicates whether newly allocated regions contain zeros. + fn allocates_zeros(&self) -> bool; + + /// Returns the page size. Must be a power of two + fn page_size(&self) -> usize; } -/// A platform interface for platforms that support the global lock -#[cfg(feature = "global")] -pub trait GlobalSystem: System { - /// Acquires the global lock - fn acquire_global_lock(); - - /// Releases the global lock - fn release_global_lock(); -} - -/// 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(any(target_os = "linux", target_arch = "wasm32", target_os = "macos"))] -pub struct Dlmalloc(dlmalloc::Dlmalloc); - /// 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(dlmalloc::Dlmalloc); - -/// Constant initializer for `Dlmalloc` structure. -#[cfg(any(target_os = "linux", target_os = "macos", target_arch = "wasm32"))] -pub const DLMALLOC_INIT: Dlmalloc = Dlmalloc::new(); +pub struct Dlmalloc(dlmalloc::Dlmalloc); #[cfg(target_arch = "wasm32")] #[path = "wasm.rs"] @@ -96,14 +87,25 @@ mod sys; #[path = "linux.rs"] mod sys; -impl Dlmalloc { +#[cfg(not(any(target_os = "linux", target_os = "macos", target_arch = "wasm32")))] +#[path = "dummy.rs"] +mod sys; + +impl Dlmalloc { + /// Creates a new instance of an allocator + pub const fn new() -> Dlmalloc { + Dlmalloc(dlmalloc::Dlmalloc::new(System::new())) + } +} + +impl Dlmalloc { /// Creates a new instance of an allocator - pub const fn new() -> Dlmalloc { - Dlmalloc(dlmalloc::Dlmalloc::init()) + pub const fn new_with_allocator(sys_allocator: A) -> Dlmalloc { + Dlmalloc(dlmalloc::Dlmalloc::new(sys_allocator)) } } -impl Dlmalloc { +impl Dlmalloc { /// Allocates `size` bytes with `align` align. /// /// Returns a null pointer if allocation fails. Returns a valid pointer diff --git a/src/linux.rs b/src/linux.rs index b432b69..42e6549 100644 --- a/src/linux.rs +++ b/src/linux.rs @@ -1,26 +1,34 @@ extern crate libc; use core::ptr; -#[cfg(feature = "global")] -use GlobalSystem; -use System; +use Allocator; /// System setting for Linux -pub struct Platform; +pub struct System { + _priv: (), +} + +impl System { + pub const fn new() -> System { + System { _priv: () } + } +} #[cfg(feature = "global")] static mut LOCK: libc::pthread_mutex_t = libc::PTHREAD_MUTEX_INITIALIZER; -impl System for Platform { - unsafe fn alloc(size: usize) -> (*mut u8, usize, u32) { - let addr = libc::mmap( - 0 as *mut _, - size, - libc::PROT_WRITE | libc::PROT_READ, - libc::MAP_ANONYMOUS | libc::MAP_PRIVATE, - -1, - 0, - ); +unsafe impl Allocator for System { + fn alloc(&self, size: usize) -> (*mut u8, usize, u32) { + let addr = unsafe { + libc::mmap( + 0 as *mut _, + size, + libc::PROT_WRITE | libc::PROT_READ, + libc::MAP_ANONYMOUS | libc::MAP_PRIVATE, + -1, + 0, + ) + }; if addr == libc::MAP_FAILED { (ptr::null_mut(), 0, 0) } else { @@ -28,9 +36,9 @@ impl System for Platform { } } - unsafe fn remap(ptr: *mut u8, oldsize: usize, newsize: usize, can_move: bool) -> *mut u8 { + fn remap(&self, ptr: *mut u8, oldsize: usize, newsize: usize, can_move: bool) -> *mut u8 { let flags = if can_move { libc::MREMAP_MAYMOVE } else { 0 }; - let ptr = libc::mremap(ptr as *mut _, oldsize, newsize, flags); + let ptr = unsafe { libc::mremap(ptr as *mut _, oldsize, newsize, flags) }; if ptr == libc::MAP_FAILED { ptr::null_mut() } else { @@ -38,38 +46,39 @@ impl System for Platform { } } - unsafe fn free_part(ptr: *mut u8, oldsize: usize, newsize: usize) -> bool { - let rc = libc::mremap(ptr as *mut _, oldsize, newsize, 0); - if rc != libc::MAP_FAILED { - return true; + fn free_part(&self, ptr: *mut u8, oldsize: usize, newsize: usize) -> bool { + unsafe { + let rc = libc::mremap(ptr as *mut _, oldsize, newsize, 0); + if rc != libc::MAP_FAILED { + return true; + } + libc::munmap(ptr.offset(newsize as isize) as *mut _, oldsize - newsize) == 0 } - libc::munmap(ptr.offset(newsize as isize) as *mut _, oldsize - newsize) == 0 } - unsafe fn free(ptr: *mut u8, size: usize) -> bool { - libc::munmap(ptr as *mut _, size) == 0 + fn free(&self, ptr: *mut u8, size: usize) -> bool { + unsafe { libc::munmap(ptr as *mut _, size) == 0 } } - fn can_release_part(_flags: u32) -> bool { + fn can_release_part(&self, _flags: u32) -> bool { true } - fn allocates_zeros() -> bool { + fn allocates_zeros(&self) -> bool { true } - fn page_size() -> usize { + fn page_size(&self) -> usize { 4096 } } #[cfg(feature = "global")] -impl GlobalSystem for Platform { - fn acquire_global_lock() { - unsafe { assert_eq!(libc::pthread_mutex_lock(&mut LOCK), 0) } - } +pub fn acquire_global_lock() { + unsafe { assert_eq!(libc::pthread_mutex_lock(&mut LOCK), 0) } +} - fn release_global_lock() { - unsafe { assert_eq!(libc::pthread_mutex_unlock(&mut LOCK), 0) } - } +#[cfg(feature = "global")] +pub fn release_global_lock() { + unsafe { assert_eq!(libc::pthread_mutex_unlock(&mut LOCK), 0) } } diff --git a/src/macos.rs b/src/macos.rs index abf2dd5..d131271 100644 --- a/src/macos.rs +++ b/src/macos.rs @@ -1,26 +1,34 @@ extern crate libc; use core::ptr; -#[cfg(feature = "global")] -use GlobalSystem; -use System; +use Allocator; /// System setting for MacOS -pub struct Platform; +pub struct System { + _priv: (), +} + +impl System { + pub const fn new() -> System { + System { _priv: () } + } +} #[cfg(feature = "global")] static mut LOCK: libc::pthread_mutex_t = libc::PTHREAD_MUTEX_INITIALIZER; -impl System for Platform { - unsafe fn alloc(size: usize) -> (*mut u8, usize, u32) { - let addr = libc::mmap( - 0 as *mut _, - size, - libc::PROT_WRITE | libc::PROT_READ, - libc::MAP_ANON | libc::MAP_PRIVATE, - -1, - 0, - ); +unsafe impl Allocator for System { + fn alloc(&self, size: usize) -> (*mut u8, usize, u32) { + let addr = unsafe { + libc::mmap( + 0 as *mut _, + size, + libc::PROT_WRITE | libc::PROT_READ, + libc::MAP_ANON | libc::MAP_PRIVATE, + -1, + 0, + ) + }; if addr == libc::MAP_FAILED { (ptr::null_mut(), 0, 0) } else { @@ -28,38 +36,37 @@ impl System for Platform { } } - unsafe fn remap(_ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool) -> *mut u8 { + fn remap(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool) -> *mut u8 { ptr::null_mut() } - unsafe fn free_part(ptr: *mut u8, oldsize: usize, newsize: usize) -> bool { - libc::munmap(ptr.offset(newsize as isize) as *mut _, oldsize - newsize) == 0 + fn free_part(&self, ptr: *mut u8, oldsize: usize, newsize: usize) -> bool { + unsafe { libc::munmap(ptr.offset(newsize as isize) as *mut _, oldsize - newsize) == 0 } } - unsafe fn free(ptr: *mut u8, size: usize) -> bool { - libc::munmap(ptr as *mut _, size) == 0 + fn free(&self, ptr: *mut u8, size: usize) -> bool { + unsafe { libc::munmap(ptr as *mut _, size) == 0 } } - fn can_release_part(_flags: u32) -> bool { + fn can_release_part(&self, _flags: u32) -> bool { true } - fn allocates_zeros() -> bool { + fn allocates_zeros(&self) -> bool { true } - fn page_size() -> usize { + fn page_size(&self) -> usize { 4096 } } #[cfg(feature = "global")] -impl GlobalSystem for Platform { - fn acquire_global_lock() { - unsafe { assert_eq!(libc::pthread_mutex_lock(&mut LOCK), 0) } - } +pub fn acquire_global_lock() { + unsafe { assert_eq!(libc::pthread_mutex_lock(&mut LOCK), 0) } +} - fn release_global_lock() { - unsafe { assert_eq!(libc::pthread_mutex_unlock(&mut LOCK), 0) } - } +#[cfg(feature = "global")] +pub fn release_global_lock() { + unsafe { assert_eq!(libc::pthread_mutex_unlock(&mut LOCK), 0) } } diff --git a/src/wasm.rs b/src/wasm.rs index 146d4b2..381fb02 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -1,59 +1,64 @@ use core::arch::wasm32; use core::ptr; -#[cfg(feature = "global")] -use GlobalSystem; -use System; +use Allocator; /// System setting for Wasm -pub struct Platform; +pub struct System { + _priv: (), +} + +impl System { + pub const fn new() -> System { + System { _priv: () } + } +} -impl System for Platform { - unsafe fn alloc(size: usize) -> (*mut u8, usize, u32) { - let pages = size / Self::page_size(); +unsafe impl Allocator for System { + fn alloc(&self, size: usize) -> (*mut u8, usize, u32) { + let pages = size / self.page_size(); let prev = wasm32::memory_grow(0, pages); if prev == usize::max_value() { return (ptr::null_mut(), 0, 0); } ( - (prev * Self::page_size()) as *mut u8, - pages * Self::page_size(), + (prev * self.page_size()) as *mut u8, + pages * self.page_size(), 0, ) } - unsafe fn remap(_ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool) -> *mut u8 { + fn remap(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool) -> *mut u8 { // TODO: I think this can be implemented near the end? ptr::null_mut() } - unsafe fn free_part(_ptr: *mut u8, _oldsize: usize, _newsize: usize) -> bool { + fn free_part(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize) -> bool { false } - unsafe fn free(_ptr: *mut u8, _size: usize) -> bool { + fn free(&self, _ptr: *mut u8, _size: usize) -> bool { false } - fn can_release_part(_flags: u32) -> bool { + fn can_release_part(&self, _flags: u32) -> bool { false } - fn allocates_zeros() -> bool { + fn allocates_zeros(&self) -> bool { true } - fn page_size() -> usize { + fn page_size(&self) -> usize { 64 * 1024 } } #[cfg(feature = "global")] -impl GlobalSystem for Platform { - fn acquire_global_lock() { - // single threaded, no need! - } +pub fn acquire_global_lock() { + // single threaded, no need! +} - fn release_global_lock() { - // single threaded, no need! - } +#[cfg(feature = "global")] +pub fn release_global_lock() { + // single threaded, no need! } diff --git a/tests/smoke.rs b/tests/smoke.rs index f2512eb..cd69f94 100644 --- a/tests/smoke.rs +++ b/tests/smoke.rs @@ -1,17 +1,13 @@ extern crate dlmalloc; extern crate rand; -#[cfg(any(target_os = "linux", target_arch = "wasm32", target_os = "macos"))] 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"))] use std::cmp; #[test] -#[cfg(any(target_os = "linux", target_arch = "wasm32", target_os = "macos"))] fn smoke() { - let mut a: Dlmalloc = Dlmalloc::new(); + let mut a = Dlmalloc::new(); unsafe { let ptr = a.malloc(1, 1); assert!(!ptr.is_null()); @@ -28,9 +24,8 @@ fn smoke() { } #[test] -#[cfg(any(target_os = "linux", target_arch = "wasm32", target_os = "macos"))] fn stress() { - let mut a: Dlmalloc = Dlmalloc::new(); + let mut a = 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 };