Skip to content

Commit cd8d627

Browse files
committed
Use new allocator API, and otherwise update for new Rust
- New allocator API - Remove the "allocator" feature, which should be unnecessary due to how the new allocator API works - NonZero no longer implements Deref (rust-lang/rust#41064) - NonZero::new() returns an Option; use NonZero::new_unchecked() - Thread locals are no longer 'static (rust-lang/rust#43746) - Changes to feature flags - Use unsafe to access extern static (rust-lang/rust#36247)
1 parent 56a91d8 commit cd8d627

File tree

10 files changed

+75
-94
lines changed

10 files changed

+75
-94
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ debug-assertions = false
2929
codegen-units = 1
3030

3131
[features]
32-
default = ["allocator", "tls"]
32+
default = ["tls"]
3333
# ---
3434
alloc_id = []
35-
allocator = []
3635
debugger = []
3736
log = ["write", "alloc_id"]
3837
no_log_lock = ["log"]

shim/src/thread_destructor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ pub mod arch {
2222
/// A thread destructor.
2323
type Dtor = unsafe extern fn(dtor: unsafe extern fn(*mut u8), arg: *mut u8, dso_handle: *mut u8) -> i32;
2424

25-
// Make sure the symbols exist.
26-
assert!(!__cxa_thread_atexit_impl.is_null());
27-
2825
unsafe {
26+
// Make sure the symbols exist.
27+
assert!(!__cxa_thread_atexit_impl.is_null());
28+
2929
mem::transmute::<*const u8, Dtor>(__cxa_thread_atexit_impl)
3030
(dtor, t, &__dso_handle as *const _ as *mut _)
3131
};

src/allocator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl Allocator for LocalAllocator {
300300
pub fn alloc(size: usize, align: usize) -> *mut u8 {
301301
log!(CALL, "Allocating buffer of size {} (align {}).", size, align);
302302

303-
get_allocator!(|alloc| *Pointer::from(alloc.alloc(size, align)))
303+
get_allocator!(|alloc| Pointer::from(alloc.alloc(size, align)).get())
304304
}
305305

306306
/// Free a buffer.
@@ -353,11 +353,11 @@ pub unsafe fn realloc(ptr: *mut u8, old_size: usize, size: usize, align: usize)
353353
log!(CALL, "Reallocating buffer of size {} to new size {}.", old_size, size);
354354

355355
get_allocator!(|alloc| {
356-
*Pointer::from(alloc.realloc(
356+
Pointer::from(alloc.realloc(
357357
Block::from_raw_parts(Pointer::new(ptr), old_size),
358358
size,
359359
align
360-
))
360+
)).get()
361361
})
362362
}
363363

src/block.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl Block {
110110
/// Is this block aligned to `align`?
111111
#[inline]
112112
pub fn aligned_to(&self, align: usize) -> bool {
113-
*self.ptr as usize % align == 0
113+
self.ptr.get() as usize % align == 0
114114
}
115115

116116
/// memcpy the block to another pointer.
@@ -129,7 +129,7 @@ impl Block {
129129
// LAST AUDIT: 2016-08-21 (Ticki).
130130

131131
// From the invariants of `Block`, this copy is well-defined.
132-
ptr::copy_nonoverlapping(*self.ptr, *block.ptr, self.size);
132+
ptr::copy_nonoverlapping(self.ptr.get(), block.ptr.get(), self.size);
133133
}
134134
}
135135

@@ -145,7 +145,7 @@ impl Block {
145145

146146
// Since the memory of the block is inaccessible (read-wise), zeroing it is fully
147147
// safe.
148-
intrinsics::volatile_set_memory(*self.ptr, 0, self.size);
148+
intrinsics::volatile_set_memory(self.ptr.get(), 0, self.size);
149149
}
150150
}
151151
}
@@ -162,7 +162,7 @@ impl Block {
162162
#[inline]
163163
pub fn left_to(&self, to: &Block) -> bool {
164164
// This won't overflow due to the end being bounded by the address space.
165-
self.size + *self.ptr as usize == *to.ptr as usize
165+
self.size + self.ptr.get() as usize == to.ptr.get() as usize
166166
}
167167

168168
/// Split the block at some position.
@@ -207,7 +207,7 @@ impl Block {
207207

208208
// Calculate the aligner, which defines the smallest size required as precursor to align
209209
// the block to `align`.
210-
let aligner = (align - *self.ptr as usize % align) % align;
210+
let aligner = (align - self.ptr.get() as usize % align) % align;
211211
// ^^^^^^^^
212212
// To avoid wasting space on the case where the block is already aligned, we calculate it
213213
// modulo `align`.
@@ -275,30 +275,30 @@ impl From<Block> for Pointer<u8> {
275275
impl PartialOrd for Block {
276276
#[inline]
277277
fn partial_cmp(&self, other: &Block) -> Option<cmp::Ordering> {
278-
self.ptr.partial_cmp(&other.ptr)
278+
self.ptr.get().partial_cmp(&other.ptr.get())
279279
}
280280
}
281281

282282
/// Compare the blocks address.
283283
impl Ord for Block {
284284
#[inline]
285285
fn cmp(&self, other: &Block) -> cmp::Ordering {
286-
self.ptr.cmp(&other.ptr)
286+
self.ptr.get().cmp(&other.ptr.get())
287287
}
288288
}
289289

290290
impl cmp::PartialEq for Block {
291291
#[inline]
292292
fn eq(&self, other: &Block) -> bool {
293-
*self.ptr == *other.ptr
293+
self.ptr.get() == other.ptr.get()
294294
}
295295
}
296296

297297
impl cmp::Eq for Block {}
298298

299299
impl fmt::Debug for Block {
300300
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
301-
write!(f, "0x{:x}[{}]", *self.ptr as usize, self.size)
301+
write!(f, "0x{:x}[{}]", self.ptr.get() as usize, self.size)
302302
}
303303
}
304304

src/brk.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl BrkLock {
4646
let expected_brk = self.current_brk().offset(size);
4747

4848
// Break it to me, babe!
49-
let old_brk = Pointer::new(syscalls::brk(*expected_brk as *const u8) as *mut u8);
49+
let old_brk = Pointer::new(syscalls::brk(expected_brk.get() as *const u8) as *mut u8);
5050

5151
/// AAAARGH WAY TOO MUCH LOGGING
5252
///
@@ -180,7 +180,7 @@ pub fn lock() -> BrkLock {
180180
///
181181
/// On failure the maximum pointer (`!0 as *mut u8`) is returned.
182182
pub unsafe extern fn sbrk(size: isize) -> *mut u8 {
183-
*lock().sbrk(size).unwrap_or_else(|()| Pointer::new(!0 as *mut u8))
183+
lock().sbrk(size).unwrap_or_else(|()| Pointer::new(!0 as *mut u8)).get()
184184
}
185185

186186
/// Get the current program break.

src/lib.rs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,28 @@
99
//! relatively strong condition, which means that you are forced to rewrite primitives and make
1010
//! sure no allocation ever happens.
1111
12-
#![cfg_attr(feature = "allocator", allocator)]
1312
#![cfg_attr(feature = "clippy", feature(plugin))]
1413
#![cfg_attr(feature = "clippy", plugin(clippy))]
1514

1615
#![no_std]
1716

18-
#![feature(allocator, const_fn, core_intrinsics, stmt_expr_attributes, drop_types_in_const,
17+
#![feature(alloc, allocator_api, const_fn, core_intrinsics, stmt_expr_attributes, drop_types_in_const,
1918
nonzero, optin_builtin_traits, type_ascription, thread_local, linkage,
20-
try_from)]
19+
try_from, const_unsafe_cell_new, const_atomic_bool_new, const_nonzero_new,
20+
const_atomic_ptr_new)]
2121
#![warn(missing_docs, cast_precision_loss, cast_sign_loss, cast_possible_wrap,
2222
cast_possible_truncation, filter_map, if_not_else, items_after_statements,
2323
invalid_upcast_comparisons, mutex_integer, nonminimal_bool, shadow_same, shadow_unrelated,
2424
single_match_else, string_add, string_add_assign, wrong_pub_self_convention)]
2525

26+
extern crate alloc;
2627
extern crate ralloc_shim as shim;
2728

2829
#[macro_use]
2930
mod log;
3031
#[macro_use]
3132
#[cfg(feature = "tls")]
3233
mod tls;
33-
#[cfg(feature = "allocator")]
34-
mod symbols;
3534

3635
#[macro_use]
3736
mod unborrow;
@@ -49,8 +48,47 @@ mod ptr;
4948
mod sync;
5049
mod vec;
5150

51+
use alloc::heap::{Alloc, AllocErr, Layout, CannotReallocInPlace};
52+
5253
pub use allocator::{alloc, free, realloc, realloc_inplace};
5354
pub use brk::sbrk;
5455
pub use fail::set_oom_handler;
5556
#[cfg(feature = "tls")]
5657
pub use fail::set_thread_oom_handler;
58+
59+
pub struct Allocator;
60+
61+
unsafe impl<'a> Alloc for &'a Allocator {
62+
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
63+
Ok(allocator::alloc(layout.size(), layout.align()))
64+
}
65+
66+
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
67+
allocator::free(ptr, layout.size());
68+
}
69+
70+
unsafe fn realloc(&mut self, ptr: *mut u8, layout: Layout, new_layout: Layout) -> Result<*mut u8, AllocErr> {
71+
Ok(allocator::realloc(ptr, layout.size(), new_layout.size(), new_layout.align()))
72+
}
73+
74+
unsafe fn grow_in_place(&mut self, ptr: *mut u8, layout: Layout, new_layout: Layout) -> Result<(), CannotReallocInPlace> {
75+
if allocator::realloc_inplace(ptr, layout.size(), new_layout.size()).is_ok() {
76+
Ok(())
77+
} else {
78+
Err(CannotReallocInPlace)
79+
}
80+
}
81+
82+
unsafe fn shrink_in_place(&mut self, ptr: *mut u8, layout: Layout, new_layout: Layout) -> Result<(), CannotReallocInPlace> {
83+
if allocator::realloc_inplace(ptr, layout.size(), new_layout.size()).is_ok() {
84+
Ok(())
85+
} else {
86+
Err(CannotReallocInPlace)
87+
}
88+
}
89+
90+
fn usable_size(&self, layout: &Layout) -> (usize, usize) {
91+
// Yay! It matches exactly.
92+
(layout.size(), layout.size())
93+
}
94+
}

src/ptr.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<T> Pointer<T> {
3030
debug_assert!(!ptr.is_null(), "Null pointer!");
3131

3232
Pointer {
33-
ptr: NonZero::new(ptr),
33+
ptr: NonZero::new_unchecked(ptr),
3434
_phantom: marker::PhantomData,
3535
}
3636
}
@@ -45,7 +45,7 @@ impl<T> Pointer<T> {
4545
// LAST AUDIT: 2016-08-21 (Ticki).
4646

4747
// 0x1 is non-zero.
48-
NonZero::new(0x1 as *mut T)
48+
NonZero::new_unchecked(0x1 as *mut T)
4949
},
5050
_phantom: marker::PhantomData,
5151
}
@@ -61,7 +61,7 @@ impl<T> Pointer<T> {
6161
// LAST AUDIT: 2016-08-21 (Ticki).
6262

6363
// Casting the pointer will preserve its nullable state.
64-
NonZero::new(*self as *mut U)
64+
NonZero::new_unchecked(self.get() as *mut U)
6565
},
6666
_phantom: marker::PhantomData,
6767
}
@@ -76,7 +76,11 @@ impl<T> Pointer<T> {
7676
/// This is unsafe, due to OOB offsets being undefined behavior.
7777
#[inline]
7878
pub unsafe fn offset(self, diff: isize) -> Pointer<T> {
79-
Pointer::new(self.ptr.offset(diff))
79+
Pointer::new(self.ptr.get().offset(diff))
80+
}
81+
82+
pub fn get(&self) -> *mut T {
83+
self.ptr.get()
8084
}
8185
}
8286

@@ -89,15 +93,6 @@ impl<T> Default for Pointer<T> {
8993
unsafe impl<T: Send> Send for Pointer<T> {}
9094
unsafe impl<T: Sync> Sync for Pointer<T> {}
9195

92-
impl<T> ops::Deref for Pointer<T> {
93-
type Target = *mut T;
94-
95-
#[inline]
96-
fn deref(&self) -> &*mut T {
97-
&self.ptr
98-
}
99-
}
100-
10196
#[cfg(test)]
10297
mod test {
10398
use super::*;

src/symbols.rs

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/tls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<T: 'static> Key<T> {
2929
/// Having a reference newtype would be unsound, due to the ability to leak a reference to
3030
/// another thread.
3131
#[inline]
32-
pub fn with<F, R>(&'static self, f: F) -> R
32+
pub fn with<F, R>(&self, f: F) -> R
3333
where F: FnOnce(&T) -> R {
3434
// Logging.
3535
log!(INTERNAL, "Accessing TLS variable.");
@@ -42,7 +42,7 @@ impl<T: 'static> Key<T> {
4242
/// Note that this has to be registered for every thread, it is needed for.
4343
// TODO: Make this automatic on `Drop`.
4444
#[inline]
45-
pub fn register_thread_destructor(&'static self, dtor: extern fn(&T)) {
45+
pub fn register_thread_destructor(&self, dtor: extern fn(&T)) {
4646
// Logging.
4747
log!(INTERNAL, "Registering thread destructor.");
4848

src/vec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<T: Leak> Vec<T> {
6868

6969
// Due to the invariants of `Block`, this copy is safe (the pointer is valid and
7070
// unaliased).
71-
ptr::copy_nonoverlapping(*old.ptr, *self.ptr, old.len);
71+
ptr::copy_nonoverlapping(old.ptr.get(), self.ptr.get(), old.len);
7272
}
7373

7474
Block::from(old)
@@ -95,7 +95,7 @@ impl<T: Leak> Vec<T> {
9595

9696
// By the invariants of this type (the size is bounded by the address space), this
9797
// conversion isn't overflowing.
98-
ptr::write((*self.ptr).offset(self.len as isize), elem);
98+
ptr::write((self.ptr.get()).offset(self.len as isize), elem);
9999
}
100100

101101
// Increment the length.
@@ -193,7 +193,7 @@ impl<T: Leak> ops::Deref for Vec<T> {
193193
// LAST AUDIT: 2016-08-21 (Ticki).
194194

195195
// The invariants maintains safety.
196-
slice::from_raw_parts(*self.ptr as *const T, self.len)
196+
slice::from_raw_parts(self.ptr.get() as *const T, self.len)
197197
}
198198
}
199199
}
@@ -205,7 +205,7 @@ impl<T: Leak> ops::DerefMut for Vec<T> {
205205
// LAST AUDIT: 2016-08-21 (Ticki).
206206

207207
// The invariants maintains safety.
208-
slice::from_raw_parts_mut(*self.ptr as *mut T, self.len)
208+
slice::from_raw_parts_mut(self.ptr.get() as *mut T, self.len)
209209
}
210210
}
211211
}

0 commit comments

Comments
 (0)