浏览代码

Use spinlock if std feature is off

Gary Guo 3 年之前
父节点
当前提交
c195fc91eb
共有 3 个文件被更改,包括 29 次插入5 次删除
  1. 5 1
      Cargo.toml
  2. 21 4
      src/find_fde/registry.rs
  3. 3 0
      src/lib.rs

+ 5 - 1
Cargo.toml

@@ -7,7 +7,11 @@ edition = "2018"
 [dependencies]
 gimli = { version = "0.25.0", default-features = false, features = ["read"] }
 libc = "0.2"
-once_cell = "1.8"
+once_cell = { version = "1.8", optional = true }
+spin = { version = "0.9", default-features = false, features = ["lazy", "mutex", "spin_mutex"] }
+
+[features]
+std = ["once_cell"]
 
 [profile.release]
 debug = true

+ 21 - 4
src/find_fde/registry.rs

@@ -1,12 +1,29 @@
 use super::FDESearchResult;
 use crate::util::get_unlimited_slice;
+use alloc::boxed::Box;
 use core::ffi::c_void;
 use core::mem::MaybeUninit;
 use core::ptr;
 use gimli::{BaseAddresses, EhFrame, NativeEndian, UnwindSection};
+
+#[cfg(feature = "std")]
 use once_cell::sync::Lazy;
+#[cfg(feature = "std")]
 use std::sync::Mutex;
 
+#[cfg(not(feature = "std"))]
+use spin::{Lazy, Mutex};
+
+#[cfg(feature = "std")]
+fn unwrap_guard<T, E: Debug>(x: Result<T, E>) -> T {
+    x.unwrap()
+}
+
+#[cfg(not(feature = "std"))]
+fn unwrap_guard<T>(x: T) -> T {
+    x
+}
+
 enum Table {
     Single(*const c_void),
     Multiple(*const *const c_void),
@@ -40,7 +57,7 @@ pub fn get_finder() -> &'static Registry {
 
 impl super::FDEFinder for Registry {
     fn find_fde(&self, pc: usize) -> Option<FDESearchResult> {
-        let guard = get_finder().inner.lock().unwrap();
+        let guard = unwrap_guard(get_finder().inner.lock());
         unsafe {
             let mut cur = guard.object;
 
@@ -111,7 +128,7 @@ unsafe extern "C" fn __register_frame_info_bases(
             table: Table::Single(begin),
         });
 
-        let mut guard = get_finder().inner.lock().unwrap();
+        let mut guard = unwrap_guard(get_finder().inner.lock());
         (*ob).next = guard.object;
         guard.object = ob;
     }
@@ -147,7 +164,7 @@ unsafe extern "C" fn __register_frame_info_table_bases(
             table: Table::Multiple(begin as _),
         });
 
-        let mut guard = get_finder().inner.lock().unwrap();
+        let mut guard = unwrap_guard(get_finder().inner.lock());
         (*ob).next = guard.object;
         guard.object = ob;
     }
@@ -176,7 +193,7 @@ extern "C" fn __deregister_frame_info_bases(begin: *const c_void) -> *mut Object
         return core::ptr::null_mut();
     }
 
-    let mut guard = get_finder().inner.lock().unwrap();
+    let mut guard = unwrap_guard(get_finder().inner.lock());
     unsafe {
         let mut prev = &mut guard.object;
         let mut cur = *prev;

+ 3 - 0
src/lib.rs

@@ -3,6 +3,9 @@
 #![feature(asm)]
 #![warn(rust_2018_idioms)]
 #![warn(unsafe_op_in_unsafe_fn)]
+#![cfg_attr(not(feature = "std"), no_std)]
+
+extern crate alloc;
 
 mod abi;
 mod arch;