Skip to content

Commit a40d79c

Browse files
authored
Rollup merge of #76993 - blitzerr:alloc-ref, r=Amanieu
Changing the alloc() to accept &self instead of &mut self Fixes: [#55](rust-lang/wg-allocators#55) This is the first cut. It only makes the change for `alloc` method.
2 parents eaaf5d7 + 2b19b14 commit a40d79c

File tree

7 files changed

+48
-46
lines changed

7 files changed

+48
-46
lines changed

library/alloc/src/alloc.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
145145

146146
impl Global {
147147
#[inline]
148-
fn alloc_impl(&mut self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocErr> {
148+
fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocErr> {
149149
match layout.size() {
150150
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
151151
// SAFETY: `layout` is non-zero in size,
@@ -160,7 +160,7 @@ impl Global {
160160
// SAFETY: Same as `AllocRef::grow`
161161
#[inline]
162162
unsafe fn grow_impl(
163-
&mut self,
163+
&self,
164164
ptr: NonNull<u8>,
165165
old_layout: Layout,
166166
new_layout: Layout,
@@ -208,17 +208,17 @@ impl Global {
208208
#[unstable(feature = "allocator_api", issue = "32838")]
209209
unsafe impl AllocRef for Global {
210210
#[inline]
211-
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
211+
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
212212
self.alloc_impl(layout, false)
213213
}
214214

215215
#[inline]
216-
fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
216+
fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
217217
self.alloc_impl(layout, true)
218218
}
219219

220220
#[inline]
221-
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
221+
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
222222
if layout.size() != 0 {
223223
// SAFETY: `layout` is non-zero in size,
224224
// other conditions must be upheld by the caller
@@ -228,7 +228,7 @@ unsafe impl AllocRef for Global {
228228

229229
#[inline]
230230
unsafe fn grow(
231-
&mut self,
231+
&self,
232232
ptr: NonNull<u8>,
233233
old_layout: Layout,
234234
new_layout: Layout,
@@ -239,7 +239,7 @@ unsafe impl AllocRef for Global {
239239

240240
#[inline]
241241
unsafe fn grow_zeroed(
242-
&mut self,
242+
&self,
243243
ptr: NonNull<u8>,
244244
old_layout: Layout,
245245
new_layout: Layout,
@@ -250,7 +250,7 @@ unsafe impl AllocRef for Global {
250250

251251
#[inline]
252252
unsafe fn shrink(
253-
&mut self,
253+
&self,
254254
ptr: NonNull<u8>,
255255
old_layout: Layout,
256256
new_layout: Layout,

library/alloc/src/raw_vec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
170170
Self::allocate_in(capacity, AllocInit::Zeroed, alloc)
171171
}
172172

173-
fn allocate_in(capacity: usize, init: AllocInit, mut alloc: A) -> Self {
173+
fn allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Self {
174174
if mem::size_of::<T>() == 0 {
175175
Self::new_in(alloc)
176176
} else {

library/alloc/src/raw_vec/tests.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::*;
2+
use std::cell::Cell;
23

34
#[test]
45
fn allocator_param() {
@@ -17,32 +18,32 @@ fn allocator_param() {
1718
// A dumb allocator that consumes a fixed amount of fuel
1819
// before allocation attempts start failing.
1920
struct BoundedAlloc {
20-
fuel: usize,
21+
fuel: Cell<usize>,
2122
}
2223
unsafe impl AllocRef for BoundedAlloc {
23-
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
24+
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
2425
let size = layout.size();
25-
if size > self.fuel {
26+
if size > self.fuel.get() {
2627
return Err(AllocErr);
2728
}
2829
match Global.alloc(layout) {
2930
ok @ Ok(_) => {
30-
self.fuel -= size;
31+
self.fuel.set(self.fuel.get() - size);
3132
ok
3233
}
3334
err @ Err(_) => err,
3435
}
3536
}
36-
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
37+
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
3738
unsafe { Global.dealloc(ptr, layout) }
3839
}
3940
}
4041

41-
let a = BoundedAlloc { fuel: 500 };
42+
let a = BoundedAlloc { fuel: Cell::new(500) };
4243
let mut v: RawVec<u8, _> = RawVec::with_capacity_in(50, a);
43-
assert_eq!(v.alloc.fuel, 450);
44+
assert_eq!(v.alloc.fuel.get(), 450);
4445
v.reserve(50, 150); // (causes a realloc, thus using 50 + 150 = 200 units of fuel)
45-
assert_eq!(v.alloc.fuel, 250);
46+
assert_eq!(v.alloc.fuel.get(), 250);
4647
}
4748

4849
#[test]

library/alloc/tests/heap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn std_heap_overaligned_request() {
1111
check_overalign_requests(Global)
1212
}
1313

14-
fn check_overalign_requests<T: AllocRef>(mut allocator: T) {
14+
fn check_overalign_requests<T: AllocRef>(allocator: T) {
1515
for &align in &[4, 8, 16, 32] {
1616
// less than and bigger than `MIN_ALIGN`
1717
for &size in &[align / 2, align - 1] {

library/core/src/alloc/mod.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub unsafe trait AllocRef {
109109
/// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
110110
///
111111
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
112-
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr>;
112+
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr>;
113113

114114
/// Behaves like `alloc`, but also ensures that the returned memory is zero-initialized.
115115
///
@@ -126,7 +126,7 @@ pub unsafe trait AllocRef {
126126
/// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
127127
///
128128
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
129-
fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
129+
fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
130130
let ptr = self.alloc(layout)?;
131131
// SAFETY: `alloc` returns a valid memory block
132132
unsafe { ptr.as_non_null_ptr().as_ptr().write_bytes(0, ptr.len()) }
@@ -142,7 +142,7 @@ pub unsafe trait AllocRef {
142142
///
143143
/// [*currently allocated*]: #currently-allocated-memory
144144
/// [*fit*]: #memory-fitting
145-
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout);
145+
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout);
146146

147147
/// Attempts to extend the memory block.
148148
///
@@ -183,7 +183,7 @@ pub unsafe trait AllocRef {
183183
///
184184
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
185185
unsafe fn grow(
186-
&mut self,
186+
&self,
187187
ptr: NonNull<u8>,
188188
old_layout: Layout,
189189
new_layout: Layout,
@@ -244,7 +244,7 @@ pub unsafe trait AllocRef {
244244
///
245245
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
246246
unsafe fn grow_zeroed(
247-
&mut self,
247+
&self,
248248
ptr: NonNull<u8>,
249249
old_layout: Layout,
250250
new_layout: Layout,
@@ -308,7 +308,7 @@ pub unsafe trait AllocRef {
308308
///
309309
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
310310
unsafe fn shrink(
311-
&mut self,
311+
&self,
312312
ptr: NonNull<u8>,
313313
old_layout: Layout,
314314
new_layout: Layout,
@@ -337,35 +337,35 @@ pub unsafe trait AllocRef {
337337
///
338338
/// The returned adaptor also implements `AllocRef` and will simply borrow this.
339339
#[inline(always)]
340-
fn by_ref(&mut self) -> &mut Self {
340+
fn by_ref(&mut self) -> &Self {
341341
self
342342
}
343343
}
344344

345345
#[unstable(feature = "allocator_api", issue = "32838")]
346-
unsafe impl<A> AllocRef for &mut A
346+
unsafe impl<A> AllocRef for &A
347347
where
348348
A: AllocRef + ?Sized,
349349
{
350350
#[inline]
351-
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
351+
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
352352
(**self).alloc(layout)
353353
}
354354

355355
#[inline]
356-
fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
356+
fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
357357
(**self).alloc_zeroed(layout)
358358
}
359359

360360
#[inline]
361-
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
361+
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
362362
// SAFETY: the safety contract must be upheld by the caller
363363
unsafe { (**self).dealloc(ptr, layout) }
364364
}
365365

366366
#[inline]
367367
unsafe fn grow(
368-
&mut self,
368+
&self,
369369
ptr: NonNull<u8>,
370370
old_layout: Layout,
371371
new_layout: Layout,
@@ -376,7 +376,7 @@ where
376376

377377
#[inline]
378378
unsafe fn grow_zeroed(
379-
&mut self,
379+
&self,
380380
ptr: NonNull<u8>,
381381
old_layout: Layout,
382382
new_layout: Layout,
@@ -387,7 +387,7 @@ where
387387

388388
#[inline]
389389
unsafe fn shrink(
390-
&mut self,
390+
&self,
391391
ptr: NonNull<u8>,
392392
old_layout: Layout,
393393
new_layout: Layout,

library/std/src/alloc.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub struct System;
133133

134134
impl System {
135135
#[inline]
136-
fn alloc_impl(&mut self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocErr> {
136+
fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocErr> {
137137
match layout.size() {
138138
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
139139
// SAFETY: `layout` is non-zero in size,
@@ -152,7 +152,7 @@ impl System {
152152
// SAFETY: Same as `AllocRef::grow`
153153
#[inline]
154154
unsafe fn grow_impl(
155-
&mut self,
155+
&self,
156156
ptr: NonNull<u8>,
157157
old_layout: Layout,
158158
new_layout: Layout,
@@ -190,7 +190,7 @@ impl System {
190190
old_size => unsafe {
191191
let new_ptr = self.alloc_impl(new_layout, zeroed)?;
192192
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), old_size);
193-
self.dealloc(ptr, old_layout);
193+
AllocRef::dealloc(&self, ptr, old_layout);
194194
Ok(new_ptr)
195195
},
196196
}
@@ -202,17 +202,17 @@ impl System {
202202
#[unstable(feature = "allocator_api", issue = "32838")]
203203
unsafe impl AllocRef for System {
204204
#[inline]
205-
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
205+
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
206206
self.alloc_impl(layout, false)
207207
}
208208

209209
#[inline]
210-
fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
210+
fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
211211
self.alloc_impl(layout, true)
212212
}
213213

214214
#[inline]
215-
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
215+
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
216216
if layout.size() != 0 {
217217
// SAFETY: `layout` is non-zero in size,
218218
// other conditions must be upheld by the caller
@@ -222,7 +222,7 @@ unsafe impl AllocRef for System {
222222

223223
#[inline]
224224
unsafe fn grow(
225-
&mut self,
225+
&self,
226226
ptr: NonNull<u8>,
227227
old_layout: Layout,
228228
new_layout: Layout,
@@ -233,7 +233,7 @@ unsafe impl AllocRef for System {
233233

234234
#[inline]
235235
unsafe fn grow_zeroed(
236-
&mut self,
236+
&self,
237237
ptr: NonNull<u8>,
238238
old_layout: Layout,
239239
new_layout: Layout,
@@ -244,7 +244,7 @@ unsafe impl AllocRef for System {
244244

245245
#[inline]
246246
unsafe fn shrink(
247-
&mut self,
247+
&self,
248248
ptr: NonNull<u8>,
249249
old_layout: Layout,
250250
new_layout: Layout,
@@ -257,7 +257,7 @@ unsafe impl AllocRef for System {
257257
match new_layout.size() {
258258
// SAFETY: conditions must be upheld by the caller
259259
0 => unsafe {
260-
self.dealloc(ptr, old_layout);
260+
AllocRef::dealloc(&self, ptr, old_layout);
261261
Ok(NonNull::slice_from_raw_parts(new_layout.dangling(), 0))
262262
},
263263

@@ -277,9 +277,9 @@ unsafe impl AllocRef for System {
277277
// `new_ptr`. Thus, the call to `copy_nonoverlapping` is safe. The safety contract
278278
// for `dealloc` must be upheld by the caller.
279279
new_size => unsafe {
280-
let new_ptr = self.alloc(new_layout)?;
280+
let new_ptr = AllocRef::alloc(&self, new_layout)?;
281281
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), new_size);
282-
self.dealloc(ptr, old_layout);
282+
AllocRef::dealloc(&self, ptr, old_layout);
283283
Ok(new_ptr)
284284
},
285285
}

src/test/ui/allocator/custom.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern crate helper;
1010

1111
use std::alloc::{self, AllocRef, Global, Layout, System};
1212
use std::sync::atomic::{AtomicUsize, Ordering};
13+
use std::ptr::NonNull;
1314

1415
static HITS: AtomicUsize = AtomicUsize::new(0);
1516

@@ -18,12 +19,12 @@ struct A;
1819
unsafe impl alloc::GlobalAlloc for A {
1920
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
2021
HITS.fetch_add(1, Ordering::SeqCst);
21-
System.alloc(layout)
22+
alloc::GlobalAlloc::alloc(&System, layout)
2223
}
2324

2425
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
2526
HITS.fetch_add(1, Ordering::SeqCst);
26-
System.dealloc(ptr, layout)
27+
AllocRef::dealloc(&System, NonNull::new(ptr).unwrap(), layout)
2728
}
2829
}
2930

0 commit comments

Comments
 (0)