Skip to content

Commit d9d02fa

Browse files
committed
Changing the alloc() to accept &self instead of &mut self
1 parent fb1dc34 commit d9d02fa

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

library/alloc/src/alloc.rs

+2-2
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,
@@ -208,7 +208,7 @@ 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

library/alloc/src/raw_vec/tests.rs

+8-7
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,17 +18,17 @@ 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.update(|old| old - size);
3132
ok
3233
}
3334
err @ Err(_) => err,
@@ -38,11 +39,11 @@ fn allocator_param() {
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/core/src/alloc/mod.rs

+2-2
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
///
@@ -348,7 +348,7 @@ 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

library/std/src/alloc.rs

+2-2
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,
@@ -202,7 +202,7 @@ 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

src/test/ui/allocator/custom.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct A;
1818
unsafe impl alloc::GlobalAlloc for A {
1919
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
2020
HITS.fetch_add(1, Ordering::SeqCst);
21-
System.alloc(layout)
21+
AllocRef::alloc(&System, layout).unwrap().as_mut_ptr()
2222
}
2323

2424
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {

0 commit comments

Comments
 (0)