mirror of
https://github.com/girlbossceo/jemallocator.git
synced 2024-11-25 14:31:43 +00:00
Merge pull request #28 from gnzlbg/excess
Add alloc_excess/realloc_excess
This commit is contained in:
commit
b87f8b806a
30
src/lib.rs
30
src/lib.rs
|
@ -162,6 +162,18 @@ unsafe impl<'a> Alloc for &'a Jemalloc {
|
|||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
|
||||
let flags = align_to_flags(layout.align());
|
||||
let ptr = ffi::mallocx(layout.size(), flags);
|
||||
if ptr.is_null() {
|
||||
Err(AllocErr::Exhausted { request: layout })
|
||||
} else {
|
||||
let excess = ffi::nallocx(layout.size(), flags);
|
||||
Ok(Excess(ptr as *mut u8, excess))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
|
||||
let flags = align_to_flags(layout.align(), layout.size());
|
||||
|
@ -185,6 +197,24 @@ unsafe impl<'a> Alloc for &'a Jemalloc {
|
|||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn realloc_excess(&mut self,
|
||||
ptr: *mut u8,
|
||||
old_layout: Layout,
|
||||
new_layout: Layout) -> Result<Excess, AllocErr> {
|
||||
if old_layout.align() != new_layout.align() {
|
||||
return Err(AllocErr::Unsupported { details: "cannot change align" })
|
||||
}
|
||||
let flags = align_to_flags(new_layout.align());
|
||||
let ptr = ffi::rallocx(ptr as *mut c_void, new_layout.size(), flags);
|
||||
if ptr.is_null() {
|
||||
Err(AllocErr::Exhausted { request: new_layout })
|
||||
} else {
|
||||
let excess = ffi::nallocx(new_layout.size(), flags);
|
||||
Ok(Excess(ptr as *mut u8, excess))
|
||||
}
|
||||
}
|
||||
|
||||
fn oom(&mut self, err: AllocErr) -> ! {
|
||||
System.oom(err)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue