Skip to content

Commit 38e2d54

Browse files
committed
Use a newtype instead of an extern type for Opaque
1 parent 4f99f37 commit 38e2d54

File tree

7 files changed

+30
-48
lines changed

7 files changed

+30
-48
lines changed

src/liballoc/arc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ impl<T: ?Sized> Arc<T> {
518518

519519
if self.inner().weak.fetch_sub(1, Release) == 1 {
520520
atomic::fence(Acquire);
521-
Global.dealloc(self.ptr.as_opaque(), Layout::for_value(self.ptr.as_ref()))
521+
Global.dealloc(self.ptr.cast(), Layout::for_value(self.ptr.as_ref()))
522522
}
523523
}
524524

@@ -638,7 +638,7 @@ impl<T: Clone> ArcFromSlice<T> for Arc<[T]> {
638638
let slice = from_raw_parts_mut(self.elems, self.n_elems);
639639
ptr::drop_in_place(slice);
640640

641-
Global.dealloc(self.mem.as_opaque(), self.layout.clone());
641+
Global.dealloc(self.mem.cast(), self.layout.clone());
642642
}
643643
}
644644
}
@@ -1157,7 +1157,7 @@ impl<T: ?Sized> Drop for Weak<T> {
11571157
if self.inner().weak.fetch_sub(1, Release) == 1 {
11581158
atomic::fence(Acquire);
11591159
unsafe {
1160-
Global.dealloc(self.ptr.as_opaque(), Layout::for_value(self.ptr.as_ref()))
1160+
Global.dealloc(self.ptr.cast(), Layout::for_value(self.ptr.as_ref()))
11611161
}
11621162
}
11631163
}

src/liballoc/btree/node.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ impl<K, V> Root<K, V> {
287287
self.as_mut().as_leaf_mut().parent = ptr::null();
288288

289289
unsafe {
290-
Global.dealloc(NonNull::from(top).as_opaque(), Layout::new::<InternalNode<K, V>>());
290+
Global.dealloc(NonNull::from(top).cast(), Layout::new::<InternalNode<K, V>>());
291291
}
292292
}
293293
}
@@ -478,7 +478,7 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::Leaf> {
478478
debug_assert!(!self.is_shared_root());
479479
let node = self.node;
480480
let ret = self.ascend().ok();
481-
Global.dealloc(node.as_opaque(), Layout::new::<LeafNode<K, V>>());
481+
Global.dealloc(node.cast(), Layout::new::<LeafNode<K, V>>());
482482
ret
483483
}
484484
}
@@ -499,7 +499,7 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::Internal> {
499499
> {
500500
let node = self.node;
501501
let ret = self.ascend().ok();
502-
Global.dealloc(node.as_opaque(), Layout::new::<InternalNode<K, V>>());
502+
Global.dealloc(node.cast(), Layout::new::<InternalNode<K, V>>());
503503
ret
504504
}
505505
}
@@ -1321,12 +1321,12 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
13211321
}
13221322

13231323
Global.dealloc(
1324-
right_node.node.as_opaque(),
1324+
right_node.node.cast(),
13251325
Layout::new::<InternalNode<K, V>>(),
13261326
);
13271327
} else {
13281328
Global.dealloc(
1329-
right_node.node.as_opaque(),
1329+
right_node.node.cast(),
13301330
Layout::new::<LeafNode<K, V>>(),
13311331
);
13321332
}

src/liballoc/raw_vec.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl<T, A: Alloc> RawVec<T, A> {
9393

9494
// handles ZSTs and `cap = 0` alike
9595
let ptr = if alloc_size == 0 {
96-
NonNull::<T>::dangling().as_opaque()
96+
NonNull::<T>::dangling().cast()
9797
} else {
9898
let align = mem::align_of::<T>();
9999
let layout = Layout::from_size_align(alloc_size, align).unwrap();
@@ -314,7 +314,7 @@ impl<T, A: Alloc> RawVec<T, A> {
314314
let new_cap = 2 * self.cap;
315315
let new_size = new_cap * elem_size;
316316
alloc_guard(new_size).unwrap_or_else(|_| capacity_overflow());
317-
let ptr_res = self.a.realloc(NonNull::from(self.ptr).as_opaque(),
317+
let ptr_res = self.a.realloc(NonNull::from(self.ptr).cast(),
318318
cur,
319319
new_size);
320320
match ptr_res {
@@ -373,7 +373,7 @@ impl<T, A: Alloc> RawVec<T, A> {
373373
let new_cap = 2 * self.cap;
374374
let new_size = new_cap * elem_size;
375375
alloc_guard(new_size).unwrap_or_else(|_| capacity_overflow());
376-
match self.a.grow_in_place(NonNull::from(self.ptr).as_opaque(), old_layout, new_size) {
376+
match self.a.grow_in_place(NonNull::from(self.ptr).cast(), old_layout, new_size) {
377377
Ok(_) => {
378378
// We can't directly divide `size`.
379379
self.cap = new_cap;
@@ -546,7 +546,7 @@ impl<T, A: Alloc> RawVec<T, A> {
546546
// FIXME: may crash and burn on over-reserve
547547
alloc_guard(new_layout.size()).unwrap_or_else(|_| capacity_overflow());
548548
match self.a.grow_in_place(
549-
NonNull::from(self.ptr).as_opaque(), old_layout, new_layout.size(),
549+
NonNull::from(self.ptr).cast(), old_layout, new_layout.size(),
550550
) {
551551
Ok(_) => {
552552
self.cap = new_cap;
@@ -607,7 +607,7 @@ impl<T, A: Alloc> RawVec<T, A> {
607607
let new_size = elem_size * amount;
608608
let align = mem::align_of::<T>();
609609
let old_layout = Layout::from_size_align_unchecked(old_size, align);
610-
match self.a.realloc(NonNull::from(self.ptr).as_opaque(),
610+
match self.a.realloc(NonNull::from(self.ptr).cast(),
611611
old_layout,
612612
new_size) {
613613
Ok(p) => self.ptr = p.cast().into(),
@@ -667,7 +667,7 @@ impl<T, A: Alloc> RawVec<T, A> {
667667
let res = match self.current_layout() {
668668
Some(layout) => {
669669
debug_assert!(new_layout.align() == layout.align());
670-
self.a.realloc(NonNull::from(self.ptr).as_opaque(), layout, new_layout.size())
670+
self.a.realloc(NonNull::from(self.ptr).cast(), layout, new_layout.size())
671671
}
672672
None => self.a.alloc(new_layout),
673673
};
@@ -710,7 +710,7 @@ impl<T, A: Alloc> RawVec<T, A> {
710710
let elem_size = mem::size_of::<T>();
711711
if elem_size != 0 {
712712
if let Some(layout) = self.current_layout() {
713-
self.a.dealloc(NonNull::from(self.ptr).as_opaque(), layout);
713+
self.a.dealloc(NonNull::from(self.ptr).cast(), layout);
714714
}
715715
}
716716
}

src/liballoc/rc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Rc<T> {
845845
self.dec_weak();
846846

847847
if self.weak() == 0 {
848-
Global.dealloc(self.ptr.as_opaque(), Layout::for_value(self.ptr.as_ref()));
848+
Global.dealloc(self.ptr.cast(), Layout::for_value(self.ptr.as_ref()));
849849
}
850850
}
851851
}
@@ -1269,7 +1269,7 @@ impl<T: ?Sized> Drop for Weak<T> {
12691269
// the weak count starts at 1, and will only go to zero if all
12701270
// the strong pointers have disappeared.
12711271
if self.weak() == 0 {
1272-
Global.dealloc(self.ptr.as_opaque(), Layout::for_value(self.ptr.as_ref()));
1272+
Global.dealloc(self.ptr.cast(), Layout::for_value(self.ptr.as_ref()));
12731273
}
12741274
}
12751275
}

src/libcore/alloc.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,15 @@ use mem;
2121
use usize;
2222
use ptr::{self, NonNull};
2323

24-
extern {
25-
/// An opaque, unsized type. Used for pointers to allocated memory.
26-
///
27-
/// This type can only be used behind a pointer like `*mut Opaque` or `ptr::NonNull<Opaque>`.
28-
/// Such pointers are similar to C’s `void*` type.
29-
pub type Opaque;
30-
}
31-
32-
impl Opaque {
33-
/// Similar to `std::ptr::null`, which requires `T: Sized`.
34-
pub fn null() -> *const Self {
35-
0 as _
36-
}
37-
38-
/// Similar to `std::ptr::null_mut`, which requires `T: Sized`.
39-
pub fn null_mut() -> *mut Self {
40-
0 as _
41-
}
42-
}
24+
/// An opaque, byte-sized type. Used for pointers to allocated memory.
25+
///
26+
/// This type can only be used behind a pointer like `*mut Opaque` or `ptr::NonNull<Opaque>`.
27+
/// Such pointers are similar to C’s `void*` type.
28+
///
29+
/// `Opaque` has a size of 1 byte, which allows you to calculate byte offsets
30+
/// from it using the `offset`, `add` and `sub` methods on raw pointers.
31+
#[allow(missing_debug_implementations)]
32+
pub struct Opaque(u8);
4333

4434
/// Represents the combination of a starting address and
4535
/// a total capacity of the returned block.
@@ -964,7 +954,7 @@ pub unsafe trait Alloc {
964954
{
965955
let k = Layout::new::<T>();
966956
if k.size() > 0 {
967-
self.dealloc(ptr.as_opaque(), k);
957+
self.dealloc(ptr.cast(), k);
968958
}
969959
}
970960

@@ -1052,7 +1042,7 @@ pub unsafe trait Alloc {
10521042
match (Layout::array::<T>(n_old), Layout::array::<T>(n_new)) {
10531043
(Ok(ref k_old), Ok(ref k_new)) if k_old.size() > 0 && k_new.size() > 0 => {
10541044
debug_assert!(k_old.align() == k_new.align());
1055-
self.realloc(ptr.as_opaque(), k_old.clone(), k_new.size()).map(NonNull::cast)
1045+
self.realloc(ptr.cast(), k_old.clone(), k_new.size()).map(NonNull::cast)
10561046
}
10571047
_ => {
10581048
Err(AllocErr)
@@ -1085,7 +1075,7 @@ pub unsafe trait Alloc {
10851075
{
10861076
match Layout::array::<T>(n) {
10871077
Ok(ref k) if k.size() > 0 => {
1088-
Ok(self.dealloc(ptr.as_opaque(), k.clone()))
1078+
Ok(self.dealloc(ptr.cast(), k.clone()))
10891079
}
10901080
_ => {
10911081
Err(AllocErr)

src/libcore/ptr.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2921,14 +2921,6 @@ impl<T: ?Sized> NonNull<T> {
29212921
NonNull::new_unchecked(self.as_ptr() as *mut U)
29222922
}
29232923
}
2924-
2925-
/// Cast to an `Opaque` pointer
2926-
#[unstable(feature = "allocator_api", issue = "32838")]
2927-
pub fn as_opaque(self) -> NonNull<::alloc::Opaque> {
2928-
unsafe {
2929-
NonNull::new_unchecked(self.as_ptr() as _)
2930-
}
2931-
}
29322924
}
29332925

29342926
#[stable(feature = "nonnull", since = "1.25.0")]

src/libstd/collections/hash/table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ unsafe impl<#[may_dangle] K, #[may_dangle] V> Drop for RawTable<K, V> {
12041204
debug_assert!(!oflo, "should be impossible");
12051205

12061206
unsafe {
1207-
Global.dealloc(NonNull::new_unchecked(self.hashes.ptr()).as_opaque(),
1207+
Global.dealloc(NonNull::new_unchecked(self.hashes.ptr()).cast(),
12081208
Layout::from_size_align(size, align).unwrap());
12091209
// Remember how everything was allocated out of one buffer
12101210
// during initialization? We only need one call to free here.

0 commit comments

Comments
 (0)