]> git.lizzy.rs Git - rust.git/commitdiff
Add a GlobalAlloc trait
authorSimon Sapin <simon.sapin@exyr.org>
Tue, 3 Apr 2018 12:07:06 +0000 (14:07 +0200)
committerSimon Sapin <simon.sapin@exyr.org>
Thu, 12 Apr 2018 20:52:47 +0000 (22:52 +0200)
src/libcore/heap.rs

index 80eedb5bff22aa1c6ba4c906775e4f425374bb5d..5c51bb2b51b9ca89d0ed2fc812392a9c2f446917 100644 (file)
@@ -404,6 +404,36 @@ fn from(err: AllocErr) -> Self {
     }
 }
 
+// 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`.
 ///