Skip to content

Commit 03b055b

Browse files
committed
Remove alignment from MemoryBlock
1 parent bfbdb5f commit 03b055b

File tree

14 files changed

+211
-202
lines changed

14 files changed

+211
-202
lines changed

src/liballoc/alloc.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use core::intrinsics::{self, min_align_of_val, size_of_val};
66
use core::ptr::{NonNull, Unique};
7-
use core::{mem, usize};
7+
use core::usize;
88

99
#[stable(feature = "alloc_module", since = "1.28.0")]
1010
#[doc(inline)]
@@ -167,94 +167,94 @@ unsafe impl AllocRef for Global {
167167
#[inline]
168168
fn alloc(&mut self, layout: Layout, init: AllocInit) -> Result<MemoryBlock, AllocErr> {
169169
unsafe {
170-
if layout.size() == 0 {
171-
Ok(MemoryBlock::new(layout.dangling(), layout))
170+
let size = layout.size();
171+
if size == 0 {
172+
Ok(MemoryBlock::new(layout.dangling(), 0))
172173
} else {
173174
let raw_ptr = match init {
174175
AllocInit::Uninitialized => alloc(layout),
175176
AllocInit::Zeroed => alloc_zeroed(layout),
176177
};
177178
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
178-
Ok(MemoryBlock::new(ptr, layout))
179+
Ok(MemoryBlock::new(ptr, size))
179180
}
180181
}
181182
}
182183

183184
#[inline]
184-
unsafe fn dealloc(&mut self, memory: MemoryBlock) {
185-
if memory.size() != 0 {
186-
dealloc(memory.ptr().as_ptr(), memory.layout())
185+
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
186+
if layout.size() != 0 {
187+
dealloc(ptr.as_ptr(), layout)
187188
}
188189
}
189190

190191
#[inline]
191192
unsafe fn grow(
192193
&mut self,
193-
memory: &mut MemoryBlock,
194+
ptr: NonNull<u8>,
195+
layout: Layout,
194196
new_size: usize,
195197
placement: ReallocPlacement,
196198
init: AllocInit,
197-
) -> Result<(), AllocErr> {
198-
let old_size = memory.size();
199+
) -> Result<MemoryBlock, AllocErr> {
200+
let old_size = layout.size();
199201
debug_assert!(
200202
new_size >= old_size,
201203
"`new_size` must be greater than or equal to `memory.size()`"
202204
);
203205

204206
if old_size == new_size {
205-
return Ok(());
207+
return Ok(MemoryBlock::new(ptr, old_size));
206208
}
207209

208-
let new_layout = Layout::from_size_align_unchecked(new_size, memory.align());
209210
match placement {
210-
ReallocPlacement::InPlace => return Err(AllocErr),
211-
ReallocPlacement::MayMove if memory.size() == 0 => {
212-
*memory = self.alloc(new_layout, init)?
211+
ReallocPlacement::InPlace => Err(AllocErr),
212+
ReallocPlacement::MayMove if layout.size() == 0 => {
213+
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
214+
self.alloc(new_layout, init)
213215
}
214216
ReallocPlacement::MayMove => {
215217
// `realloc` probably checks for `new_size > old_size` or something similar.
216218
intrinsics::assume(new_size > old_size);
217-
let ptr = realloc(memory.ptr().as_ptr(), memory.layout(), new_size);
218-
*memory = MemoryBlock::new(NonNull::new(ptr).ok_or(AllocErr)?, new_layout);
219+
let ptr = realloc(ptr.as_ptr(), layout, new_size);
220+
let mut memory = MemoryBlock::new(NonNull::new(ptr).ok_or(AllocErr)?, new_size);
219221
memory.init_offset(init, old_size);
222+
Ok(memory)
220223
}
221224
}
222-
Ok(())
223225
}
224226

225227
#[inline]
226228
unsafe fn shrink(
227229
&mut self,
228-
memory: &mut MemoryBlock,
230+
ptr: NonNull<u8>,
231+
layout: Layout,
229232
new_size: usize,
230233
placement: ReallocPlacement,
231-
) -> Result<(), AllocErr> {
232-
let old_size = memory.size();
234+
) -> Result<MemoryBlock, AllocErr> {
235+
let old_size = layout.size();
233236
debug_assert!(
234237
new_size <= old_size,
235238
"`new_size` must be smaller than or equal to `memory.size()`"
236239
);
237240

238241
if old_size == new_size {
239-
return Ok(());
242+
return Ok(MemoryBlock::new(ptr, old_size));
240243
}
241244

242-
let new_layout = Layout::from_size_align_unchecked(new_size, memory.align());
243245
match placement {
244-
ReallocPlacement::InPlace => return Err(AllocErr),
246+
ReallocPlacement::InPlace => Err(AllocErr),
245247
ReallocPlacement::MayMove if new_size == 0 => {
246-
let new_memory = MemoryBlock::new(new_layout.dangling(), new_layout);
247-
let old_memory = mem::replace(memory, new_memory);
248-
self.dealloc(old_memory)
248+
self.dealloc(ptr, layout);
249+
Ok(MemoryBlock::new(layout.dangling(), 0))
249250
}
250251
ReallocPlacement::MayMove => {
251252
// `realloc` probably checks for `new_size < old_size` or something similar.
252253
intrinsics::assume(new_size < old_size);
253-
let ptr = realloc(memory.ptr().as_ptr(), memory.layout(), new_size);
254-
*memory = MemoryBlock::new(NonNull::new(ptr).ok_or(AllocErr)?, new_layout);
254+
let ptr = realloc(ptr.as_ptr(), layout, new_size);
255+
Ok(MemoryBlock::new(NonNull::new(ptr).ok_or(AllocErr)?, new_size))
255256
}
256257
}
257-
Ok(())
258258
}
259259
}
260260

@@ -282,7 +282,7 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
282282
let size = size_of_val(ptr.as_ref());
283283
let align = min_align_of_val(ptr.as_ref());
284284
let layout = Layout::from_size_align_unchecked(size, align);
285-
Global.dealloc(MemoryBlock::new(ptr.cast().into(), layout))
285+
Global.dealloc(ptr.cast().into(), layout)
286286
}
287287

288288
/// Abort on memory allocation error or failure.

src/liballoc/alloc/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn allocate_zeroed() {
1818
assert_eq!(*i, 0);
1919
i = i.offset(1);
2020
}
21-
Global.dealloc(memory);
21+
Global.dealloc(memory.ptr(), layout);
2222
}
2323
}
2424

src/liballoc/collections/btree/node.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
// - A node of length `n` has `n` keys, `n` values, and (in an internal node) `n + 1` edges.
3232
// This implies that even an empty internal node has at least one edge.
3333

34-
use core::alloc::MemoryBlock;
3534
use core::cmp::Ordering;
3635
use core::marker::PhantomData;
3736
use core::mem::{self, MaybeUninit};
@@ -228,10 +227,7 @@ impl<K, V> Root<K, V> {
228227
}
229228

230229
unsafe {
231-
Global.dealloc(MemoryBlock::new(
232-
NonNull::from(top).cast(),
233-
Layout::new::<InternalNode<K, V>>(),
234-
));
230+
Global.dealloc(NonNull::from(top).cast(), Layout::new::<InternalNode<K, V>>());
235231
}
236232
}
237233
}
@@ -396,14 +392,14 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
396392
let height = self.height;
397393
let node = self.node;
398394
let ret = self.ascend().ok();
399-
Global.dealloc(MemoryBlock::new(
395+
Global.dealloc(
400396
node.cast(),
401397
if height > 0 {
402398
Layout::new::<InternalNode<K, V>>()
403399
} else {
404400
Layout::new::<LeafNode<K, V>>()
405401
},
406-
));
402+
);
407403
ret
408404
}
409405
}
@@ -1167,7 +1163,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
11671163
} else {
11681164
Layout::new::<LeafNode<K, V>>()
11691165
};
1170-
Global.dealloc(MemoryBlock::new(right_node.node.cast(), layout));
1166+
Global.dealloc(right_node.node.cast(), layout);
11711167

11721168
Handle::new_edge(self.node, self.idx)
11731169
}

src/liballoc/raw_vec.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::alloc::MemoryBlock;
55
use core::cmp;
66
use core::mem::{self, MaybeUninit};
77
use core::ops::Drop;
8-
use core::ptr::Unique;
8+
use core::ptr::{NonNull, Unique};
99
use core::slice;
1010

1111
use crate::alloc::{
@@ -197,7 +197,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
197197
&mut self.alloc
198198
}
199199

200-
fn current_memory(&self) -> Option<MemoryBlock> {
200+
fn current_memory(&self) -> Option<(NonNull<u8>, Layout)> {
201201
if mem::size_of::<T>() == 0 || self.cap == 0 {
202202
None
203203
} else {
@@ -207,7 +207,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
207207
let align = mem::align_of::<T>();
208208
let size = mem::size_of::<T>() * self.cap;
209209
let layout = Layout::from_size_align_unchecked(size, align);
210-
Some(MemoryBlock::new(self.ptr.cast().into(), layout))
210+
Some((self.ptr.cast().into(), layout))
211211
}
212212
}
213213
}
@@ -472,7 +472,6 @@ impl<T, A: AllocRef> RawVec<T, A> {
472472
fn set_memory(&mut self, memory: MemoryBlock) {
473473
self.ptr = memory.ptr().cast().into();
474474
self.cap = Self::capacity_from_bytes(memory.size());
475-
drop(memory);
476475
}
477476

478477
/// Single method to handle all possibilities of growing the buffer.
@@ -488,7 +487,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
488487
// 0, getting to here necessarily means the `RawVec` is overfull.
489488
return Err(CapacityOverflow);
490489
}
491-
let layout = match strategy {
490+
let new_layout = match strategy {
492491
Double => unsafe {
493492
// Since we guarantee that we never allocate more than `isize::MAX` bytes,
494493
// `elem_size * self.cap <= isize::MAX` as a precondition, so this can't overflow.
@@ -522,22 +521,20 @@ impl<T, A: AllocRef> RawVec<T, A> {
522521
}
523522
};
524523

525-
let memory = if let Some(mut memory) = self.current_memory() {
526-
debug_assert_eq!(memory.align(), layout.align());
524+
let memory = if let Some((ptr, old_layout)) = self.current_memory() {
525+
debug_assert_eq!(old_layout.align(), new_layout.align());
527526
unsafe {
528527
self.alloc
529-
.grow(&mut memory, layout.size(), placement, init)
530-
.map_err(|_| AllocError { layout, non_exhaustive: () })?
531-
};
532-
memory
528+
.grow(ptr, old_layout, new_layout.size(), placement, init)
529+
.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () })?
530+
}
533531
} else {
534532
match placement {
535-
MayMove => self.alloc.alloc(layout, init),
533+
MayMove => self.alloc.alloc(new_layout, init),
536534
InPlace => Err(AllocErr),
537535
}
538-
.map_err(|_| AllocError { layout, non_exhaustive: () })?
536+
.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () })?
539537
};
540-
541538
self.set_memory(memory);
542539
Ok(())
543540
}
@@ -549,18 +546,17 @@ impl<T, A: AllocRef> RawVec<T, A> {
549546
) -> Result<(), TryReserveError> {
550547
assert!(amount <= self.capacity(), "Tried to shrink to a larger capacity");
551548

552-
let mut memory = if let Some(mem) = self.current_memory() { mem } else { return Ok(()) };
549+
let (ptr, layout) = if let Some(mem) = self.current_memory() { mem } else { return Ok(()) };
553550
let new_size = amount * mem::size_of::<T>();
554551

555-
unsafe {
556-
self.alloc.shrink(&mut memory, new_size, placement).map_err(|_| {
552+
let memory = unsafe {
553+
self.alloc.shrink(ptr, layout, new_size, placement).map_err(|_| {
557554
TryReserveError::AllocError {
558-
layout: Layout::from_size_align_unchecked(new_size, memory.align()),
555+
layout: Layout::from_size_align_unchecked(new_size, layout.align()),
559556
non_exhaustive: (),
560557
}
561-
})?;
562-
}
563-
558+
})?
559+
};
564560
self.set_memory(memory);
565561
Ok(())
566562
}
@@ -593,8 +589,8 @@ impl<T> RawVec<T, Global> {
593589
unsafe impl<#[may_dangle] T, A: AllocRef> Drop for RawVec<T, A> {
594590
/// Frees the memory owned by the `RawVec` *without* trying to drop its contents.
595591
fn drop(&mut self) {
596-
if let Some(memory) = self.current_memory() {
597-
unsafe { self.alloc.dealloc(memory) }
592+
if let Some((ptr, layout)) = self.current_memory() {
593+
unsafe { self.alloc.dealloc(ptr, layout) }
598594
}
599595
}
600596
}

src/liballoc/raw_vec/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ fn allocator_param() {
3434
err @ Err(_) => err,
3535
}
3636
}
37-
unsafe fn dealloc(&mut self, memory: MemoryBlock) {
38-
Global.dealloc(memory)
37+
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
38+
Global.dealloc(ptr, layout)
3939
}
4040
}
4141

src/liballoc/rc.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ use crate::boxed::Box;
234234
#[cfg(test)]
235235
use std::boxed::Box;
236236

237-
use core::alloc::MemoryBlock;
238237
use core::any::Any;
239238
use core::array::LengthAtMost32;
240239
use core::borrow;
@@ -1032,7 +1031,7 @@ impl<T> Rc<[T]> {
10321031
let slice = from_raw_parts_mut(self.elems, self.n_elems);
10331032
ptr::drop_in_place(slice);
10341033

1035-
Global.dealloc(MemoryBlock::new(self.mem, self.layout));
1034+
Global.dealloc(self.mem, self.layout);
10361035
}
10371036
}
10381037
}
@@ -1132,10 +1131,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Rc<T> {
11321131
self.dec_weak();
11331132

11341133
if self.weak() == 0 {
1135-
Global.dealloc(MemoryBlock::new(
1136-
self.ptr.cast(),
1137-
Layout::for_value(self.ptr.as_ref()),
1138-
));
1134+
Global.dealloc(self.ptr.cast(), Layout::for_value(self.ptr.as_ref()));
11391135
}
11401136
}
11411137
}
@@ -1943,10 +1939,7 @@ impl<T: ?Sized> Drop for Weak<T> {
19431939
// the strong pointers have disappeared.
19441940
if inner.weak() == 0 {
19451941
unsafe {
1946-
Global.dealloc(MemoryBlock::new(
1947-
self.ptr.cast(),
1948-
Layout::for_value(self.ptr.as_ref()),
1949-
));
1942+
Global.dealloc(self.ptr.cast(), Layout::for_value(self.ptr.as_ref()));
19501943
}
19511944
}
19521945
}

src/liballoc/sync.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//!
77
//! [arc]: struct.Arc.html
88
9-
use core::alloc::MemoryBlock;
109
use core::any::Any;
1110
use core::array::LengthAtMost32;
1211
use core::borrow;
@@ -771,7 +770,7 @@ impl<T: ?Sized> Arc<T> {
771770

772771
if self.inner().weak.fetch_sub(1, Release) == 1 {
773772
acquire!(self.inner().weak);
774-
Global.dealloc(MemoryBlock::new(self.ptr.cast(), Layout::for_value(self.ptr.as_ref())))
773+
Global.dealloc(self.ptr.cast(), Layout::for_value(self.ptr.as_ref()))
775774
}
776775
}
777776

@@ -910,7 +909,7 @@ impl<T> Arc<[T]> {
910909
let slice = from_raw_parts_mut(self.elems, self.n_elems);
911910
ptr::drop_in_place(slice);
912911

913-
Global.dealloc(MemoryBlock::new(self.mem.cast(), self.layout));
912+
Global.dealloc(self.mem.cast(), self.layout);
914913
}
915914
}
916915
}
@@ -1735,12 +1734,7 @@ impl<T: ?Sized> Drop for Weak<T> {
17351734

17361735
if inner.weak.fetch_sub(1, Release) == 1 {
17371736
acquire!(inner.weak);
1738-
unsafe {
1739-
Global.dealloc(MemoryBlock::new(
1740-
self.ptr.cast(),
1741-
Layout::for_value(self.ptr.as_ref()),
1742-
))
1743-
}
1737+
unsafe { Global.dealloc(self.ptr.cast(), Layout::for_value(self.ptr.as_ref())) }
17441738
}
17451739
}
17461740
}

0 commit comments

Comments
 (0)