-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
74 additions
and
121 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,80 +11,30 @@ | |
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use crate::*; | ||
pub use no_mangle::*; | ||
use zenoh_result::ZResult; | ||
|
||
|
||
/// This number should change any time the [`Compatibility`] structure changes or plugin loading sequence changes | ||
pub type PluginLoaderVersion = u64; | ||
pub const PLUGIN_LOADER_VERSION: PluginLoaderVersion = 1; | ||
|
||
type StartFn<StartArgs, RunningPlugin> = fn(&str, &StartArgs) -> ZResult<RunningPlugin>; | ||
|
||
#[repr(C)] | ||
struct PluginVTableInner<StartArgs, RunningPlugin> { | ||
start: StartFn<StartArgs, RunningPlugin>, | ||
} | ||
|
||
/// Automagical padding such that [PluginVTable::init]'s result is the size of a cache line | ||
#[repr(C)] | ||
struct PluginVTablePadding { | ||
__padding: [u8; PluginVTablePadding::padding_length()], | ||
} | ||
impl PluginVTablePadding { | ||
const fn padding_length() -> usize { | ||
64 - std::mem::size_of::<LoadPluginResultInner>() | ||
} | ||
fn new() -> Self { | ||
PluginVTablePadding { | ||
__padding: [0; Self::padding_length()], | ||
} | ||
} | ||
} | ||
|
||
#[repr(C)] | ||
pub struct PluginVTable<StartArgs, RunningPlugin> { | ||
inner: PluginVTableInner<StartArgs, RunningPlugin>, | ||
padding: PluginVTablePadding, | ||
} | ||
|
||
impl<StartArgs, RunningPlugin> PluginVTable<StartArgs, RunningPlugin> { | ||
pub fn new<ConcretePlugin: Plugin<StartArgs = StartArgs, RunningPlugin = RunningPlugin>>( | ||
) -> Self { | ||
PluginVTable { | ||
inner: PluginVTableInner { | ||
start: ConcretePlugin::start, | ||
}, | ||
padding: PluginVTablePadding::new(), | ||
} | ||
} | ||
pub fn start(&self, name: &str, start_args: &StartArgs) -> ZResult<RunningPlugin> { | ||
(self.inner.start)(name, start_args) | ||
} | ||
} | ||
|
||
pub use no_mangle::*; | ||
#[cfg(feature = "no_mangle")] | ||
pub mod no_mangle { | ||
/// This macro will add a set of non-mangled functions for plugin loading if feature `no_mangle` is enabled (which it is by default). | ||
#[macro_export] | ||
macro_rules! declare_plugin { | ||
($ty: path) => { | ||
($ident: ident, $trait: item) => { | ||
#[no_mangle] | ||
fn get_plugin_loader_version() -> $crate::prelude::PluginVTableVersion { | ||
fn get_plugin_loader_version() -> $crate::prelude::PluginLoaderVersion { | ||
$crate::prelude::PLUGIN_LOADER_VERSION | ||
} | ||
#[no_mangle] | ||
fn get_plugin_vtable_compatibility() -> $crate::Compatibility { | ||
<<$ty as $crate::prelude::Plugin>::StartArgs as $crate::prelude::Validator>::compatibility() | ||
fn get_plugin_compatibility() -> $crate::Compatibility { | ||
<$item as $trait>::compatibility() | ||
} | ||
#[no_mangle] | ||
fn get_plugin_vtable() -> $crate::prelude::PluginVTable< | ||
<$ty as $crate::prelude::Plugin>::StartArgs, | ||
<$ty as $crate::prelude::Plugin>::RunningPlugin, | ||
> { | ||
$crate::prelude::PluginVTable::new::<$ty>() | ||
fn get_plugin_trait() -> &'static dyn $trait { | ||
&$ident as &dyn $trait | ||
} | ||
}; | ||
} | ||
|
@@ -93,6 +43,6 @@ pub mod no_mangle { | |
pub mod no_mangle { | ||
#[macro_export] | ||
macro_rules! declare_plugin { | ||
($ty: path) => {}; | ||
($ident: ident, $trait: item) => {}; | ||
} | ||
} |
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