-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial attempt to use dynamically acquired symbols on Unix
- Loading branch information
Showing
8 changed files
with
161 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crate::nif_filler::{self, DynNifFiller}; | ||
use crate::types::*; | ||
|
||
static mut DYN_NIF_CALLBACKS: DynNifCallbacks = | ||
unsafe { std::mem::MaybeUninit::zeroed().assume_init() }; | ||
|
||
pub unsafe fn internal_set_symbols(callbacks: DynNifCallbacks) { | ||
DYN_NIF_CALLBACKS = callbacks; | ||
} | ||
|
||
pub unsafe fn internal_write_symbols() { | ||
let filler = nif_filler::new(); | ||
DYN_NIF_CALLBACKS.write_symbols(filler); | ||
} | ||
|
||
/// See [enif_make_pid](http://erlang.org/doc/man/erl_nif.html#enif_make_pid) in the Erlang docs | ||
pub unsafe fn enif_make_pid(_env: *mut ErlNifEnv, pid: ErlNifPid) -> ERL_NIF_TERM { | ||
pid.pid | ||
} | ||
|
||
/// See [enif_compare_pids](http://erlang.org/doc/man/erl_nif.html#enif_compare_pids) in the Erlang docs | ||
pub unsafe fn enif_compare_pids(pid1: *const ErlNifPid, pid2: *const ErlNifPid) -> c_int { | ||
// Mimics the implementation of the enif_compare_pids macro | ||
enif_compare((*pid1).pid, (*pid2).pid) | ||
} | ||
|
||
// Include the file generated by `build.rs`. | ||
include!(concat!(env!("OUT_DIR"), "/nif_api.snippet.rs")); | ||
// example of included content: | ||
// extern "C" { | ||
// pub fn enif_priv_data(arg1: *mut ErlNifEnv) -> *mut c_void; | ||
// pub fn enif_alloc(size: size_t) -> *mut c_void; | ||
// pub fn enif_free(ptr: *mut c_void); | ||
// pub fn enif_is_atom(arg1: *mut ErlNifEnv, term: ERL_NIF_TERM) -> c_int; | ||
// pub fn enif_is_binary(arg1: *mut ErlNifEnv, term: ERL_NIF_TERM) -> c_int; | ||
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
pub(crate) trait DynNifFiller { | ||
fn write<T: Copy>(&self, field: &mut Option<T>, name: &str); | ||
} | ||
|
||
pub struct NullNifFiller; | ||
impl DynNifFiller for NullNifFiller { | ||
fn write<T: Copy>(&self, _field: &mut Option<T>, _name: &str) {} | ||
} | ||
|
||
#[cfg(not(target_os = "windows"))] | ||
mod internal { | ||
use std::ffi::OsStr; | ||
|
||
use super::DynNifFiller; | ||
use libloading::os::unix::{Library, RTLD_GLOBAL, RTLD_NOW}; | ||
|
||
pub(crate) struct DlsymNifFiller { | ||
lib: libloading::Library, | ||
} | ||
|
||
impl DlsymNifFiller { | ||
pub(crate) fn new() -> Self { | ||
let lib = unsafe { Library::open(None::<&OsStr>, RTLD_NOW | RTLD_GLOBAL) }; | ||
DlsymNifFiller { | ||
lib: lib.unwrap().into(), | ||
} | ||
} | ||
} | ||
|
||
impl DynNifFiller for DlsymNifFiller { | ||
fn write<T: Copy>(&self, field: &mut Option<T>, name: &str) { | ||
let symbol = unsafe { self.lib.get::<T>(name.as_bytes()).unwrap() }; | ||
*field = Some(*symbol); | ||
} | ||
} | ||
|
||
pub(crate) fn new() -> impl DynNifFiller { | ||
DlsymNifFiller::new() | ||
} | ||
} | ||
|
||
#[cfg(target_os = "windows")] | ||
mod internal { | ||
use super::*; | ||
|
||
pub fn new() -> impl DynNifFiller { | ||
NullNifFiller | ||
} | ||
} | ||
|
||
pub(crate) use internal::new; |
Oops, something went wrong.