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 {
|
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,
|
||||||
|
@ -160,7 +160,7 @@ impl Global {
|
||||||
// SAFETY: Same as `AllocRef::grow`
|
// SAFETY: Same as `AllocRef::grow`
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn grow_impl(
|
unsafe fn grow_impl(
|
||||||
&mut self,
|
&self,
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
old_layout: Layout,
|
old_layout: Layout,
|
||||||
new_layout: Layout,
|
new_layout: Layout,
|
||||||
|
@ -208,17 +208,17 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[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)
|
self.alloc_impl(layout, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
|
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
|
||||||
if layout.size() != 0 {
|
if layout.size() != 0 {
|
||||||
// SAFETY: `layout` is non-zero in size,
|
// SAFETY: `layout` is non-zero in size,
|
||||||
// other conditions must be upheld by the caller
|
// other conditions must be upheld by the caller
|
||||||
|
@ -228,7 +228,7 @@ unsafe impl AllocRef for Global {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn grow(
|
unsafe fn grow(
|
||||||
&mut self,
|
&self,
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
old_layout: Layout,
|
old_layout: Layout,
|
||||||
new_layout: Layout,
|
new_layout: Layout,
|
||||||
|
@ -239,7 +239,7 @@ unsafe impl AllocRef for Global {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn grow_zeroed(
|
unsafe fn grow_zeroed(
|
||||||
&mut self,
|
&self,
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
old_layout: Layout,
|
old_layout: Layout,
|
||||||
new_layout: Layout,
|
new_layout: Layout,
|
||||||
|
@ -250,7 +250,7 @@ unsafe impl AllocRef for Global {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn shrink(
|
unsafe fn shrink(
|
||||||
&mut self,
|
&self,
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
old_layout: Layout,
|
old_layout: Layout,
|
||||||
new_layout: Layout,
|
new_layout: Layout,
|
||||||
|
|
|
@ -170,7 +170,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||||
Self::allocate_in(capacity, AllocInit::Zeroed, alloc)
|
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 {
|
if mem::size_of::<T>() == 0 {
|
||||||
Self::new_in(alloc)
|
Self::new_in(alloc)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use std::cell::Cell;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn allocator_param() {
|
fn allocator_param() {
|
||||||
|
@ -17,32 +18,32 @@ 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.set(self.fuel.get() - size);
|
||||||
ok
|
ok
|
||||||
}
|
}
|
||||||
err @ Err(_) => err,
|
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) }
|
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);
|
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]
|
||||||
|
|
|
@ -11,7 +11,7 @@ fn std_heap_overaligned_request() {
|
||||||
check_overalign_requests(Global)
|
check_overalign_requests(Global)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_overalign_requests<T: AllocRef>(mut allocator: T) {
|
fn check_overalign_requests<T: AllocRef>(allocator: T) {
|
||||||
for &align in &[4, 8, 16, 32] {
|
for &align in &[4, 8, 16, 32] {
|
||||||
// less than and bigger than `MIN_ALIGN`
|
// less than and bigger than `MIN_ALIGN`
|
||||||
for &size in &[align / 2, align - 1] {
|
for &size in &[align / 2, align - 1] {
|
||||||
|
|
|
@ -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.
|
||||||
///
|
///
|
||||||
|
@ -126,7 +126,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_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
|
fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
|
||||||
let ptr = self.alloc(layout)?;
|
let ptr = self.alloc(layout)?;
|
||||||
// SAFETY: `alloc` returns a valid memory block
|
// SAFETY: `alloc` returns a valid memory block
|
||||||
unsafe { ptr.as_non_null_ptr().as_ptr().write_bytes(0, ptr.len()) }
|
unsafe { ptr.as_non_null_ptr().as_ptr().write_bytes(0, ptr.len()) }
|
||||||
|
@ -142,7 +142,7 @@ pub unsafe trait AllocRef {
|
||||||
///
|
///
|
||||||
/// [*currently allocated*]: #currently-allocated-memory
|
/// [*currently allocated*]: #currently-allocated-memory
|
||||||
/// [*fit*]: #memory-fitting
|
/// [*fit*]: #memory-fitting
|
||||||
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout);
|
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout);
|
||||||
|
|
||||||
/// Attempts to extend the memory block.
|
/// Attempts to extend the memory block.
|
||||||
///
|
///
|
||||||
|
@ -183,7 +183,7 @@ pub unsafe trait AllocRef {
|
||||||
///
|
///
|
||||||
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||||
unsafe fn grow(
|
unsafe fn grow(
|
||||||
&mut self,
|
&self,
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
old_layout: Layout,
|
old_layout: Layout,
|
||||||
new_layout: Layout,
|
new_layout: Layout,
|
||||||
|
@ -244,7 +244,7 @@ pub unsafe trait AllocRef {
|
||||||
///
|
///
|
||||||
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||||
unsafe fn grow_zeroed(
|
unsafe fn grow_zeroed(
|
||||||
&mut self,
|
&self,
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
old_layout: Layout,
|
old_layout: Layout,
|
||||||
new_layout: Layout,
|
new_layout: Layout,
|
||||||
|
@ -308,7 +308,7 @@ pub unsafe trait AllocRef {
|
||||||
///
|
///
|
||||||
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||||
unsafe fn shrink(
|
unsafe fn shrink(
|
||||||
&mut self,
|
&self,
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
old_layout: Layout,
|
old_layout: Layout,
|
||||||
new_layout: Layout,
|
new_layout: Layout,
|
||||||
|
@ -337,35 +337,35 @@ pub unsafe trait AllocRef {
|
||||||
///
|
///
|
||||||
/// The returned adaptor also implements `AllocRef` and will simply borrow this.
|
/// The returned adaptor also implements `AllocRef` and will simply borrow this.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn by_ref(&mut self) -> &mut Self {
|
fn by_ref(&mut self) -> &Self {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unstable(feature = "allocator_api", issue = "32838")]
|
#[unstable(feature = "allocator_api", issue = "32838")]
|
||||||
unsafe impl<A> AllocRef for &mut A
|
unsafe impl<A> AllocRef for &A
|
||||||
where
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
|
fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
|
||||||
(**self).alloc_zeroed(layout)
|
(**self).alloc_zeroed(layout)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
|
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
|
||||||
// SAFETY: the safety contract must be upheld by the caller
|
// SAFETY: the safety contract must be upheld by the caller
|
||||||
unsafe { (**self).dealloc(ptr, layout) }
|
unsafe { (**self).dealloc(ptr, layout) }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn grow(
|
unsafe fn grow(
|
||||||
&mut self,
|
&self,
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
old_layout: Layout,
|
old_layout: Layout,
|
||||||
new_layout: Layout,
|
new_layout: Layout,
|
||||||
|
@ -376,7 +376,7 @@ where
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn grow_zeroed(
|
unsafe fn grow_zeroed(
|
||||||
&mut self,
|
&self,
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
old_layout: Layout,
|
old_layout: Layout,
|
||||||
new_layout: Layout,
|
new_layout: Layout,
|
||||||
|
@ -387,7 +387,7 @@ where
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn shrink(
|
unsafe fn shrink(
|
||||||
&mut self,
|
&self,
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
old_layout: Layout,
|
old_layout: Layout,
|
||||||
new_layout: Layout,
|
new_layout: 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,
|
||||||
|
@ -152,7 +152,7 @@ impl System {
|
||||||
// SAFETY: Same as `AllocRef::grow`
|
// SAFETY: Same as `AllocRef::grow`
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn grow_impl(
|
unsafe fn grow_impl(
|
||||||
&mut self,
|
&self,
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
old_layout: Layout,
|
old_layout: Layout,
|
||||||
new_layout: Layout,
|
new_layout: Layout,
|
||||||
|
@ -190,7 +190,7 @@ impl System {
|
||||||
old_size => unsafe {
|
old_size => unsafe {
|
||||||
let new_ptr = self.alloc_impl(new_layout, zeroed)?;
|
let new_ptr = self.alloc_impl(new_layout, zeroed)?;
|
||||||
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), old_size);
|
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), old_size);
|
||||||
self.dealloc(ptr, old_layout);
|
AllocRef::dealloc(&self, ptr, old_layout);
|
||||||
Ok(new_ptr)
|
Ok(new_ptr)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -202,17 +202,17 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[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)
|
self.alloc_impl(layout, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
|
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
|
||||||
if layout.size() != 0 {
|
if layout.size() != 0 {
|
||||||
// SAFETY: `layout` is non-zero in size,
|
// SAFETY: `layout` is non-zero in size,
|
||||||
// other conditions must be upheld by the caller
|
// other conditions must be upheld by the caller
|
||||||
|
@ -222,7 +222,7 @@ unsafe impl AllocRef for System {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn grow(
|
unsafe fn grow(
|
||||||
&mut self,
|
&self,
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
old_layout: Layout,
|
old_layout: Layout,
|
||||||
new_layout: Layout,
|
new_layout: Layout,
|
||||||
|
@ -233,7 +233,7 @@ unsafe impl AllocRef for System {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn grow_zeroed(
|
unsafe fn grow_zeroed(
|
||||||
&mut self,
|
&self,
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
old_layout: Layout,
|
old_layout: Layout,
|
||||||
new_layout: Layout,
|
new_layout: Layout,
|
||||||
|
@ -244,7 +244,7 @@ unsafe impl AllocRef for System {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn shrink(
|
unsafe fn shrink(
|
||||||
&mut self,
|
&self,
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
old_layout: Layout,
|
old_layout: Layout,
|
||||||
new_layout: Layout,
|
new_layout: Layout,
|
||||||
|
@ -257,7 +257,7 @@ unsafe impl AllocRef for System {
|
||||||
match new_layout.size() {
|
match new_layout.size() {
|
||||||
// SAFETY: conditions must be upheld by the caller
|
// SAFETY: conditions must be upheld by the caller
|
||||||
0 => unsafe {
|
0 => unsafe {
|
||||||
self.dealloc(ptr, old_layout);
|
AllocRef::dealloc(&self, ptr, old_layout);
|
||||||
Ok(NonNull::slice_from_raw_parts(new_layout.dangling(), 0))
|
Ok(NonNull::slice_from_raw_parts(new_layout.dangling(), 0))
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -277,9 +277,9 @@ unsafe impl AllocRef for System {
|
||||||
// `new_ptr`. Thus, the call to `copy_nonoverlapping` is safe. The safety contract
|
// `new_ptr`. Thus, the call to `copy_nonoverlapping` is safe. The safety contract
|
||||||
// for `dealloc` must be upheld by the caller.
|
// for `dealloc` must be upheld by the caller.
|
||||||
new_size => unsafe {
|
new_size => unsafe {
|
||||||
let new_ptr = self.alloc(new_layout)?;
|
let new_ptr = AllocRef::alloc(&self, new_layout)?;
|
||||||
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), new_size);
|
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), new_size);
|
||||||
self.dealloc(ptr, old_layout);
|
AllocRef::dealloc(&self, ptr, old_layout);
|
||||||
Ok(new_ptr)
|
Ok(new_ptr)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ extern crate helper;
|
||||||
|
|
||||||
use std::alloc::{self, AllocRef, Global, Layout, System};
|
use std::alloc::{self, AllocRef, Global, Layout, System};
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
use std::ptr::NonNull;
|
||||||
|
|
||||||
static HITS: AtomicUsize = AtomicUsize::new(0);
|
static HITS: AtomicUsize = AtomicUsize::new(0);
|
||||||
|
|
||||||
|
@ -18,12 +19,12 @@ 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)
|
alloc::GlobalAlloc::alloc(&System, layout)
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
||||||
HITS.fetch_add(1, Ordering::SeqCst);
|
HITS.fetch_add(1, Ordering::SeqCst);
|
||||||
System.dealloc(ptr, layout)
|
AllocRef::dealloc(&System, NonNull::new(ptr).unwrap(), layout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue