ffi.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // SPDX-License-Identifier: GPL-2.0
  2. //! Foreign function interface (FFI) types.
  3. //!
  4. //! This crate provides mapping from C primitive types to Rust ones.
  5. //!
  6. //! The Rust [`core`] crate provides [`core::ffi`], which maps integer types to the platform default
  7. //! C ABI. The kernel does not use [`core::ffi`], so it can customise the mapping that deviates from
  8. //! the platform default.
  9. #![no_std]
  10. macro_rules! alias {
  11. ($($name:ident = $ty:ty;)*) => {$(
  12. #[allow(non_camel_case_types, missing_docs)]
  13. pub type $name = $ty;
  14. // Check size compatibility with `core`.
  15. const _: () = assert!(
  16. core::mem::size_of::<$name>() == core::mem::size_of::<core::ffi::$name>()
  17. );
  18. )*}
  19. }
  20. alias! {
  21. // `core::ffi::c_char` is either `i8` or `u8` depending on architecture. In the kernel, we use
  22. // `-funsigned-char` so it's always mapped to `u8`.
  23. c_char = u8;
  24. c_schar = i8;
  25. c_uchar = u8;
  26. c_short = i16;
  27. c_ushort = u16;
  28. c_int = i32;
  29. c_uint = u32;
  30. // In the kernel, `intptr_t` is defined to be `long` in all platforms, so we can map the type to
  31. // `isize`.
  32. c_long = isize;
  33. c_ulong = usize;
  34. c_longlong = i64;
  35. c_ulonglong = u64;
  36. }
  37. pub use core::ffi::c_void;