diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/ntapi/src/macros.rs | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/ntapi/src/macros.rs')
-rw-r--r-- | third_party/rust/ntapi/src/macros.rs | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/third_party/rust/ntapi/src/macros.rs b/third_party/rust/ntapi/src/macros.rs new file mode 100644 index 0000000000..92cdd96784 --- /dev/null +++ b/third_party/rust/ntapi/src/macros.rs @@ -0,0 +1,112 @@ +#[macro_export] +macro_rules! EXTERN { + (extern $c:tt {$( + fn $n:ident ($( $p:tt $(: $t:ty)?),* $(,)?) $(-> $r:ty)?; + )+}) => { + #[cfg_attr(all(target_env = "msvc", feature = "user"), link(name = "ntdll"))] + #[cfg_attr(all(target_env = "msvc", feature = "kernel"), link(name = "ntoskrnl"))] + extern $c {$( + pub fn $n( + $($p $(: $t)?),* + ) $(-> $r)?; + )+} + $( + #[cfg(feature = "func-types")] + pub type $n = unsafe extern $c fn($($p $(: $t)?),*) $(-> $r)?; + )+ + }; + (extern $c:tt {$( + static mut $n:ident : $t:ty; + )+}) => { + #[cfg_attr(all(target_env = "msvc", feature = "user"), link(name = "ntdll"))] + extern $c {$( + pub static mut $n: $t; + )+} + }; +} +#[macro_export] +#[doc(hidden)] +macro_rules! FIELD_OFFSET { + ($_type:ty, $field:ident$(.$cfields:ident)*) => { + unsafe { + union Transmuter<T: 'static> { + p: *const T, + r: &'static T, + i: usize, + } + #[allow(unaligned_references)] + Transmuter { + r: &(&Transmuter { + p: $crate::_core::ptr::null::<$_type>() + }.r).$field$(.$cfields)* + }.i + } + }; +} +macro_rules! BITFIELD { + ($base:ident $field:ident: $fieldtype:ty [ + $($thing:ident $set_thing:ident[$r:expr],)+ + ]) => { + impl $base {$( + #[inline] + pub const fn $thing(&self) -> $fieldtype { + const SIZE: usize = $crate::_core::mem::size_of::<$fieldtype>() * 8; + self.$field << (SIZE - $r.end) >> (SIZE - $r.end + $r.start) + } + #[inline] + pub fn $set_thing(&mut self, val: $fieldtype) { + const MASK: $fieldtype = ((1 << ($r.end - $r.start)) - 1) << $r.start; + self.$field &= !MASK; + self.$field |= (val << $r.start) & MASK; + } + )+} + }; + (unsafe $base:ident $field:ident: $fieldtype:ty [ + $($thing:ident $set_thing:ident[$r:expr],)+ + ]) => { + impl $base {$( + #[inline] + pub unsafe fn $thing(&self) -> $fieldtype { + const SIZE: usize = $crate::_core::mem::size_of::<$fieldtype>() * 8; + self.$field << (SIZE - $r.end) >> (SIZE - $r.end + $r.start) + } + #[inline] + pub unsafe fn $set_thing(&mut self, val: $fieldtype) { + const MASK: $fieldtype = ((1 << ($r.end - $r.start)) - 1) << $r.start; + self.$field &= !MASK; + self.$field |= (val << $r.start) & MASK; + } + )+} + }; +} +macro_rules! UNION { + ($(#[$attrs:meta])* union $name:ident { + $($variant:ident: $ftype:ty,)+ + }) => ( + #[repr(C)] $(#[$attrs])* + pub union $name { + $(pub $variant: $ftype,)+ + } + impl Copy for $name {} + impl Clone for $name { + #[inline] + fn clone(&self) -> $name { *self } + } + #[cfg(feature = "impl-default")] + impl Default for $name { + #[inline] + fn default() -> $name { unsafe { $crate::_core::mem::zeroed() } } + } + ); +} +macro_rules! FN { + (stdcall $func:ident($($p:ident: $t:ty,)*) -> $ret:ty) => ( + pub type $func = Option<unsafe extern "system" fn($($p: $t,)*) -> $ret>; + ); + (cdecl $func:ident($($p:ident: $t:ty,)*) -> $ret:ty) => ( + pub type $func = Option<unsafe extern "C" fn($($p: $t,)*) -> $ret>; + ); +} +macro_rules! IFDEF { + ($($thing:item)*) => ($($thing)*) +} |