Merge pull request #28 from gnzlbg/excess

Add alloc_excess/realloc_excess
This commit is contained in:
Alex Crichton 2017-11-24 09:50:13 -06:00 committed by GitHub
commit b87f8b806a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 0 deletions

View File

@ -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)
}