From c660cedc02e125fe47d90075837c5d8adeb4c097 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Tue, 3 Apr 2018 14:07:06 +0200 Subject: [PATCH] Add a GlobalAlloc trait --- src/libcore/heap.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/libcore/heap.rs b/src/libcore/heap.rs index 80eedb5bff22..5c51bb2b51b9 100644 --- a/src/libcore/heap.rs +++ b/src/libcore/heap.rs @@ -404,6 +404,36 @@ impl From for CollectionAllocErr { } } +// FIXME: docs +pub unsafe trait GlobalAlloc { + unsafe fn alloc(&self, layout: Layout) -> *mut Void; + + unsafe fn dealloc(&self, ptr: *mut Void, layout: Layout); + + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut Void { + let size = layout.size(); + let ptr = self.alloc(layout); + if !ptr.is_null() { + ptr::write_bytes(ptr as *mut u8, 0, size); + } + ptr + } + + unsafe fn realloc(&self, ptr: *mut Void, old_layout: Layout, new_size: usize) -> *mut Void { + let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align()); + let new_ptr = self.alloc(new_layout); + if !new_ptr.is_null() { + ptr::copy_nonoverlapping( + ptr as *const u8, + new_ptr as *mut u8, + cmp::min(old_layout.size(), new_size), + ); + self.dealloc(ptr, old_layout); + } + new_ptr + } +} + /// An implementation of `Alloc` can allocate, reallocate, and /// deallocate arbitrary blocks of data described via `Layout`. ///