Skip to content

Commit 4cfd38a

Browse files
authored
Merge pull request #45 from 0x7CFE/dk-alloc-fix
Fixes allocator to reflect changes in nightly API
2 parents bdd0c11 + 4102b6c commit 4cfd38a

File tree

9 files changed

+64
-65
lines changed

9 files changed

+64
-65
lines changed

README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,9 @@ infrastructure. Nevertheless, `wee_alloc` is also usable with `std`.
5050
// We aren't using the standard library.
5151
#![no_std]
5252

53-
// Required to replace the global allocator.
54-
#![feature(global_allocator)]
55-
5653
// Required to use the `alloc` crate and its types, the `abort` intrinsic, and a
5754
// custom panic handler.
58-
#![feature(alloc, core_intrinsics, lang_items)]
55+
#![feature(alloc, core_intrinsics, panic_implementation, lang_items)]
5956

6057
extern crate alloc;
6158
extern crate wee_alloc;
@@ -64,18 +61,22 @@ extern crate wee_alloc;
6461
#[global_allocator]
6562
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
6663

67-
// Need to provide a tiny `panic_fmt` lang-item implementation for `#![no_std]`.
68-
// This implementation will translate panics into traps in the resulting
69-
// WebAssembly.
70-
#[lang = "panic_fmt"]
71-
extern "C" fn panic_fmt(
72-
_args: ::core::fmt::Arguments,
73-
_file: &'static str,
74-
_line: u32
75-
) -> ! {
76-
use core::intrinsics;
64+
// Need to provide a tiny `panic` implementation for `#![no_std]`.
65+
// This translates into an `unreachable` instruction that will
66+
// raise a `trap` the WebAssembly execution if we panic at runtime.
67+
#[panic_implementation]
68+
fn panic(_info: &::core::panic::PanicInfo) -> ! {
69+
unsafe {
70+
::core::intrinsics::abort();
71+
}
72+
}
73+
74+
// Need to provide a tiny `oom` lang-item implementation for
75+
// `#![no_std]`.
76+
#[lang = "oom"]
77+
extern "C" fn oom() -> ! {
7778
unsafe {
78-
intrinsics::abort();
79+
::core::intrinsics::abort();
7980
}
8081
}
8182

example/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// We aren't using the standard library.
77
#![no_std]
88
// Replacing the allocator and using the `alloc` crate are still unstable.
9-
#![feature(alloc, core_intrinsics, global_allocator, lang_items)]
9+
#![feature(alloc, core_intrinsics, panic_implementation, lang_items)]
1010

1111
extern crate alloc;
1212
extern crate wee_alloc;
@@ -15,11 +15,11 @@ extern crate wee_alloc;
1515
#[global_allocator]
1616
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
1717

18-
// Need to provide a tiny `panic_fmt` lang-item implementation for
19-
// `#![no_std]`. This translates into an `unreachable` instruction that will
18+
// Need to provide a tiny `panic` implementation for `#![no_std]`.
19+
// This translates into an `unreachable` instruction that will
2020
// raise a `trap` the WebAssembly execution if we panic at runtime.
21-
#[lang = "panic_fmt"]
22-
extern "C" fn panic_fmt(_args: ::core::fmt::Arguments, _file: &'static str, _line: u32) -> ! {
21+
#[panic_implementation]
22+
fn panic(_info: &::core::panic::PanicInfo) -> ! {
2323
unsafe {
2424
::core::intrinsics::abort();
2525
}

test/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![feature(alloc, allocator_api)]
22

3-
extern crate alloc;
43
extern crate histo;
54
#[macro_use]
65
extern crate quickcheck;
@@ -9,7 +8,7 @@ extern crate cfg_if;
98
extern crate rand;
109
extern crate wee_alloc;
1110

12-
use alloc::heap::{Alloc, Layout};
11+
use std::alloc::{Alloc, Layout};
1312
use quickcheck::{Arbitrary, Gen};
1413
use std::f64;
1514
use std::fs;
@@ -374,7 +373,7 @@ quickcheck! {
374373
let align = ALIGNS[align % ALIGNS.len()];
375374

376375
let mut w = &wee_alloc::WeeAlloc::INIT;
377-
let layout = alloc::heap::Layout::from_size_align(size, align).unwrap();
376+
let layout = Layout::from_size_align(size, align).unwrap();
378377
let _ = unsafe { w.alloc(layout) };
379378
}
380379
}

test/tests/global.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Adopted from
22
// https://github.com/alexcrichton/dlmalloc-rs/blob/master/tests/global.rs
33

4-
#![feature(global_allocator)]
5-
64
extern crate wee_alloc;
75

86
use std::collections::HashMap;

wee_alloc/src/imp_static_array.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use const_init::ConstInit;
2-
use core::alloc::{AllocErr, Opaque};
2+
use core::alloc::AllocErr;
33
#[cfg(feature = "extra_assertions")]
44
use core::cell::Cell;
55
use core::ptr::NonNull;
@@ -10,12 +10,12 @@ const SCRATCH_LEN_BYTES: usize = include!(concat!(env!("OUT_DIR"), "/wee_alloc_s
1010
static mut SCRATCH_HEAP: [u8; SCRATCH_LEN_BYTES] = [0; SCRATCH_LEN_BYTES];
1111
static mut OFFSET: Mutex<usize> = Mutex::new(0);
1212

13-
pub(crate) unsafe fn alloc_pages(pages: Pages) -> Result<NonNull<Opaque>, AllocErr> {
13+
pub(crate) unsafe fn alloc_pages(pages: Pages) -> Result<NonNull<u8>, AllocErr> {
1414
let bytes: Bytes = pages.into();
1515
let mut offset = OFFSET.lock();
1616
let end = bytes.0 + *offset;
1717
if end < SCRATCH_LEN_BYTES {
18-
let ptr = SCRATCH_HEAP[*offset..end].as_mut_ptr() as *mut u8 as *mut Opaque;
18+
let ptr = SCRATCH_HEAP[*offset..end].as_mut_ptr() as *mut u8;
1919
*offset = end;
2020
NonNull::new(ptr).ok_or_else(|| AllocErr)
2121
} else {

wee_alloc/src/imp_unix.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use const_init::ConstInit;
2-
use core::alloc::{AllocErr, Opaque};
2+
use core::alloc::AllocErr;
33
use core::cell::UnsafeCell;
44
use core::ptr::NonNull;
55
use libc;
66
use memory_units::{Bytes, Pages};
77

8-
pub(crate) fn alloc_pages(pages: Pages) -> Result<NonNull<Opaque>, AllocErr> {
8+
pub(crate) fn alloc_pages(pages: Pages) -> Result<NonNull<u8>, AllocErr> {
99
unsafe {
1010
let bytes: Bytes = pages.into();
1111
let addr = libc::mmap(
@@ -19,7 +19,7 @@ pub(crate) fn alloc_pages(pages: Pages) -> Result<NonNull<Opaque>, AllocErr> {
1919
if addr == libc::MAP_FAILED {
2020
Err(AllocErr)
2121
} else {
22-
NonNull::new(addr as *mut Opaque).ok_or(AllocErr)
22+
NonNull::new(addr as *mut u8).ok_or(AllocErr)
2323
}
2424
}
2525
}

wee_alloc/src/imp_wasm32.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{assert_is_word_aligned, PAGE_SIZE, unchecked_unwrap};
22
use const_init::ConstInit;
3-
use core::alloc::{AllocErr, Opaque};
3+
use core::alloc::AllocErr;
44
use core::cell::UnsafeCell;
55
use core::ptr::NonNull;
66
use memory_units::Pages;
@@ -10,10 +10,10 @@ extern "C" {
1010
fn grow_memory(pages: usize) -> i32;
1111
}
1212

13-
pub(crate) unsafe fn alloc_pages(n: Pages) -> Result<NonNull<Opaque>, AllocErr> {
13+
pub(crate) unsafe fn alloc_pages(n: Pages) -> Result<NonNull<u8>, AllocErr> {
1414
let ptr = grow_memory(n.0);
1515
if -1 != ptr {
16-
let ptr = (ptr as usize * PAGE_SIZE.0) as *mut Opaque;
16+
let ptr = (ptr as usize * PAGE_SIZE.0) as *mut u8;
1717
assert_is_word_aligned(ptr as *mut u8);
1818
Ok(unchecked_unwrap(NonNull::new(ptr)))
1919
} else {

wee_alloc/src/imp_windows.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use const_init::ConstInit;
2-
use core::alloc::{AllocErr, Opaque};
2+
use core::alloc::AllocErr;
33
use core::cell::UnsafeCell;
44
use core::ptr::NonNull;
55
use memory_units::{Bytes, Pages};
@@ -12,10 +12,10 @@ use winapi::um::synchapi::{CreateMutexW, ReleaseMutex, WaitForSingleObject};
1212
use winapi::um::winbase::{WAIT_OBJECT_0, INFINITE};
1313
use winapi::um::winnt::{HANDLE, MEM_COMMIT, PAGE_READWRITE};
1414

15-
pub(crate) fn alloc_pages(pages: Pages) -> Result<NonNull<Opaque>, AllocErr> {
15+
pub(crate) fn alloc_pages(pages: Pages) -> Result<NonNull<u8>, AllocErr> {
1616
let bytes: Bytes = pages.into();
1717
let ptr = unsafe { VirtualAlloc(NULL, bytes.0, MEM_COMMIT, PAGE_READWRITE) };
18-
NonNull::new(ptr as *mut Opaque).ok_or(AllocErr)
18+
NonNull::new(ptr as *mut u8).ok_or(AllocErr)
1919
}
2020

2121
// Align to the cache line size on an i7 to avoid false sharing.

wee_alloc/src/lib.rs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,9 @@ infrastructure. Nevertheless, `wee_alloc` is also usable with `std`.
5050
// We aren't using the standard library.
5151
#![no_std]
5252
53-
// Required to replace the global allocator.
54-
#![feature(global_allocator)]
55-
5653
// Required to use the `alloc` crate and its types, the `abort` intrinsic, and a
5754
// custom panic handler.
58-
#![feature(alloc, core_intrinsics, lang_items)]
55+
#![feature(alloc, core_intrinsics, panic_implementation, lang_items)]
5956
6057
extern crate alloc;
6158
extern crate wee_alloc;
@@ -64,18 +61,22 @@ extern crate wee_alloc;
6461
#[global_allocator]
6562
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
6663
67-
// Need to provide a tiny `panic_fmt` lang-item implementation for `#![no_std]`.
68-
// This implementation will translate panics into traps in the resulting
69-
// WebAssembly.
70-
#[lang = "panic_fmt"]
71-
extern "C" fn panic_fmt(
72-
_args: ::core::fmt::Arguments,
73-
_file: &'static str,
74-
_line: u32
75-
) -> ! {
76-
use core::intrinsics;
64+
// Need to provide a tiny `panic` implementation for `#![no_std]`.
65+
// This translates into an `unreachable` instruction that will
66+
// raise a `trap` the WebAssembly execution if we panic at runtime.
67+
#[panic_implementation]
68+
fn panic(_info: &::core::panic::PanicInfo) -> ! {
69+
unsafe {
70+
::core::intrinsics::abort();
71+
}
72+
}
73+
74+
// Need to provide a tiny `oom` lang-item implementation for
75+
// `#![no_std]`.
76+
#[lang = "oom"]
77+
extern "C" fn oom() -> ! {
7778
unsafe {
78-
intrinsics::abort();
79+
::core::intrinsics::abort();
7980
}
8081
}
8182
@@ -222,7 +223,7 @@ for hacking!
222223

223224
#![deny(missing_docs)]
224225
#![cfg_attr(not(feature = "use_std_for_test_debugging"), no_std)]
225-
#![feature(alloc, allocator_api, core_intrinsics, global_allocator)]
226+
#![feature(alloc, allocator_api, core_intrinsics)]
226227
#![cfg_attr(target_arch = "wasm32", feature(link_llvm_intrinsics))]
227228

228229
#[macro_use]
@@ -269,7 +270,7 @@ mod neighbors;
269270
mod size_classes;
270271

271272
use const_init::ConstInit;
272-
use core::alloc::{Alloc, AllocErr, GlobalAlloc, Layout, Opaque};
273+
use core::alloc::{Alloc, AllocErr, GlobalAlloc, Layout};
273274
use core::cell::Cell;
274275
use core::cmp;
275276
use core::marker::Sync;
@@ -520,7 +521,7 @@ impl<'a> FreeCell<'a> {
520521
}
521522

522523
unsafe fn from_uninitialized(
523-
raw: NonNull<Opaque>,
524+
raw: NonNull<u8>,
524525
size: Bytes,
525526
next_free: Option<*const FreeCell<'a>>,
526527
policy: &AllocPolicy<'a>,
@@ -586,7 +587,7 @@ impl<'a> FreeCell<'a> {
586587
let split_cell_head = split_and_aligned - size_of::<CellHeader>().0;
587588
let split_cell = unsafe {
588589
&*FreeCell::from_uninitialized(
589-
unchecked_unwrap(NonNull::new(split_cell_head as *mut Opaque)),
590+
unchecked_unwrap(NonNull::new(split_cell_head as *mut u8)),
590591
Bytes(next - split_cell_head) - size_of::<CellHeader>(),
591592
None,
592593
policy,
@@ -951,7 +952,7 @@ unsafe fn alloc_first_fit<'a>(
951952
align: Bytes,
952953
head: &Cell<*const FreeCell<'a>>,
953954
policy: &AllocPolicy<'a>,
954-
) -> Result<NonNull<Opaque>, AllocErr> {
955+
) -> Result<NonNull<u8>, AllocErr> {
955956
extra_assert!(size.0 > 0);
956957

957958
walk_free_list(head, policy, |previous, current| {
@@ -960,7 +961,7 @@ unsafe fn alloc_first_fit<'a>(
960961
if let Some(allocated) = current.try_alloc(previous, size, align, policy) {
961962
assert_aligned_to(allocated.data(), align);
962963
return Some(unchecked_unwrap(
963-
NonNull::new(allocated.data() as *mut Opaque),
964+
NonNull::new(allocated.data() as *mut u8),
964965
));
965966
}
966967

@@ -973,7 +974,7 @@ unsafe fn alloc_with_refill<'a, 'b>(
973974
align: Bytes,
974975
head: &'b Cell<*const FreeCell<'a>>,
975976
policy: &AllocPolicy<'a>,
976-
) -> Result<NonNull<Opaque>, AllocErr> {
977+
) -> Result<NonNull<u8>, AllocErr> {
977978
if let Ok(result) = alloc_first_fit(size, align, head, policy) {
978979
return Ok(result);
979980
}
@@ -1072,7 +1073,7 @@ unsafe impl<'a, 'b> Alloc for &'b WeeAlloc<'a>
10721073
where
10731074
'a: 'b,
10741075
{
1075-
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<Opaque>, AllocErr> {
1076+
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
10761077
let size = Bytes(layout.size());
10771078
let align = if layout.align() == 0 {
10781079
Bytes(1)
@@ -1084,7 +1085,7 @@ where
10841085
// Ensure that our made up pointer is properly aligned by using the
10851086
// alignment as the pointer.
10861087
extra_assert!(align.0 > 0);
1087-
return Ok(NonNull::new_unchecked(align.0 as *mut Opaque));
1088+
return Ok(NonNull::new_unchecked(align.0 as *mut u8));
10881089
}
10891090

10901091
let size: Words = size.round_up_to();
@@ -1095,7 +1096,7 @@ where
10951096
})
10961097
}
10971098

1098-
unsafe fn dealloc(&mut self, ptr: NonNull<Opaque>, layout: Layout) {
1099+
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
10991100
let size = Bytes(layout.size());
11001101
if size.0 == 0 {
11011102
return;
@@ -1185,15 +1186,15 @@ where
11851186
}
11861187

11871188
unsafe impl GlobalAlloc for WeeAlloc<'static> {
1188-
unsafe fn alloc(&self, layout: Layout) -> *mut Opaque {
1189+
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
11891190
let mut me = self;
11901191
match Alloc::alloc(&mut me, layout) {
11911192
Ok(ptr) => ptr.as_ptr(),
1192-
Err(AllocErr) => 0 as *mut Opaque,
1193+
Err(AllocErr) => 0 as *mut u8,
11931194
}
11941195
}
11951196

1196-
unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) {
1197+
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
11971198
if let Some(ptr) = NonNull::new(ptr) {
11981199
let mut me = self;
11991200
Alloc::dealloc(&mut me, ptr, layout);

0 commit comments

Comments
 (0)