Update comms/gnuradio to version 3.8.0.0_3
[dports.git] / lang / rust / files / patch-src_stdsimd_crates_std__detect_src_detect_os_freebsd_auxvec.rs
1 --- src/stdsimd/crates/std_detect/src/detect/os/freebsd/auxvec.rs.orig  2019-04-25 17:33:21 UTC
2 +++ src/stdsimd/crates/std_detect/src/detect/os/freebsd/auxvec.rs
3 @@ -0,0 +1,86 @@
4 +//! Parses ELF auxiliary vectors.
5 +#![cfg_attr(any(target_arch = "arm", target_arch = "powerpc64"), allow(dead_code))]
6 +
7 +/// Key to access the CPU Hardware capabilities bitfield.
8 +pub(crate) const AT_HWCAP: usize = 25;
9 +/// Key to access the CPU Hardware capabilities 2 bitfield.
10 +pub(crate) const AT_HWCAP2: usize = 26;
11 +
12 +/// Cache HWCAP bitfields of the ELF Auxiliary Vector.
13 +///
14 +/// If an entry cannot be read all the bits in the bitfield are set to zero.
15 +/// This should be interpreted as all the features being disabled.
16 +#[derive(Debug, Copy, Clone)]
17 +pub(crate) struct AuxVec {
18 +    pub hwcap: usize,
19 +    pub hwcap2: usize,
20 +}
21 +
22 +/// ELF Auxiliary Vector
23 +///
24 +/// The auxiliary vector is a memory region in a running ELF program's stack
25 +/// composed of (key: usize, value: usize) pairs.
26 +///
27 +/// The keys used in the aux vector are platform dependent. For FreeBSD, they are
28 +/// defined in [sys/elf_common.h][elf_common_h]. The hardware capabilities of a given
29 +/// CPU can be queried with the  `AT_HWCAP` and `AT_HWCAP2` keys.
30 +///
31 +/// Note that run-time feature detection is not invoked for features that can
32 +/// be detected at compile-time.
33 +///
34 +/// [elf_common.h]: https://svnweb.freebsd.org/base/release/12.0.0/sys/sys/elf_common.h?revision=341707
35 +pub(crate) fn auxv() -> Result<AuxVec, ()> {
36 +    if let Ok(hwcap) = archauxv(AT_HWCAP) {
37 +        if let Ok(hwcap2) = archauxv(AT_HWCAP2) {
38 +            if hwcap != 0 && hwcap2 != 0 {
39 +                return Ok(AuxVec { hwcap, hwcap2 });
40 +            }
41 +        }
42 +    }
43 +    Err(())
44 +}
45 +
46 +/// Tries to read the `key` from the auxiliary vector.
47 +fn archauxv(key: usize) -> Result<usize, ()> {
48 +    use crate::mem;
49 +
50 +    #[derive (Copy, Clone)]
51 +    #[repr(C)]
52 +    pub struct Elf_Auxinfo {
53 +        pub a_type: usize,
54 +        pub a_un: unnamed,
55 +    }
56 +    #[derive (Copy, Clone)]
57 +    #[repr(C)]
58 +    pub union unnamed {
59 +        pub a_val: libc::c_long,
60 +        pub a_ptr: *mut libc::c_void,
61 +        pub a_fcn: Option<unsafe extern "C" fn() -> ()>,
62 +    }
63 +
64 +    let mut auxv: [Elf_Auxinfo; 27] =
65 +        [Elf_Auxinfo{a_type: 0, a_un: unnamed{a_val: 0,},}; 27];
66 +
67 +    let mut len: libc::c_uint = mem::size_of_val(&auxv) as libc::c_uint;
68 +
69 +    unsafe {
70 +        let mut mib = [libc::CTL_KERN, libc::KERN_PROC, libc::KERN_PROC_AUXV, libc::getpid()];
71 +    
72 +        let ret = libc::sysctl(mib.as_mut_ptr(),
73 +                       mib.len() as u32,
74 +                       &mut auxv as *mut _ as *mut _,
75 +                       &mut len as *mut _ as *mut _,
76 +                       0 as *mut libc::c_void,
77 +                       0,
78 +                );
79 +    
80 +        if ret != -1 {
81 +            for i in 0..auxv.len() {
82 +                if auxv[i].a_type == key {
83 +                    return Ok(auxv[i].a_un.a_val as usize);
84 +                }
85 +            }
86 +        }
87 +    }
88 +    return Ok(0);
89 +}