Skip to content

Commit

Permalink
Move rustler_sys into rustler::sys
Browse files Browse the repository at this point in the history
  • Loading branch information
filmor committed Sep 30, 2024
1 parent f4a2923 commit 6caf207
Show file tree
Hide file tree
Showing 41 changed files with 198 additions and 505 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ resolver = "2"
members = [
"rustler",
"rustler_codegen",
"rustler_sys",
"rustler_tests/native/binary_example",
"rustler_tests/native/rustler_test",
"rustler_tests/native/rustler_bigint_test",
Expand All @@ -16,5 +15,4 @@ members = [
default-members = [
"rustler",
"rustler_codegen",
"rustler_sys",
]
13 changes: 8 additions & 5 deletions rustler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ default = ["nif_version_2_15"]
derive = []
alternative_nif_init_name = []
allocator = []
nif_version_2_14 = ["rustler_sys/nif_version_2_14"]
nif_version_2_15 = ["nif_version_2_14", "rustler_sys/nif_version_2_15"]
nif_version_2_16 = ["nif_version_2_15", "rustler_sys/nif_version_2_16"]
nif_version_2_17 = ["nif_version_2_16", "rustler_sys/nif_version_2_17"]
nif_version_2_14 = []
nif_version_2_15 = ["nif_version_2_14"]
nif_version_2_16 = ["nif_version_2_15"]
nif_version_2_17 = ["nif_version_2_16"]
serde = ["dep:serde"]

[dependencies]
libloading = "0.8"
inventory = "0.3"
rustler_codegen = { path = "../rustler_codegen", version = "0.34.0" }
rustler_sys = { path = "../rustler_sys", version = "~2.4.3" }
num-bigint = { version = "0.4", optional = true }
serde = { version = "1", optional = true }

[build-dependencies]
regex-lite = "0.1"

[package.metadata.release]

[[package.metadata.release.pre-release-replacements]]
Expand Down
14 changes: 7 additions & 7 deletions rustler_sys/build.rs → rustler/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ impl<'a> ApiBuilder for BasicApiBuilder<'a> {
name, name
)
.unwrap();
writeln!(self.0, "#[macro_export]").unwrap();
writeln!(self.0, "macro_rules! {} {{", name).unwrap();
writeln!(self.0, "#[macro_export] macro_rules! {} {{", name).unwrap();
writeln!(
self.0,
" ( $( $arg:expr ),* ) => {{ $crate::_{}($($arg),*) }};",
" ( $( $arg:expr ),* ) => {{ $crate::sys::_{}($($arg),*) }};",
name
)
.unwrap();
Expand All @@ -92,6 +91,7 @@ impl<'a> ApiBuilder for BasicApiBuilder<'a> {
)
.unwrap();
writeln!(self.0, "}}\n").unwrap();
writeln!(self.0, "pub use {name};\n").unwrap();
}
fn dummy(&mut self, _name: &str) {}
}
Expand Down Expand Up @@ -156,11 +156,10 @@ impl<'a> ApiBuilder for WinForwardersApiBuilder<'a> {
writeln!(self.0, "}}\n").unwrap();
}
fn variadic_func(&mut self, ret: &str, name: &str, args: &str) {
writeln!(self.0, "#[macro_export]").unwrap();
writeln!(self.0, "macro_rules! {} {{", name).unwrap();
writeln!(self.0, "#[macro_export] macro_rules! {} {{", name).unwrap();
writeln!(
self.0,
" ( $( $arg:expr ),* ) => {{ $crate::get_{}()($($arg),*) }};",
" ( $( $arg:expr ),* ) => {{ $crate::sys::get_{}()($($arg),*) }};",
name
)
.unwrap();
Expand All @@ -171,6 +170,7 @@ impl<'a> ApiBuilder for WinForwardersApiBuilder<'a> {
)
.unwrap();
writeln!(self.0, "}}\n").unwrap();
writeln!(self.0, "pub use {name};\n").unwrap();

write!(self.0, "pub unsafe fn get_{}() -> ", name).unwrap();
write_variadic_fn_type(self.0, args, ret);
Expand Down Expand Up @@ -833,7 +833,7 @@ fn build_api(b: &mut dyn ApiBuilder, opts: &GenerateOptions) {
// Func("int", "enif_ioq_peek_head", "ErlNifEnv *env, ErlNifIOQueue *q, size_t *size, ERL_NIF_TERM *head"),
// Func("char*, "enif_mutex_name", "ErlNifMutex*"),
// Func("char*, "enif_cond_name", "ErlNifCond*"),
// Func("char*, "enif_rwlock_name", "ErlNifRWLock*"),buid
// Func("char*, "enif_rwlock_name", "ErlNifRWLock*"),
// Func("char*, "enif_thread_name", "ErlNifTid"),
b.dummy("dummy_enif_ioq_peek_head");
b.dummy("dummy_enif_mutex_name");
Expand Down
12 changes: 7 additions & 5 deletions rustler/src/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::alloc::{GlobalAlloc, Layout};

use crate::sys::{c_void, enif_alloc, enif_free};

const SIZEOF_USIZE: usize = std::mem::size_of::<usize>();
const MAX_ALIGN: usize = 8;

Expand All @@ -23,7 +25,7 @@ unsafe impl GlobalAlloc for EnifAllocator {
// `layout.align() - 1`. The requirement for an additional `usize` just shifts the
// problem without changing the padding requirement.
let total_size = SIZEOF_USIZE + layout.size() + layout.align() - 1;
let ptr = rustler_sys::enif_alloc(total_size) as *mut u8;
let ptr = enif_alloc(total_size) as *mut u8;

// Shift the returned pointer to make space for the original pointer
let ptr1 = ptr.wrapping_add(SIZEOF_USIZE);
Expand All @@ -37,7 +39,7 @@ unsafe impl GlobalAlloc for EnifAllocator {

aligned_ptr
} else {
rustler_sys::enif_alloc(layout.size()) as *mut u8
enif_alloc(layout.size()) as *mut u8
}
}

Expand All @@ -46,10 +48,10 @@ unsafe impl GlobalAlloc for EnifAllocator {
// Retrieve the original pointer
let header = ptr.wrapping_sub(SIZEOF_USIZE);
let ptr = *(header as *mut usize);
ptr as *mut rustler_sys::c_void
ptr as *mut c_void
} else {
ptr as *mut rustler_sys::c_void
ptr as *mut c_void
};
rustler_sys::enif_free(ptr);
enif_free(ptr);
}
}
4 changes: 2 additions & 2 deletions rustler/src/codegen_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use crate::wrapper::{
NIF_ENV, NIF_MAJOR_VERSION, NIF_MINOR_VERSION, NIF_TERM,
};

pub use rustler_sys::{internal_set_symbols, internal_write_symbols, DynNifCallbacks};
pub use crate::sys::{internal_set_symbols, internal_write_symbols, DynNifCallbacks};

pub unsafe trait NifReturnable {
unsafe fn into_returned(self, env: Env) -> NifReturned;
Expand Down Expand Up @@ -76,7 +76,7 @@ impl NifReturned {
flags,
fun,
args,
} => rustler_sys::enif_schedule_nif(
} => crate::sys::enif_schedule_nif(
env.as_c_arg(),
fun_name.as_ptr() as *const c_char,
flags as i32,
Expand Down
4 changes: 2 additions & 2 deletions rustler/src/dynamic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ffi::c_double;

#[cfg(feature = "nif_version_2_15")]
use rustler_sys::ErlNifTermType;
use crate::sys::ErlNifTermType;

use crate::wrapper::check;
use crate::Term;
Expand Down Expand Up @@ -108,7 +108,7 @@ impl<'a> Term<'a> {
pub fn is_float(self) -> bool {
let mut val: c_double = 0.0;
unsafe {
rustler_sys::enif_get_double(self.get_env().as_c_arg(), self.as_c_arg(), &mut val) == 1
crate::sys::enif_get_double(self.get_env().as_c_arg(), self.as_c_arg(), &mut val) == 1
}
}

Expand Down
17 changes: 7 additions & 10 deletions rustler/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::sys::{enif_alloc_env, enif_clear_env, enif_free_env, enif_send, enif_whereis_pid};
use crate::thread::is_scheduler_thread;
use crate::types::LocalPid;
use crate::wrapper::{NIF_ENV, NIF_TERM};
Expand Down Expand Up @@ -114,9 +115,7 @@ impl<'a> Env<'a> {
let message = message.encode(self);

// Send the message.
let res = unsafe {
rustler_sys::enif_send(env, pid.as_c_arg(), ptr::null_mut(), message.as_c_arg())
};
let res = unsafe { enif_send(env, pid.as_c_arg(), ptr::null_mut(), message.as_c_arg()) };

if res == 1 {
Ok(())
Expand All @@ -143,7 +142,7 @@ impl<'a> Env<'a> {
let mut enif_pid = std::mem::MaybeUninit::uninit();

if unsafe {
rustler_sys::enif_whereis_pid(
enif_whereis_pid(
self.as_c_arg(),
name_or_pid.as_c_arg(),
enif_pid.as_mut_ptr(),
Expand Down Expand Up @@ -208,7 +207,7 @@ impl OwnedEnv {
#[allow(clippy::arc_with_non_send_sync)] // Likely false negative, see https://github.com/rust-lang/rust-clippy/issues/11382
pub fn new() -> OwnedEnv {
OwnedEnv {
env: Arc::new(unsafe { rustler_sys::enif_alloc_env() }),
env: Arc::new(unsafe { enif_alloc_env() }),
}
}

Expand Down Expand Up @@ -249,9 +248,7 @@ impl OwnedEnv {

let message = self.run(|env| closure(env).encode(env).as_c_arg());

let res = unsafe {
rustler_sys::enif_send(ptr::null_mut(), recipient.as_c_arg(), *self.env, message)
};
let res = unsafe { enif_send(ptr::null_mut(), recipient.as_c_arg(), *self.env, message) };

self.clear();

Expand All @@ -275,7 +272,7 @@ impl OwnedEnv {
let c_env = *self.env;
self.env = Arc::new(c_env);
unsafe {
rustler_sys::enif_clear_env(c_env);
enif_clear_env(c_env);
}
}

Expand Down Expand Up @@ -318,7 +315,7 @@ impl OwnedEnv {
impl Drop for OwnedEnv {
fn drop(&mut self) {
unsafe {
rustler_sys::enif_free_env(*self.env);
enif_free_env(*self.env);
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions rustler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,4 @@ pub mod serde;
#[cfg(feature = "serde")]
pub use crate::serde::SerdeTerm;

pub mod sys {
pub use rustler_sys::*;
}
pub mod sys;
35 changes: 17 additions & 18 deletions rustler/src/resource/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::mem::MaybeUninit;
use std::ops::Deref;
use std::ptr;

use rustler_sys::{c_void, ErlNifEnv};
use crate::sys::{
c_void, enif_alloc_resource, enif_demonitor_process, enif_keep_resource, enif_make_resource,
enif_make_resource_binary, enif_monitor_process, enif_release_resource, ErlNifEnv,
};

use crate::thread::is_scheduler_thread;
use crate::{Binary, Decoder, Encoder, Env, Error, LocalPid, Monitor, NifResult, OwnedEnv, Term};
Expand Down Expand Up @@ -40,7 +43,7 @@ where
pub fn new(data: T) -> Self {
let alloc_size = get_alloc_size_struct::<T>();
let resource_type = T::get_resource_type().unwrap();
let mem_raw = unsafe { rustler_sys::enif_alloc_resource(resource_type, alloc_size) };
let mem_raw = unsafe { enif_alloc_resource(resource_type, alloc_size) };
let aligned_mem = unsafe { align_alloced_mem_for_struct::<T>(mem_raw) as *mut T };

unsafe { ptr::write(aligned_mem, data) };
Expand Down Expand Up @@ -80,7 +83,7 @@ where
F: FnOnce(&'a T) -> &'b [u8],
{
let bin = f(&*self.inner);
let binary = rustler_sys::enif_make_resource_binary(
let binary = enif_make_resource_binary(
env.as_c_arg(),
self.raw,
bin.as_ptr() as *const c_void,
Expand All @@ -93,17 +96,12 @@ where

fn from_term(term: Term) -> Result<Self, Error> {
let (raw, inner) = unsafe { term.try_get_resource_ptrs::<T>() }.ok_or(Error::BadArg)?;
unsafe { rustler_sys::enif_keep_resource(raw) };
unsafe { enif_keep_resource(raw) };
Ok(ResourceArc { raw, inner })
}

fn as_term<'a>(&self, env: Env<'a>) -> Term<'a> {
unsafe {
Term::new(
env,
rustler_sys::enif_make_resource(env.as_c_arg(), self.raw),
)
}
unsafe { Term::new(env, enif_make_resource(env.as_c_arg(), self.raw)) }
}

fn as_c_arg(&mut self) -> *const c_void {
Expand All @@ -126,9 +124,8 @@ where

let env = maybe_env(caller_env);
let mut mon = MaybeUninit::uninit();
let res = unsafe {
rustler_sys::enif_monitor_process(env, self.raw, pid.as_c_arg(), mon.as_mut_ptr()) == 0
};
let res =
unsafe { enif_monitor_process(env, self.raw, pid.as_c_arg(), mon.as_mut_ptr()) == 0 };
if res {
Some(unsafe { Monitor::new(mon.assume_init()) })
} else {
Expand All @@ -142,7 +139,7 @@ where
}

let env = maybe_env(caller_env);
unsafe { rustler_sys::enif_demonitor_process(env, self.raw, mon.as_c_arg()) == 0 }
unsafe { enif_demonitor_process(env, self.raw, mon.as_c_arg()) == 0 }
}
}

Expand Down Expand Up @@ -179,9 +176,11 @@ impl<'a> Env<'a> {
module: crate::Atom,
name: crate::Atom,
resource: Term<'a>,
call_data: *mut rustler_sys::c_void,
call_data: *mut c_void,
) -> Result<(), super::DynamicResourceCallError> {
let res = rustler_sys::enif_dynamic_resource_call(
use crate::sys::enif_dynamic_resource_call;

let res = enif_dynamic_resource_call(
self.as_c_arg(),
module.as_c_arg(),
name.as_c_arg(),
Expand Down Expand Up @@ -215,7 +214,7 @@ where
/// Cloning a `ResourceArc` simply increments the reference count for the
/// resource. The `T` value is not cloned.
fn clone(&self) -> Self {
unsafe { rustler_sys::enif_keep_resource(self.raw) };
unsafe { enif_keep_resource(self.raw) };
ResourceArc {
raw: self.raw,
inner: self.inner,
Expand All @@ -234,7 +233,7 @@ where
/// at an unpredictable time: whenever the VM decides to do garbage
/// collection.
fn drop(&mut self) {
unsafe { rustler_sys::enif_release_resource(self.as_c_arg()) };
unsafe { enif_release_resource(self.as_c_arg()) };
}
}

Expand Down
4 changes: 2 additions & 2 deletions rustler/src/resource/monitor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustler_sys::ErlNifMonitor;
use crate::sys::{enif_compare_monitors, ErlNifMonitor};

/// Handle for a monitor created using `ResourceArc<T>::monitor`.
///
Expand All @@ -25,6 +25,6 @@ impl Monitor {

impl PartialEq for Monitor {
fn eq(&self, other: &Self) -> bool {
unsafe { rustler_sys::enif_compare_monitors(&self.inner, &other.inner) == 0 }
unsafe { enif_compare_monitors(&self.inner, &other.inner) == 0 }
}
}
15 changes: 7 additions & 8 deletions rustler/src/resource/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ use super::traits;
use super::util::align_alloced_mem_for_struct;
use super::ResourceInitError;
use crate::env::EnvKind;
use crate::{Env, LocalPid, Monitor, Resource};
use rustler_sys::ErlNifResourceDtor;
use rustler_sys::{
c_char, c_void, ErlNifEnv, ErlNifMonitor, ErlNifPid, ErlNifResourceDown, ErlNifResourceFlags,
ErlNifResourceType, ErlNifResourceTypeInit,
use crate::sys::{
c_char, c_void, ErlNifEnv, ErlNifMonitor, ErlNifPid, ErlNifResourceDown, ErlNifResourceDtor,
ErlNifResourceFlags, ErlNifResourceType, ErlNifResourceTypeInit,
};
use crate::{Env, LocalPid, Monitor, Resource};
use std::any::TypeId;
use std::ffi::CString;
use std::mem::MaybeUninit;
Expand Down Expand Up @@ -116,7 +115,7 @@ impl Registration {
if T::IMPLEMENTS_DYNCALL {
Self {
init: ErlNifResourceTypeInit {
dyncall: resource_dyncall::<T> as *const rustler_sys::ErlNifResourceDynCall,
dyncall: resource_dyncall::<T> as *const crate::sys::ErlNifResourceDynCall,
members: max(self.init.members, 4),
..self.init
},
Expand Down Expand Up @@ -230,10 +229,10 @@ type OpenResourceTypeFn = unsafe extern "C" fn(
) -> *const ErlNifResourceType;

#[cfg(feature = "nif_version_2_16")]
static OPEN_RESOURCE_TYPE: OpenResourceTypeFn = rustler_sys::enif_init_resource_type;
static OPEN_RESOURCE_TYPE: OpenResourceTypeFn = crate::sys::enif_init_resource_type;

#[cfg(not(feature = "nif_version_2_16"))]
static OPEN_RESOURCE_TYPE: OpenResourceTypeFn = rustler_sys::enif_open_resource_type_x;
static OPEN_RESOURCE_TYPE: OpenResourceTypeFn = crate::sys::enif_open_resource_type_x;

const fn max(i: i32, j: i32) -> i32 {
if i > j {
Expand Down
Loading

0 comments on commit 6caf207

Please sign in to comment.