Skip to content

Commit bf6a46d

Browse files
committed
Make fields in MemoryBlock public
1 parent db15fe6 commit bf6a46d

File tree

13 files changed

+79
-100
lines changed

13 files changed

+79
-100
lines changed

src/liballoc/alloc.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ unsafe impl AllocRef for Global {
169169
unsafe {
170170
let size = layout.size();
171171
if size == 0 {
172-
Ok(MemoryBlock::new(layout.dangling(), 0))
172+
Ok(MemoryBlock { ptr: layout.dangling(), size: 0 })
173173
} else {
174174
let raw_ptr = match init {
175175
AllocInit::Uninitialized => alloc(layout),
176176
AllocInit::Zeroed => alloc_zeroed(layout),
177177
};
178178
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
179-
Ok(MemoryBlock::new(ptr, size))
179+
Ok(MemoryBlock { ptr, size })
180180
}
181181
}
182182
}
@@ -197,14 +197,14 @@ unsafe impl AllocRef for Global {
197197
placement: ReallocPlacement,
198198
init: AllocInit,
199199
) -> Result<MemoryBlock, AllocErr> {
200-
let old_size = layout.size();
200+
let size = layout.size();
201201
debug_assert!(
202-
new_size >= old_size,
202+
new_size >= size,
203203
"`new_size` must be greater than or equal to `memory.size()`"
204204
);
205205

206-
if old_size == new_size {
207-
return Ok(MemoryBlock::new(ptr, old_size));
206+
if size == new_size {
207+
return Ok(MemoryBlock { ptr, size });
208208
}
209209

210210
match placement {
@@ -215,10 +215,11 @@ unsafe impl AllocRef for Global {
215215
}
216216
ReallocPlacement::MayMove => {
217217
// `realloc` probably checks for `new_size > old_size` or something similar.
218-
intrinsics::assume(new_size > old_size);
218+
intrinsics::assume(new_size > size);
219219
let ptr = realloc(ptr.as_ptr(), layout, new_size);
220-
let mut memory = MemoryBlock::new(NonNull::new(ptr).ok_or(AllocErr)?, new_size);
221-
memory.init_offset(init, old_size);
220+
let mut memory =
221+
MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size };
222+
memory.init_offset(init, size);
222223
Ok(memory)
223224
}
224225
}
@@ -232,27 +233,27 @@ unsafe impl AllocRef for Global {
232233
new_size: usize,
233234
placement: ReallocPlacement,
234235
) -> Result<MemoryBlock, AllocErr> {
235-
let old_size = layout.size();
236+
let size = layout.size();
236237
debug_assert!(
237-
new_size <= old_size,
238+
new_size <= size,
238239
"`new_size` must be smaller than or equal to `memory.size()`"
239240
);
240241

241-
if old_size == new_size {
242-
return Ok(MemoryBlock::new(ptr, old_size));
242+
if size == new_size {
243+
return Ok(MemoryBlock { ptr, size });
243244
}
244245

245246
match placement {
246247
ReallocPlacement::InPlace => Err(AllocErr),
247248
ReallocPlacement::MayMove if new_size == 0 => {
248249
self.dealloc(ptr, layout);
249-
Ok(MemoryBlock::new(layout.dangling(), 0))
250+
Ok(MemoryBlock { ptr: layout.dangling(), size: 0 })
250251
}
251252
ReallocPlacement::MayMove => {
252253
// `realloc` probably checks for `new_size < old_size` or something similar.
253-
intrinsics::assume(new_size < old_size);
254+
intrinsics::assume(new_size < size);
254255
let ptr = realloc(ptr.as_ptr(), layout, new_size);
255-
Ok(MemoryBlock::new(NonNull::new(ptr).ok_or(AllocErr)?, new_size))
256+
Ok(MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size })
256257
}
257258
}
258259
}
@@ -266,7 +267,7 @@ unsafe impl AllocRef for Global {
266267
unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
267268
let layout = Layout::from_size_align_unchecked(size, align);
268269
match Global.alloc(layout, AllocInit::Uninitialized) {
269-
Ok(memory) => memory.ptr().as_ptr(),
270+
Ok(memory) => memory.ptr.as_ptr(),
270271
Err(_) => handle_alloc_error(layout),
271272
}
272273
}

src/liballoc/alloc/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ fn allocate_zeroed() {
1212
.alloc(layout.clone(), AllocInit::Zeroed)
1313
.unwrap_or_else(|_| handle_alloc_error(layout));
1414

15-
let mut i = memory.ptr().cast::<u8>().as_ptr();
15+
let mut i = memory.ptr.cast::<u8>().as_ptr();
1616
let end = i.add(layout.size());
1717
while i < end {
1818
assert_eq!(*i, 0);
1919
i = i.offset(1);
2020
}
21-
Global.dealloc(memory.ptr(), layout);
21+
Global.dealloc(memory.ptr, layout);
2222
}
2323
}
2424

src/liballoc/boxed.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl<T> Box<T> {
198198
let ptr = Global
199199
.alloc(layout, AllocInit::Uninitialized)
200200
.unwrap_or_else(|_| alloc::handle_alloc_error(layout))
201-
.ptr()
201+
.ptr
202202
.cast();
203203
unsafe { Box::from_raw(ptr.as_ptr()) }
204204
}
@@ -227,7 +227,7 @@ impl<T> Box<T> {
227227
let ptr = Global
228228
.alloc(layout, AllocInit::Zeroed)
229229
.unwrap_or_else(|_| alloc::handle_alloc_error(layout))
230-
.ptr()
230+
.ptr
231231
.cast();
232232
unsafe { Box::from_raw(ptr.as_ptr()) }
233233
}

src/liballoc/raw_vec.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
152152

153153
let memory = alloc.alloc(layout, init).unwrap_or_else(|_| handle_alloc_error(layout));
154154
Self {
155-
ptr: memory.ptr().cast().into(),
156-
cap: Self::capacity_from_bytes(memory.size()),
155+
ptr: memory.ptr.cast().into(),
156+
cap: Self::capacity_from_bytes(memory.size),
157157
alloc,
158158
}
159159
}
@@ -470,8 +470,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
470470
}
471471

472472
fn set_memory(&mut self, memory: MemoryBlock) {
473-
self.ptr = memory.ptr().cast().into();
474-
self.cap = Self::capacity_from_bytes(memory.size());
473+
self.ptr = memory.ptr.cast().into();
474+
self.cap = Self::capacity_from_bytes(memory.size);
475475
}
476476

477477
/// Single method to handle all possibilities of growing the buffer.

src/liballoc/rc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ impl<T: ?Sized> Rc<T> {
941941
.unwrap_or_else(|_| handle_alloc_error(layout));
942942

943943
// Initialize the RcBox
944-
let inner = mem_to_rcbox(mem.ptr().as_ptr());
944+
let inner = mem_to_rcbox(mem.ptr.as_ptr());
945945
debug_assert_eq!(Layout::for_value(&*inner), layout);
946946

947947
ptr::write(&mut (*inner).strong, Cell::new(1));

src/liballoc/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ impl<T: ?Sized> Arc<T> {
819819
.unwrap_or_else(|_| handle_alloc_error(layout));
820820

821821
// Initialize the ArcInner
822-
let inner = mem_to_arcinner(mem.ptr().as_ptr());
822+
let inner = mem_to_arcinner(mem.ptr.as_ptr());
823823
debug_assert_eq!(Layout::for_value(&*inner), layout);
824824

825825
ptr::write(&mut (*inner).strong, atomic::AtomicUsize::new(1));

src/liballoc/tests/heap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn check_overalign_requests<T: AllocRef>(mut allocator: T) {
2626
AllocInit::Uninitialized,
2727
)
2828
.unwrap()
29-
.ptr()
29+
.ptr
3030
})
3131
.collect();
3232
for &ptr in &pointers {

src/libcore/alloc/mod.rs

+20-43
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,14 @@ pub enum AllocInit {
4242
}
4343

4444
/// Represents a block of allocated memory returned by an allocator.
45-
#[derive(Debug)]
45+
#[derive(Debug, Copy, Clone)]
4646
#[unstable(feature = "allocator_api", issue = "32838")]
4747
pub struct MemoryBlock {
48-
ptr: NonNull<u8>,
49-
size: usize,
48+
pub ptr: NonNull<u8>,
49+
pub size: usize,
5050
}
5151

5252
impl MemoryBlock {
53-
/// Creates a new `MemoryBlock` from the specified `ptr` and `size`.
54-
#[inline]
55-
#[unstable(feature = "allocator_api", issue = "32838")]
56-
pub const fn new(ptr: NonNull<u8>, size: usize) -> Self {
57-
Self { ptr, size }
58-
}
59-
60-
/// Acquires the underlying `NonNull<u8>` pointer.
61-
#[inline]
62-
#[unstable(feature = "allocator_api", issue = "32838")]
63-
pub const fn ptr(&self) -> NonNull<u8> {
64-
self.ptr
65-
}
66-
67-
/// Returns the size of the memory block.
68-
#[inline]
69-
#[unstable(feature = "allocator_api", issue = "32838")]
70-
pub const fn size(&self) -> usize {
71-
self.size
72-
}
73-
7453
/// Initialize the memory block like specified by `init`.
7554
///
7655
/// This behaves like calling [`MemoryBlock::initialize_offset(ptr, layout, 0)`][off].
@@ -98,12 +77,10 @@ impl MemoryBlock {
9877
#[inline]
9978
#[unstable(feature = "allocator_api", issue = "32838")]
10079
pub unsafe fn init_offset(&mut self, init: AllocInit, offset: usize) {
101-
debug_assert!(offset <= self.size(), "`offset` must be smaller than or equal to `size()`");
80+
debug_assert!(offset <= self.size, "`offset` must be smaller than or equal to `size()`");
10281
match init {
10382
AllocInit::Uninitialized => (),
104-
AllocInit::Zeroed => {
105-
self.ptr().as_ptr().add(offset).write_bytes(0, self.size() - offset)
106-
}
83+
AllocInit::Zeroed => self.ptr.as_ptr().add(offset).write_bytes(0, self.size - offset),
10784
}
10885
}
10986
}
@@ -246,9 +223,9 @@ pub unsafe trait AllocRef {
246223
///
247224
/// * `ptr` must be [*currently allocated*] via this allocator,
248225
/// * `layout` must [*fit*] the `ptr`. (The `new_size` argument need not fit it.)
249-
// We can't require that `new_size` is strictly greater than `memory.size()` because of ZSTs.
226+
// We can't require that `new_size` is strictly greater than `memory.size` because of ZSTs.
250227
// An alternative would be
251-
// * `new_size must be strictly greater than `memory.size()` or both are zero
228+
// * `new_size must be strictly greater than `memory.size` or both are zero
252229
/// * `new_size` must be greater than or equal to `layout.size()`
253230
/// * `new_size`, when rounded up to the nearest multiple of `layout.align()`, must not overflow
254231
/// (i.e., the rounded value must be less than `usize::MAX`).
@@ -280,19 +257,19 @@ pub unsafe trait AllocRef {
280257
match placement {
281258
ReallocPlacement::InPlace => Err(AllocErr),
282259
ReallocPlacement::MayMove => {
283-
let old_size = layout.size();
260+
let size = layout.size();
284261
debug_assert!(
285-
new_size >= old_size,
262+
new_size >= size,
286263
"`new_size` must be greater than or equal to `layout.size()`"
287264
);
288265

289-
if new_size == old_size {
290-
return Ok(MemoryBlock::new(ptr, old_size));
266+
if new_size == size {
267+
return Ok(MemoryBlock { ptr, size });
291268
}
292269

293270
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
294271
let new_memory = self.alloc(new_layout, init)?;
295-
ptr::copy_nonoverlapping(ptr.as_ptr(), new_memory.ptr().as_ptr(), old_size);
272+
ptr::copy_nonoverlapping(ptr.as_ptr(), new_memory.ptr.as_ptr(), size);
296273
self.dealloc(ptr, layout);
297274
Ok(new_memory)
298275
}
@@ -324,10 +301,10 @@ pub unsafe trait AllocRef {
324301
///
325302
/// * `ptr` must be [*currently allocated*] via this allocator,
326303
/// * `layout` must [*fit*] the `ptr`. (The `new_size` argument need not fit it.)
327-
// We can't require that `new_size` is strictly smaller than `memory.size()` because of ZSTs.
304+
// We can't require that `new_size` is strictly smaller than `memory.size` because of ZSTs.
328305
// An alternative would be
329-
// * `new_size must be strictly smaller than `memory.size()` or both are zero
330-
/// * `new_size` must be smaller than or equal to `memory.size()`
306+
// * `new_size must be strictly smaller than `memory.size` or both are zero
307+
/// * `new_size` must be smaller than or equal to `layout.size()`
331308
///
332309
/// [*currently allocated*]: #currently-allocated-memory
333310
/// [*fit*]: #memory-fitting
@@ -355,19 +332,19 @@ pub unsafe trait AllocRef {
355332
match placement {
356333
ReallocPlacement::InPlace => Err(AllocErr),
357334
ReallocPlacement::MayMove => {
358-
let old_size = layout.size();
335+
let size = layout.size();
359336
debug_assert!(
360-
new_size <= old_size,
337+
new_size <= size,
361338
"`new_size` must be smaller than or equal to `layout.size()`"
362339
);
363340

364-
if new_size == old_size {
365-
return Ok(MemoryBlock::new(ptr, old_size));
341+
if new_size == size {
342+
return Ok(MemoryBlock { ptr, size });
366343
}
367344

368345
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
369346
let new_memory = self.alloc(new_layout, AllocInit::Uninitialized)?;
370-
ptr::copy_nonoverlapping(ptr.as_ptr(), new_memory.ptr().as_ptr(), new_size);
347+
ptr::copy_nonoverlapping(ptr.as_ptr(), new_memory.ptr.as_ptr(), new_size);
371348
self.dealloc(ptr, layout);
372349
Ok(new_memory)
373350
}

src/libstd/alloc.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ unsafe impl AllocRef for System {
143143
unsafe {
144144
let size = layout.size();
145145
if size == 0 {
146-
Ok(MemoryBlock::new(layout.dangling(), 0))
146+
Ok(MemoryBlock { ptr: layout.dangling(), size: 0 })
147147
} else {
148148
let raw_ptr = match init {
149149
AllocInit::Uninitialized => GlobalAlloc::alloc(self, layout),
150150
AllocInit::Zeroed => GlobalAlloc::alloc_zeroed(self, layout),
151151
};
152152
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
153-
Ok(MemoryBlock::new(ptr, size))
153+
Ok(MemoryBlock { ptr, size })
154154
}
155155
}
156156
}
@@ -171,14 +171,14 @@ unsafe impl AllocRef for System {
171171
placement: ReallocPlacement,
172172
init: AllocInit,
173173
) -> Result<MemoryBlock, AllocErr> {
174-
let old_size = layout.size();
174+
let size = layout.size();
175175
debug_assert!(
176-
new_size >= old_size,
176+
new_size >= size,
177177
"`new_size` must be greater than or equal to `memory.size()`"
178178
);
179179

180-
if old_size == new_size {
181-
return Ok(MemoryBlock::new(ptr, old_size));
180+
if size == new_size {
181+
return Ok(MemoryBlock { ptr, size });
182182
}
183183

184184
match placement {
@@ -189,10 +189,11 @@ unsafe impl AllocRef for System {
189189
}
190190
ReallocPlacement::MayMove => {
191191
// `realloc` probably checks for `new_size > old_size` or something similar.
192-
intrinsics::assume(new_size > old_size);
192+
intrinsics::assume(new_size > size);
193193
let ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size);
194-
let mut memory = MemoryBlock::new(NonNull::new(ptr).ok_or(AllocErr)?, new_size);
195-
memory.init_offset(init, old_size);
194+
let mut memory =
195+
MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size };
196+
memory.init_offset(init, size);
196197
Ok(memory)
197198
}
198199
}
@@ -206,27 +207,27 @@ unsafe impl AllocRef for System {
206207
new_size: usize,
207208
placement: ReallocPlacement,
208209
) -> Result<MemoryBlock, AllocErr> {
209-
let old_size = layout.size();
210+
let size = layout.size();
210211
debug_assert!(
211-
new_size <= old_size,
212+
new_size <= size,
212213
"`new_size` must be smaller than or equal to `memory.size()`"
213214
);
214215

215-
if old_size == new_size {
216-
return Ok(MemoryBlock::new(ptr, old_size));
216+
if size == new_size {
217+
return Ok(MemoryBlock { ptr, size });
217218
}
218219

219220
match placement {
220221
ReallocPlacement::InPlace => Err(AllocErr),
221222
ReallocPlacement::MayMove if new_size == 0 => {
222223
self.dealloc(ptr, layout);
223-
Ok(MemoryBlock::new(layout.dangling(), 0))
224+
Ok(MemoryBlock { ptr: layout.dangling(), size: 0 })
224225
}
225226
ReallocPlacement::MayMove => {
226227
// `realloc` probably checks for `new_size < old_size` or something similar.
227-
intrinsics::assume(new_size < old_size);
228+
intrinsics::assume(new_size < size);
228229
let ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size);
229-
Ok(MemoryBlock::new(NonNull::new(ptr).ok_or(AllocErr)?, new_size))
230+
Ok(MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size })
230231
}
231232
}
232233
}

0 commit comments

Comments
 (0)