2024-09-15 10:30:22 +10:00
|
|
|
macro_rules! wrap_ok {
|
|
|
|
($e:expr) => {
|
|
|
|
::core::iter::empty().try_fold($e, |_, __x: ::core::convert::Infallible| match __x {})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-04 10:32:10 +11:00
|
|
|
macro_rules! ffi_body {
|
2023-04-23 16:01:31 +10:00
|
|
|
(nopanic $body:block) => {
|
2022-12-04 10:32:10 +11:00
|
|
|
{
|
2024-09-15 10:30:22 +10:00
|
|
|
let result: Result<(), $crate::error::LibrashaderError> = (|| $crate::ffi::wrap_ok!({
|
2022-12-04 10:32:10 +11:00
|
|
|
$body
|
2024-09-15 10:30:22 +10:00
|
|
|
}))();
|
2022-12-04 10:32:10 +11:00
|
|
|
|
|
|
|
let Err(e) = result else {
|
|
|
|
return $crate::error::LibrashaderError::ok()
|
|
|
|
};
|
|
|
|
e.export()
|
|
|
|
}
|
|
|
|
};
|
2023-04-23 16:01:31 +10:00
|
|
|
($body:block) => {
|
|
|
|
{
|
|
|
|
let result = ::std::panic::catch_unwind(::std::panic::AssertUnwindSafe(|| {
|
|
|
|
$crate::ffi::ffi_body!(nopanic $body)
|
|
|
|
}));
|
|
|
|
|
|
|
|
result.unwrap_or_else(|e| $crate::error::LibrashaderError::UnknownError(e).export())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
(nopanic |$($ref_capture:ident),*|; mut |$($mut_capture:ident),*| $body:block) => {
|
2022-12-04 10:32:10 +11:00
|
|
|
{
|
2024-09-15 10:30:22 +10:00
|
|
|
$($crate::error::assert_non_null!(@EXPORT $ref_capture);)*
|
2022-12-04 10:32:10 +11:00
|
|
|
$(let $ref_capture = unsafe { &*$ref_capture };)*
|
2024-09-15 10:30:22 +10:00
|
|
|
$($crate::error::assert_non_null!(@EXPORT $mut_capture);)*
|
2022-12-04 10:32:10 +11:00
|
|
|
$(let $mut_capture = unsafe { &mut *$mut_capture };)*
|
2024-09-15 10:30:22 +10:00
|
|
|
let result: Result<(), $crate::error::LibrashaderError> = (|| $crate::ffi::wrap_ok!({
|
2022-12-04 10:32:10 +11:00
|
|
|
$body
|
2024-09-15 10:30:22 +10:00
|
|
|
}))();
|
2022-12-04 10:32:10 +11:00
|
|
|
|
|
|
|
let Err(e) = result else {
|
|
|
|
return $crate::error::LibrashaderError::ok()
|
|
|
|
};
|
|
|
|
e.export()
|
|
|
|
}
|
|
|
|
};
|
2023-04-23 16:01:31 +10:00
|
|
|
(|$($ref_capture:ident),*|; mut |$($mut_capture:ident),*| $body:block) => {
|
|
|
|
{
|
|
|
|
let result = ::std::panic::catch_unwind(::std::panic::AssertUnwindSafe(|| {
|
|
|
|
$crate::ffi::ffi_body!(nopanic |$($ref_capture),*|; mut |$($mut_capture),*| $body)
|
|
|
|
}));
|
|
|
|
|
|
|
|
result.unwrap_or_else(|e| $crate::error::LibrashaderError::UnknownError(e).export())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
(nopanic mut |$($mut_capture:ident),*| $body:block) => {
|
2022-12-04 10:32:10 +11:00
|
|
|
{
|
2024-09-15 10:30:22 +10:00
|
|
|
$($crate::error::assert_non_null!(@EXPORT $mut_capture);)*
|
2022-12-04 10:32:10 +11:00
|
|
|
$(let $mut_capture = unsafe { &mut *$mut_capture };)*
|
2024-09-15 10:30:22 +10:00
|
|
|
let result: Result<(), $crate::error::LibrashaderError> = (|| $crate::ffi::wrap_ok!({
|
2022-12-04 10:32:10 +11:00
|
|
|
$body
|
2024-09-15 10:30:22 +10:00
|
|
|
}))();
|
2022-12-04 10:32:10 +11:00
|
|
|
|
|
|
|
let Err(e) = result else {
|
|
|
|
return $crate::error::LibrashaderError::ok()
|
|
|
|
};
|
|
|
|
e.export()
|
|
|
|
}
|
|
|
|
};
|
2023-04-23 16:01:31 +10:00
|
|
|
(mut |$($mut_capture:ident),*| $body:block) => {
|
|
|
|
{
|
|
|
|
let result = ::std::panic::catch_unwind(::std::panic::AssertUnwindSafe(|| {
|
|
|
|
$crate::ffi::ffi_body!(nopanic mut |$($mut_capture),*| $body)
|
|
|
|
}));
|
|
|
|
|
|
|
|
result.unwrap_or_else(|e| $crate::error::LibrashaderError::UnknownError(e).export())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
(nopanic |$($ref_capture:ident),*| $body:block) => {
|
2022-12-04 10:32:10 +11:00
|
|
|
{
|
2024-09-15 10:30:22 +10:00
|
|
|
$($crate::error::assert_non_null!(@EXPORT $ref_capture);)*
|
2022-12-04 10:32:10 +11:00
|
|
|
$(let $ref_capture = unsafe { &*$ref_capture };)*
|
2024-09-15 10:30:22 +10:00
|
|
|
let result: Result<(), $crate::error::LibrashaderError> = (|| $crate::ffi::wrap_ok!({
|
2022-12-04 10:32:10 +11:00
|
|
|
$body
|
2024-09-15 10:30:22 +10:00
|
|
|
}))();
|
2022-12-04 10:32:10 +11:00
|
|
|
|
|
|
|
let Err(e) = result else {
|
|
|
|
return $crate::error::LibrashaderError::ok()
|
|
|
|
};
|
|
|
|
e.export()
|
|
|
|
}
|
2023-04-23 16:01:31 +10:00
|
|
|
};
|
|
|
|
(|$($ref_capture:ident),*| $body:block) => {
|
|
|
|
{
|
|
|
|
let result = ::std::panic::catch_unwind(::std::panic::AssertUnwindSafe(|| {
|
|
|
|
$crate::ffi::ffi_body!(nopanic |$($ref_capture),*| $body)
|
|
|
|
}));
|
|
|
|
|
|
|
|
result.unwrap_or_else(|e| $crate::error::LibrashaderError::UnknownError(e).export())
|
|
|
|
}
|
|
|
|
};
|
2022-12-04 10:32:10 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! extern_fn {
|
2023-04-23 16:01:31 +10:00
|
|
|
// raw doesn't wrap in ffi_body
|
|
|
|
($(#[$($attrss:tt)*])* raw fn $func_name:ident ($($arg_name:ident : $arg_ty:ty),* $(,)?) $body:block) => {
|
2023-01-14 10:10:09 +11:00
|
|
|
::paste::paste! {
|
|
|
|
/// Function pointer definition for
|
|
|
|
#[doc = ::std::stringify!($func_name)]
|
2023-04-23 16:01:31 +10:00
|
|
|
pub type [<PFN_ $func_name>] = unsafe extern "C" fn($($arg_name: $arg_ty,)* ) -> $crate::ctypes::libra_error_t;
|
2022-12-04 10:32:10 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
$(#[$($attrss)*])*
|
2023-01-16 03:08:13 +11:00
|
|
|
pub unsafe extern "C" fn $func_name($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t {
|
2023-04-23 16:01:31 +10:00
|
|
|
$body
|
2022-12-04 10:32:10 +11:00
|
|
|
}
|
2023-01-14 08:05:13 +11:00
|
|
|
};
|
|
|
|
|
2023-04-23 16:01:31 +10:00
|
|
|
// ffi_body but panic-safe
|
|
|
|
($(#[$($attrss:tt)*])* fn $func_name:ident ($($arg_name:ident : $arg_ty:ty),* $(,)?) $body:block) => {
|
2023-01-14 10:10:09 +11:00
|
|
|
::paste::paste! {
|
|
|
|
/// Function pointer definition for
|
|
|
|
#[doc = ::std::stringify!($func_name)]
|
2023-04-23 16:01:31 +10:00
|
|
|
pub type [<PFN_ $func_name>] = unsafe extern "C" fn($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t;
|
2023-01-14 08:05:13 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
$(#[$($attrss)*])*
|
2023-01-16 03:08:13 +11:00
|
|
|
pub unsafe extern "C" fn $func_name($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t {
|
2023-04-23 16:01:31 +10:00
|
|
|
$crate::ffi::ffi_body!($body)
|
2023-01-14 08:05:13 +11:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-02-10 15:36:09 +11:00
|
|
|
($(#[$($attrss:tt)*])* fn $func_name:ident ($($arg_name:ident : $arg_ty:ty),* $(,)?) |$($ref_capture:ident),*|; mut |$($mut_capture:ident),*| $body:block) => {
|
2023-01-14 10:10:09 +11:00
|
|
|
::paste::paste! {
|
|
|
|
/// Function pointer definition for
|
|
|
|
#[doc = ::std::stringify!($func_name)]
|
2023-01-14 08:05:13 +11:00
|
|
|
pub type [<PFN_ $func_name>] = unsafe extern "C" fn($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
$(#[$($attrss)*])*
|
2023-01-16 03:08:13 +11:00
|
|
|
pub unsafe extern "C" fn $func_name($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t {
|
2023-01-14 08:05:13 +11:00
|
|
|
$crate::ffi::ffi_body!(|$($ref_capture),*|; mut |$($mut_capture),*| $body)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-02-10 15:36:09 +11:00
|
|
|
($(#[$($attrss:tt)*])* fn $func_name:ident ($($arg_name:ident : $arg_ty:ty),* $(,)?) mut |$($mut_capture:ident),*| $body:block) => {
|
2023-01-14 10:10:09 +11:00
|
|
|
::paste::paste! {
|
|
|
|
/// Function pointer definition for
|
|
|
|
#[doc = ::std::stringify!($func_name)]
|
2023-01-14 08:05:13 +11:00
|
|
|
pub type [<PFN_ $func_name>] = unsafe extern "C" fn($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
$(#[$($attrss)*])*
|
2023-01-16 03:08:13 +11:00
|
|
|
pub unsafe extern "C" fn $func_name($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t {
|
2023-01-14 08:05:13 +11:00
|
|
|
$crate::ffi::ffi_body!(mut |$($mut_capture),*| $body)
|
|
|
|
}
|
|
|
|
};
|
2023-02-10 15:36:09 +11:00
|
|
|
($(#[$($attrss:tt)*])* fn $func_name:ident ($($arg_name:ident : $arg_ty:ty),* $(,)?) |$($ref_capture:ident),*| $body:block) => {
|
2023-01-14 10:10:09 +11:00
|
|
|
::paste::paste! {
|
|
|
|
/// Function pointer definition for
|
|
|
|
#[doc = ::std::stringify!($func_name)]
|
|
|
|
pub type [<PFN_ $func_name>] = unsafe extern "C" fn($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t;
|
2023-01-14 08:05:13 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
$(#[$($attrss)*])*
|
2023-01-16 03:08:13 +11:00
|
|
|
pub unsafe extern "C" fn $func_name($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t {
|
2023-01-14 08:05:13 +11:00
|
|
|
$crate::ffi::ffi_body!(|$($ref_capture),*| $body)
|
|
|
|
}
|
|
|
|
};
|
2023-04-23 16:01:31 +10:00
|
|
|
|
|
|
|
// nopanic variants that are UB if panics
|
|
|
|
($(#[$($attrss:tt)*])* nopanic fn $func_name:ident ($($arg_name:ident : $arg_ty:ty),* $(,)?) $body:block) => {
|
|
|
|
::paste::paste! {
|
|
|
|
/// Function pointer definition for
|
|
|
|
#[doc = ::std::stringify!($func_name)]
|
|
|
|
pub type [<PFN_ $func_name>] = unsafe extern "C" fn($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
$(#[$($attrss)*])*
|
|
|
|
pub unsafe extern "C" fn $func_name($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t {
|
|
|
|
$crate::ffi::ffi_body!(nopanic $body)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($(#[$($attrss:tt)*])* nopanic fn $func_name:ident ($($arg_name:ident : $arg_ty:ty),* $(,)?) |$($ref_capture:ident),*|; mut |$($mut_capture:ident),*| $body:block) => {
|
|
|
|
::paste::paste! {
|
|
|
|
/// Function pointer definition for
|
|
|
|
#[doc = ::std::stringify!($func_name)]
|
|
|
|
pub type [<PFN_ $func_name>] = unsafe extern "C" fn($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
$(#[$($attrss)*])*
|
|
|
|
pub unsafe extern "C" fn $func_name($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t {
|
|
|
|
$crate::ffi::ffi_body!(nopanic |$($ref_capture),*|; mut |$($mut_capture),*| $body)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($(#[$($attrss:tt)*])* nopanic fn $func_name:ident ($($arg_name:ident : $arg_ty:ty),* $(,)?) mut |$($mut_capture:ident),*| $body:block) => {
|
|
|
|
::paste::paste! {
|
|
|
|
/// Function pointer definition for
|
|
|
|
#[doc = ::std::stringify!($func_name)]
|
|
|
|
pub type [<PFN_ $func_name>] = unsafe extern "C" fn($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
$(#[$($attrss)*])*
|
|
|
|
pub unsafe extern "C" fn $func_name($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t {
|
|
|
|
$crate::ffi::ffi_body!(nopanic mut |$($mut_capture),*| $body)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
($(#[$($attrss:tt)*])* nopanic fn $func_name:ident ($($arg_name:ident : $arg_ty:ty),* $(,)?) |$($ref_capture:ident),*| $body:block) => {
|
|
|
|
::paste::paste! {
|
|
|
|
/// Function pointer definition for
|
|
|
|
#[doc = ::std::stringify!($func_name)]
|
|
|
|
pub type [<PFN_ $func_name>] = unsafe extern "C" fn($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
$(#[$($attrss)*])*
|
|
|
|
pub unsafe extern "C" fn $func_name($($arg_name: $arg_ty,)*) -> $crate::ctypes::libra_error_t {
|
|
|
|
$crate::ffi::ffi_body!(nopanic |$($ref_capture),*| $body)
|
|
|
|
}
|
|
|
|
};
|
2022-12-04 10:32:10 +11:00
|
|
|
}
|
|
|
|
|
2024-02-16 17:12:48 +11:00
|
|
|
pub fn boxed_slice_into_raw_parts<T>(vec: Box<[T]>) -> (*mut T, usize) {
|
|
|
|
let mut me = ManuallyDrop::new(vec);
|
|
|
|
(me.as_mut_ptr(), me.len())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn boxed_slice_from_raw_parts<T>(ptr: *mut T, len: usize) -> Box<[T]> {
|
|
|
|
unsafe { Box::from_raw(std::slice::from_raw_parts_mut(ptr, len)) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ptr_is_aligned<T: Sized>(ptr: *const T) -> bool {
|
|
|
|
let align = std::mem::align_of::<T>();
|
|
|
|
if !align.is_power_of_two() {
|
|
|
|
panic!("is_aligned_to: align is not a power-of-two");
|
|
|
|
}
|
2024-09-15 10:30:22 +10:00
|
|
|
sptr::Strict::addr(ptr) & (align - 1) == 0
|
2024-02-16 17:12:48 +11:00
|
|
|
}
|
|
|
|
|
2022-12-04 10:32:10 +11:00
|
|
|
pub(crate) use extern_fn;
|
2022-12-05 16:06:37 +11:00
|
|
|
pub(crate) use ffi_body;
|
2024-09-15 10:30:22 +10:00
|
|
|
pub(crate) use wrap_ok;
|
|
|
|
|
2024-02-16 17:12:48 +11:00
|
|
|
use std::mem::ManuallyDrop;
|