Jeremy Soller 8 年之前
父节点
当前提交
49840afd9a
共有 4 个文件被更改,包括 30 次插入10 次删除
  1. 4 1
      Cargo.toml
  2. 4 0
      shim/Cargo.toml
  3. 18 0
      shim/src/lib.rs
  4. 4 9
      src/sys.rs

+ 4 - 1
Cargo.toml

@@ -12,6 +12,9 @@ readme = "README.md"
 keywords = ["alloc", "malloc", "allocator", "ralloc", "redox"]
 license = "MIT"
 
+[dependencies]
+ralloc_shim = { path="shim" }
+
 [dependencies.clippy]
 git = "https://github.com/Manishearth/rust-clippy.git"
 optional = true
@@ -26,7 +29,7 @@ debug-assertions = false
 codegen-units = 1
 
 [features]
-default = ["allocator", "clippy"]
+default = ["allocator"] #clippy
 
 allocator = []
 debug_tools = []

+ 4 - 0
shim/Cargo.toml

@@ -0,0 +1,4 @@
+[package]
+name = "ralloc_shim"
+version = "0.1.0"
+authors = ["Jeremy Soller <jackpot51@gmail.com>"]

+ 18 - 0
shim/src/lib.rs

@@ -0,0 +1,18 @@
+#![crate_name="ralloc_shim"]
+#![crate_type="lib"]
+#![feature(lang_items)]
+#![no_std]
+
+extern "C" {
+    /// Cooperatively gives up a timeslice to the OS scheduler.
+    pub fn sched_yield() -> isize;
+
+    /// Increment data segment of this process by some, _n_, return a pointer to the new data segment
+    /// start.
+    ///
+    /// This uses the system call BRK as backend.
+    ///
+    /// This is unsafe for multiple reasons. Most importantly, it can create an inconsistent state,
+    /// because it is not atomic. Thus, it can be used to create Undefined Behavior.
+    pub fn sbrk(n: isize) -> *mut u8;
+}

+ 4 - 9
src/sys.rs

@@ -1,15 +1,10 @@
 //! System primitives.
 
+extern crate ralloc_shim;
+
 #[cfg(not(feature = "unsafe_no_brk_lock"))]
 use sync;
 
-mod symbols {
-    extern {
-        pub fn sched_yield() -> isize;
-        pub fn sbrk(diff: isize) -> *mut u8;
-    }
-}
-
 /// The BRK mutex.
 ///
 /// This is used for avoiding data races in multiple allocator.
@@ -27,7 +22,7 @@ pub fn sbrk(n: isize) -> Result<*mut u8, ()> {
     let _guard = BRK_MUTEX.lock();
 
     unsafe {
-        let brk = symbols::sbrk(n as isize);
+        let brk = ralloc_shim::sbrk(n);
         if brk as usize == !0 {
             Err(())
         } else {
@@ -38,7 +33,7 @@ pub fn sbrk(n: isize) -> Result<*mut u8, ()> {
 
 /// Cooperatively gives up a timeslice to the OS scheduler.
 pub fn yield_now() {
-    assert_eq!(unsafe { symbols::sched_yield() }, 0);
+    assert_eq!(unsafe { ralloc_shim::sched_yield() }, 0);
 }
 
 #[cfg(test)]