1// SPDX-License-Identifier: CC0-1.0
23#![allow(non_camel_case_types)]
45pub type c_int = i32;
6pub type c_uchar = u8;
7pub type c_uint = u32;
8pub type size_t = usize;
910/// This might not match C's `c_char` exactly.
11/// The way we use it makes it fine either way but this type shouldn't be used outside of the library.
12pub type c_char = i8;
1314pub use core::ffi::c_void;
1516/// A type that is as aligned as the biggest alignment for fundamental types in C
17/// since C11 that means as aligned as `max_align_t` is.
18/// the exact size/alignment is unspecified.
19// 16 matches is as big as the biggest alignment in any arch that rust currently supports https://github.com/rust-lang/rust/blob/2c31b45ae878b821975c4ebd94cc1e49f6073fd0/library/std/src/sys_common/alloc.rs
20#[repr(align(16))]
21#[derive(Default, Copy, Clone)]
22#[allow(dead_code)] // We never access the inner data directly, only by way of a pointer.
23pub struct AlignedType([u8; 16]);
2425impl AlignedType {
26pub fn zeroed() -> Self {
27 AlignedType([0u8; 16])
28 }
2930/// A static zeroed out AlignedType for use in static assignments of [AlignedType; _]
31pub const ZERO: AlignedType = AlignedType([0u8; 16]);
32}
3334#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))]
35pub(crate) const ALIGN_TO: usize = core::mem::align_of::<AlignedType>();
3637#[cfg(test)]
38mod tests {
39extern crate libc;
40use std::any::TypeId;
41use std::mem;
42use std::os::raw;
43use crate::{types, AlignedType};
4445#[test]
46fn verify_types() {
47assert_eq!(TypeId::of::<types::c_int>(), TypeId::of::<raw::c_int>());
48assert_eq!(TypeId::of::<types::c_uchar>(), TypeId::of::<raw::c_uchar>());
49assert_eq!(TypeId::of::<types::c_uint>(), TypeId::of::<raw::c_uint>());
50assert_eq!(TypeId::of::<types::c_char>(), TypeId::of::<raw::c_char>());
5152assert!(mem::align_of::<AlignedType>() >= mem::align_of::<self::libc::max_align_t>());
53 }
54}
5556#[doc(hidden)]
57#[cfg(target_arch = "wasm32")]
58pub fn sanity_checks_for_wasm() {
59use core::mem::{align_of, size_of};
60extern "C" {
61pub static WASM32_INT_SIZE: c_uchar;
62pub static WASM32_INT_ALIGN: c_uchar;
6364pub static WASM32_UNSIGNED_INT_SIZE: c_uchar;
65pub static WASM32_UNSIGNED_INT_ALIGN: c_uchar;
6667pub static WASM32_SIZE_T_SIZE: c_uchar;
68pub static WASM32_SIZE_T_ALIGN: c_uchar;
6970pub static WASM32_UNSIGNED_CHAR_SIZE: c_uchar;
71pub static WASM32_UNSIGNED_CHAR_ALIGN: c_uchar;
7273pub static WASM32_PTR_SIZE: c_uchar;
74pub static WASM32_PTR_ALIGN: c_uchar;
75 }
76unsafe {
77assert_eq!(size_of::<c_int>(), WASM32_INT_SIZE as usize);
78assert_eq!(align_of::<c_int>(), WASM32_INT_ALIGN as usize);
7980assert_eq!(size_of::<c_uint>(), WASM32_UNSIGNED_INT_SIZE as usize);
81assert_eq!(align_of::<c_uint>(), WASM32_UNSIGNED_INT_ALIGN as usize);
8283assert_eq!(size_of::<size_t>(), WASM32_SIZE_T_SIZE as usize);
84assert_eq!(align_of::<size_t>(), WASM32_SIZE_T_ALIGN as usize);
8586assert_eq!(size_of::<c_uchar>(), WASM32_UNSIGNED_CHAR_SIZE as usize);
87assert_eq!(align_of::<c_uchar>(), WASM32_UNSIGNED_CHAR_ALIGN as usize);
8889assert_eq!(size_of::<*const ()>(), WASM32_PTR_SIZE as usize);
90assert_eq!(align_of::<*const ()>(), WASM32_PTR_ALIGN as usize);
91 }
92}