Changing the alloc() to accept &self instead of &mut self
This commit is contained in:
parent
fb1dc34a83
commit
d9d02fa168
5 changed files with 15 additions and 14 deletions
|
@ -145,7 +145,7 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
|
||||||
|
|
||||||
impl Global {
|
impl Global {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn alloc_impl(&mut self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocErr> {
|
fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocErr> {
|
||||||
match layout.size() {
|
match layout.size() {
|
||||||
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
|
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
|
||||||
// SAFETY: `layout` is non-zero in size,
|
// SAFETY: `layout` is non-zero in size,
|
||||||
|
@ -208,7 +208,7 @@ impl Global {
|
||||||
#[unstable(feature = "allocator_api", issue = "32838")]
|
#[unstable(feature = "allocator_api", issue = "32838")]
|
||||||
unsafe impl AllocRef for Global {
|
unsafe impl AllocRef for Global {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
|
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
|
||||||
self.alloc_impl(layout, false)
|
self.alloc_impl(layout, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use std::cell::Cell;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn allocator_param() {
|
fn allocator_param() {
|
||||||
|
@ -17,17 +18,17 @@ fn allocator_param() {
|
||||||
// A dumb allocator that consumes a fixed amount of fuel
|
// A dumb allocator that consumes a fixed amount of fuel
|
||||||
// before allocation attempts start failing.
|
// before allocation attempts start failing.
|
||||||
struct BoundedAlloc {
|
struct BoundedAlloc {
|
||||||
fuel: usize,
|
fuel: Cell<usize>,
|
||||||
}
|
}
|
||||||
unsafe impl AllocRef for BoundedAlloc {
|
unsafe impl AllocRef for BoundedAlloc {
|
||||||
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
|
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
|
||||||
let size = layout.size();
|
let size = layout.size();
|
||||||
if size > self.fuel {
|
if size > self.fuel.get() {
|
||||||
return Err(AllocErr);
|
return Err(AllocErr);
|
||||||
}
|
}
|
||||||
match Global.alloc(layout) {
|
match Global.alloc(layout) {
|
||||||
ok @ Ok(_) => {
|
ok @ Ok(_) => {
|
||||||
self.fuel -= size;
|
self.fuel.update(|old| old - size);
|
||||||
ok
|
ok
|
||||||
}
|
}
|
||||||
err @ Err(_) => err,
|
err @ Err(_) => err,
|
||||||
|
@ -38,11 +39,11 @@ fn allocator_param() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let a = BoundedAlloc { fuel: 500 };
|
let a = BoundedAlloc { fuel: Cell::new(500) };
|
||||||
let mut v: RawVec<u8, _> = RawVec::with_capacity_in(50, a);
|
let mut v: RawVec<u8, _> = RawVec::with_capacity_in(50, a);
|
||||||
assert_eq!(v.alloc.fuel, 450);
|
assert_eq!(v.alloc.fuel.get(), 450);
|
||||||
v.reserve(50, 150); // (causes a realloc, thus using 50 + 150 = 200 units of fuel)
|
v.reserve(50, 150); // (causes a realloc, thus using 50 + 150 = 200 units of fuel)
|
||||||
assert_eq!(v.alloc.fuel, 250);
|
assert_eq!(v.alloc.fuel.get(), 250);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -109,7 +109,7 @@ pub unsafe trait AllocRef {
|
||||||
/// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
|
/// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
|
||||||
///
|
///
|
||||||
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||||
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr>;
|
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr>;
|
||||||
|
|
||||||
/// Behaves like `alloc`, but also ensures that the returned memory is zero-initialized.
|
/// Behaves like `alloc`, but also ensures that the returned memory is zero-initialized.
|
||||||
///
|
///
|
||||||
|
@ -348,7 +348,7 @@ where
|
||||||
A: AllocRef + ?Sized,
|
A: AllocRef + ?Sized,
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
|
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
|
||||||
(**self).alloc(layout)
|
(**self).alloc(layout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -133,7 +133,7 @@ pub struct System;
|
||||||
|
|
||||||
impl System {
|
impl System {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn alloc_impl(&mut self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocErr> {
|
fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocErr> {
|
||||||
match layout.size() {
|
match layout.size() {
|
||||||
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
|
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
|
||||||
// SAFETY: `layout` is non-zero in size,
|
// SAFETY: `layout` is non-zero in size,
|
||||||
|
@ -202,7 +202,7 @@ impl System {
|
||||||
#[unstable(feature = "allocator_api", issue = "32838")]
|
#[unstable(feature = "allocator_api", issue = "32838")]
|
||||||
unsafe impl AllocRef for System {
|
unsafe impl AllocRef for System {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
|
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
|
||||||
self.alloc_impl(layout, false)
|
self.alloc_impl(layout, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ struct A;
|
||||||
unsafe impl alloc::GlobalAlloc for A {
|
unsafe impl alloc::GlobalAlloc for A {
|
||||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||||
HITS.fetch_add(1, Ordering::SeqCst);
|
HITS.fetch_add(1, Ordering::SeqCst);
|
||||||
System.alloc(layout)
|
AllocRef::alloc(&System, layout).unwrap().as_mut_ptr()
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue