Rollup merge of #76993 - blitzerr:alloc-ref, r=Amanieu
Changing the alloc() to accept &self instead of &mut self Fixes: [#55](https://github.com/rust-lang/wg-allocators/issues/55) This is the first cut. It only makes the change for `alloc` method.
This commit is contained in:
commit
a40d79c9fb
7 changed files with 48 additions and 46 deletions
|
@ -145,7 +145,7 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
|
|||
|
||||
impl Global {
|
||||
#[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() {
|
||||
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
|
||||
// SAFETY: `layout` is non-zero in size,
|
||||
|
@ -160,7 +160,7 @@ impl Global {
|
|||
// SAFETY: Same as `AllocRef::grow`
|
||||
#[inline]
|
||||
unsafe fn grow_impl(
|
||||
&mut self,
|
||||
&self,
|
||||
ptr: NonNull<u8>,
|
||||
old_layout: Layout,
|
||||
new_layout: Layout,
|
||||
|
@ -208,17 +208,17 @@ impl Global {
|
|||
#[unstable(feature = "allocator_api", issue = "32838")]
|
||||
unsafe impl AllocRef for Global {
|
||||
#[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)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
|
||||
fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
|
||||
self.alloc_impl(layout, true)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
|
||||
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
|
||||
if layout.size() != 0 {
|
||||
// SAFETY: `layout` is non-zero in size,
|
||||
// other conditions must be upheld by the caller
|
||||
|
@ -228,7 +228,7 @@ unsafe impl AllocRef for Global {
|
|||
|
||||
#[inline]
|
||||
unsafe fn grow(
|
||||
&mut self,
|
||||
&self,
|
||||
ptr: NonNull<u8>,
|
||||
old_layout: Layout,
|
||||
new_layout: Layout,
|
||||
|
@ -239,7 +239,7 @@ unsafe impl AllocRef for Global {
|
|||
|
||||
#[inline]
|
||||
unsafe fn grow_zeroed(
|
||||
&mut self,
|
||||
&self,
|
||||
ptr: NonNull<u8>,
|
||||
old_layout: Layout,
|
||||
new_layout: Layout,
|
||||
|
@ -250,7 +250,7 @@ unsafe impl AllocRef for Global {
|
|||
|
||||
#[inline]
|
||||
unsafe fn shrink(
|
||||
&mut self,
|
||||
&self,
|
||||
ptr: NonNull<u8>,
|
||||
old_layout: Layout,
|
||||
new_layout: Layout,
|
||||
|
|
|
@ -170,7 +170,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
|||
Self::allocate_in(capacity, AllocInit::Zeroed, alloc)
|
||||
}
|
||||
|
||||
fn allocate_in(capacity: usize, init: AllocInit, mut alloc: A) -> Self {
|
||||
fn allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Self {
|
||||
if mem::size_of::<T>() == 0 {
|
||||
Self::new_in(alloc)
|
||||
} else {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use super::*;
|
||||
use std::cell::Cell;
|
||||
|
||||
#[test]
|
||||
fn allocator_param() {
|
||||
|
@ -17,32 +18,32 @@ fn allocator_param() {
|
|||
// A dumb allocator that consumes a fixed amount of fuel
|
||||
// before allocation attempts start failing.
|
||||
struct BoundedAlloc {
|
||||
fuel: usize,
|
||||
fuel: Cell<usize>,
|
||||
}
|
||||
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();
|
||||
if size > self.fuel {
|
||||
if size > self.fuel.get() {
|
||||
return Err(AllocErr);
|
||||
}
|
||||
match Global.alloc(layout) {
|
||||
ok @ Ok(_) => {
|
||||
self.fuel -= size;
|
||||
self.fuel.set(self.fuel.get() - size);
|
||||
ok
|
||||
}
|
||||
err @ Err(_) => err,
|
||||
}
|
||||
}
|
||||
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
|
||||
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
|
||||
unsafe { Global.dealloc(ptr, layout) }
|
||||
}
|
||||
}
|
||||
|
||||
let a = BoundedAlloc { fuel: 500 };
|
||||
let a = BoundedAlloc { fuel: Cell::new(500) };
|
||||
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)
|
||||
assert_eq!(v.alloc.fuel, 250);
|
||||
assert_eq!(v.alloc.fuel.get(), 250);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue