xlang_interface/
lib.rs

1#![deny(warnings, clippy::all, clippy::pedantic, clippy::nursery)]
2#![allow(clippy::missing_safety_doc)] // FIXME: Remove this allow
3use std::alloc::Layout;
4
5use xlang_abi::{const_sv, span::Span, string::StringView};
6use xlang_targets::{properties::TargetProperties, Target};
7
8#[no_mangle]
9pub extern "C" fn xlang_allocate(size: usize) -> *mut core::ffi::c_void {
10    if size == 0 {
11        return 32usize as *mut core::ffi::c_void;
12    }
13    unsafe {
14        xlang_allocate_aligned(
15            size,
16            if size > 32 {
17                32
18            } else {
19                size.next_power_of_two()
20            },
21        )
22    }
23}
24
25#[no_mangle]
26pub unsafe extern "C" fn xlang_allocate_aligned(
27    size: usize,
28    align: usize,
29) -> *mut core::ffi::c_void {
30    if size == 0 {
31        return align as *mut core::ffi::c_void;
32    }
33    let size = size + (align - size % align) % align;
34    let layout = Layout::from_size_align_unchecked(size, align);
35    std::alloc::alloc(layout).cast::<_>()
36}
37
38#[no_mangle]
39pub unsafe extern "C" fn xlang_deallocate(ptr: *mut core::ffi::c_void, size: usize) {
40    if size == 0 {
41        return;
42    }
43    #[allow(unused_unsafe)]
44    unsafe {
45        xlang_deallocate_aligned(
46            ptr,
47            size,
48            if size > 32 {
49                32
50            } else {
51                size.next_power_of_two()
52            },
53        );
54    }
55}
56
57#[no_mangle]
58pub unsafe extern "C" fn xlang_deallocate_aligned(
59    ptr: *mut core::ffi::c_void,
60    size: usize,
61    align: usize,
62) {
63    if size == 0 || ptr.is_null() {
64        return;
65    }
66    let size = size + (align - size % align) % align;
67    let layout = Layout::from_size_align_unchecked(size, align);
68    std::alloc::dealloc(ptr.cast::<_>(), layout);
69}
70
71#[no_mangle]
72pub extern "C" fn xlang_on_allocation_failure(size: usize, align: usize) -> ! {
73    eprintln!(
74        "Failed to allocate with size: {}, and alignment: {}",
75        size, align
76    );
77    std::process::abort()
78}
79
80#[no_mangle]
81pub extern "C" fn xlang_get_target_properties(
82    targ: Target,
83) -> Option<&'static TargetProperties<'static>> {
84    #[allow(deprecated)]
85    xlang_targets::properties::__get_properties(targ)
86}
87
88#[no_mangle]
89#[allow(clippy::missing_const_for_fn)] // const extern fn is unstable
90pub extern "C" fn xlang_get_version() -> StringView<'static> {
91    const_sv!("0.1")
92}
93
94#[no_mangle]
95pub static XLANG_HASH_SEED: u8 = 254u8;
96
97const PRIME: u64 = 1_099_511_628_211;
98
99lazy_static::lazy_static! {
100    static ref HASH_SEED_ACTUAL: u64 = rand::random::<u64>()^14_695_981_039_346_656_037;
101}
102
103xlang_host::rustcall! {
104    #[no_mangle]
105    pub extern "rustcall" fn xlang_hash_bytes(bytes: Span<u8>) -> u64{
106        let mut hash = *HASH_SEED_ACTUAL;
107
108        for &byte in bytes{
109            hash ^= u64::from(byte);
110            hash = hash.wrapping_mul(PRIME);
111        }
112        hash
113    }
114}