Ver código fonte

Update implementation for nightly 19/6/2018

Tom Almeida 6 anos atrás
pai
commit
cccbe11957
8 arquivos alterados com 49 adições e 37 exclusões
  1. 1 1
      Cargo.toml
  2. 0 3
      src/block.rs
  3. 0 1
      src/bookkeeper.rs
  4. 0 1
      src/brk.rs
  5. 39 21
      src/lib.rs
  6. 1 1
      src/prelude.rs
  7. 8 8
      src/ptr.rs
  8. 0 1
      src/vec.rs

+ 1 - 1
Cargo.toml

@@ -5,7 +5,7 @@ authors = ["ticki <ticki@users.noreply.github.com>"]
 
 # URLs and paths
 description = "An efficient alternative platform-agnostic allocator."
-repository = "https://gitlab.redox-os.org/redox-os/ralloc"
+repository = "https://github.com/redox-os/ralloc"
 readme = "README.md"
 
 # Metadata

+ 0 - 3
src/block.rs

@@ -61,7 +61,6 @@ impl Block {
 
     /// Create an empty block representing the right edge of this block
     #[inline]
-    #[allow(cast_possible_wrap)]
     pub fn empty_right(&self) -> Block {
         Block {
             size: 0,
@@ -171,7 +170,6 @@ impl Block {
     ///
     /// Panics if `pos` is out of bound.
     #[inline]
-    #[allow(cast_possible_wrap)]
     pub fn split(self, pos: usize) -> (Block, Block) {
         assert!(pos <= self.size, "Split {} out of bound (size is {})!", pos, self.size);
 
@@ -197,7 +195,6 @@ impl Block {
     ///
     /// Returns an `None` holding the intact block if `align` is out of bounds.
     #[inline]
-    #[allow(cast_possible_wrap)]
     pub fn align(&mut self, align: usize) -> Option<(Block, Block)> {
         // Logging.
         log!(INTERNAL, "Padding {:?} to align {}", self, align);

+ 0 - 1
src/bookkeeper.rs

@@ -69,7 +69,6 @@ pub struct Bookkeeper {
     id: usize,
 }
 
-#[allow(len_without_is_empty)]
 impl Bookkeeper {
     /// Create a new bookkeeper with some initial vector.
     pub fn new(vec: Vec<Block>) -> Bookkeeper {

+ 0 - 1
src/brk.rs

@@ -73,7 +73,6 @@ impl BrkLock {
     /// Safely release memory to the OS.
     ///
     /// If failed, we return the memory.
-    #[allow(cast_possible_wrap)]
     pub fn release(&mut self, block: Block) -> Result<(), Block> {
         // Check if we are actually next to the program break.
         if self.current_brk() == Pointer::from(block.empty_right()) {

+ 39 - 21
src/lib.rs

@@ -14,16 +14,12 @@
 
 #![no_std]
 
-#![feature(alloc, allocator_api, const_fn, core_intrinsics, stmt_expr_attributes, drop_types_in_const,
-           nonzero, optin_builtin_traits, type_ascription, thread_local, linkage,
-           try_from, const_unsafe_cell_new, const_atomic_bool_new, const_nonzero_new,
-           const_atomic_ptr_new)]
-#![warn(missing_docs, cast_precision_loss, cast_sign_loss, cast_possible_wrap,
-        cast_possible_truncation, filter_map, if_not_else, items_after_statements,
-        invalid_upcast_comparisons, mutex_integer, nonminimal_bool, shadow_same, shadow_unrelated,
-        single_match_else, string_add, string_add_assign, wrong_pub_self_convention)]
-
-extern crate alloc;
+#![feature(allocator_api, const_fn, core_intrinsics, stmt_expr_attributes,
+           optin_builtin_traits, type_ascription, thread_local, linkage,
+           try_from, const_unsafe_cell_new, const_atomic_bool_new,
+           const_nonzero_new, const_atomic_ptr_new)]
+#![warn(missing_docs)]
+
 extern crate ralloc_shim as shim;
 
 #[macro_use]
@@ -48,7 +44,9 @@ mod ptr;
 mod sync;
 mod vec;
 
-use alloc::heap::{Alloc, AllocErr, Layout, CannotReallocInPlace};
+use core::alloc::{Alloc, AllocErr, Layout, CannotReallocInPlace};
+use core::alloc::GlobalAlloc;
+use core::ptr::NonNull;
 
 pub use allocator::{alloc, free, realloc, realloc_inplace};
 pub use brk::sbrk;
@@ -56,31 +54,42 @@ pub use fail::set_oom_handler;
 #[cfg(feature = "tls")]
 pub use fail::set_thread_oom_handler;
 
+/// The rallocator
 pub struct Allocator;
 
 unsafe impl<'a> Alloc for &'a Allocator {
-    unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
-        Ok(allocator::alloc(layout.size(), layout.align()))
+    unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
+        let ptr = allocator::alloc(layout.size(), layout.align());
+        if ptr.is_null() {
+            Err(AllocErr)
+        } else {
+            Ok(NonNull::new_unchecked(ptr))
+        }
     }
 
-    unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
-        allocator::free(ptr, layout.size());
+    unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
+        allocator::free(ptr.as_ptr(), layout.size());
     }
 
-    unsafe fn realloc(&mut self, ptr: *mut u8, layout: Layout, new_layout: Layout) -> Result<*mut u8, AllocErr> {
-        Ok(allocator::realloc(ptr, layout.size(), new_layout.size(), new_layout.align()))
+    unsafe fn realloc(&mut self, ptr: NonNull<u8>, layout: Layout, new_size: usize) -> Result<NonNull<u8>, AllocErr> {
+        let ptr = allocator::realloc(ptr.as_ptr(), layout.size(), new_size, layout.align());
+        if ptr.is_null() {
+            Err(AllocErr)
+        } else {
+            Ok(NonNull::new_unchecked(ptr))
+        }
     }
 
-    unsafe fn grow_in_place(&mut self, ptr: *mut u8, layout: Layout, new_layout: Layout) -> Result<(), CannotReallocInPlace> {
-        if allocator::realloc_inplace(ptr, layout.size(), new_layout.size()).is_ok() {
+    unsafe fn grow_in_place(&mut self, ptr: NonNull<u8>, layout: Layout, new_size: usize) -> Result<(), CannotReallocInPlace> {
+        if allocator::realloc_inplace(ptr.as_ptr(), layout.size(), new_size).is_ok() {
             Ok(())
         } else {
             Err(CannotReallocInPlace)
         }
     }
 
-    unsafe fn shrink_in_place(&mut self, ptr: *mut u8, layout: Layout, new_layout: Layout) -> Result<(), CannotReallocInPlace> {
-        if allocator::realloc_inplace(ptr, layout.size(), new_layout.size()).is_ok() {
+    unsafe fn shrink_in_place(&mut self, ptr: NonNull<u8>, layout: Layout, new_size: usize) -> Result<(), CannotReallocInPlace> {
+        if allocator::realloc_inplace(ptr.as_ptr(), layout.size(), new_size).is_ok() {
             Ok(())
         } else {
             Err(CannotReallocInPlace)
@@ -92,3 +101,12 @@ unsafe impl<'a> Alloc for &'a Allocator {
         (layout.size(), layout.size())
     }
 }
+
+unsafe impl GlobalAlloc for Allocator {
+    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
+        allocator::alloc(layout.size(), layout.align())
+    }
+    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
+        allocator::free(ptr, layout.size());
+    }
+}

+ 1 - 1
src/prelude.rs

@@ -6,5 +6,5 @@ pub use block::Block;
 pub use cell::MoveCell;
 pub use lazy_init::LazyInit;
 pub use sync::Mutex;
-pub use ptr::Pointer;
 pub use vec::Vec;
+pub use ptr::Pointer;

+ 8 - 8
src/ptr.rs

@@ -1,7 +1,7 @@
 //! Pointer wrappers.
 
-use core::nonzero::NonZero;
-use core::{ops, marker};
+use core::ptr::NonNull;
+use core::marker;
 
 /// A pointer wrapper type.
 ///
@@ -10,7 +10,7 @@ use core::{ops, marker};
 #[derive(PartialEq, Eq, Debug, Clone)]
 pub struct Pointer<T> {
     /// The internal pointer.
-    ptr: NonZero<*mut T>,
+    ptr: NonNull<T>,
     /// Associated phantom data.
     ///
     /// This indicates that we _own_ T.
@@ -30,7 +30,7 @@ impl<T> Pointer<T> {
         debug_assert!(!ptr.is_null(), "Null pointer!");
 
         Pointer {
-            ptr: NonZero::new_unchecked(ptr),
+            ptr: NonNull::new_unchecked(ptr),
             _phantom: marker::PhantomData,
         }
     }
@@ -45,7 +45,7 @@ impl<T> Pointer<T> {
                 // LAST AUDIT: 2016-08-21 (Ticki).
 
                 // 0x1 is non-zero.
-                NonZero::new_unchecked(0x1 as *mut T)
+                NonNull::new_unchecked(0x1 as *mut T)
             },
             _phantom: marker::PhantomData,
         }
@@ -61,7 +61,7 @@ impl<T> Pointer<T> {
                 // LAST AUDIT: 2016-08-21 (Ticki).
 
                 // Casting the pointer will preserve its nullable state.
-                NonZero::new_unchecked(self.get() as *mut U)
+                NonNull::new_unchecked(self.get() as *mut U)
             },
             _phantom: marker::PhantomData,
         }
@@ -76,11 +76,11 @@ impl<T> Pointer<T> {
     /// This is unsafe, due to OOB offsets being undefined behavior.
     #[inline]
     pub unsafe fn offset(self, diff: isize) -> Pointer<T> {
-        Pointer::new(self.ptr.get().offset(diff))
+        Pointer::new(self.ptr.as_ptr().offset(diff))
     }
 
     pub fn get(&self) -> *mut T {
-        self.ptr.get()
+        self.ptr.as_ptr()
     }
 }
 

+ 0 - 1
src/vec.rs

@@ -84,7 +84,6 @@ impl<T: Leak> Vec<T> {
     ///
     /// On success, return `Ok(())`. On failure (not enough capacity), return `Err(())`.
     #[inline]
-    #[allow(cast_possible_wrap)]
     pub fn push(&mut self, elem: T) -> Result<(), ()> {
         if self.len == self.cap {
             Err(())