summaryrefslogtreecommitdiffstats
path: root/vendor/sysinfo/src/apple/utils.rs
blob: 408c02c31d2ec0c63373b33c399d31c775cf8bec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Take a look at the license at the top of the repository in the LICENSE file.

use core_foundation_sys::base::CFRelease;
use libc::c_char;
use std::ptr::NonNull;

// A helper using to auto release the resource got from CoreFoundation.
// More information about the ownership policy for CoreFoundation pelease refer the link below:
// https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html#//apple_ref/doc/uid/20001148-CJBEJBHH
#[repr(transparent)]
pub(crate) struct CFReleaser<T>(NonNull<T>);

impl<T> CFReleaser<T> {
    pub(crate) fn new(ptr: *const T) -> Option<Self> {
        // This cast is OK because `NonNull` is a transparent wrapper
        // over a `*const T`. Additionally, mutability doesn't matter with
        // pointers here.
        NonNull::new(ptr as *mut T).map(Self)
    }

    pub(crate) fn inner(&self) -> *const T {
        self.0.as_ptr().cast()
    }
}

impl<T> Drop for CFReleaser<T> {
    fn drop(&mut self) {
        unsafe { CFRelease(self.0.as_ptr().cast()) }
    }
}

// Safety: These are safe to implement because we only wrap non-mutable
// CoreFoundation types, which are generally threadsafe unless noted
// otherwise.
unsafe impl<T> Send for CFReleaser<T> {}
unsafe impl<T> Sync for CFReleaser<T> {}

pub(crate) fn cstr_to_rust(c: *const c_char) -> Option<String> {
    cstr_to_rust_with_size(c, None)
}

pub(crate) fn cstr_to_rust_with_size(c: *const c_char, size: Option<usize>) -> Option<String> {
    if c.is_null() {
        return None;
    }
    let mut s = match size {
        Some(len) => Vec::with_capacity(len),
        None => Vec::new(),
    };
    let mut i = 0;
    unsafe {
        loop {
            let value = *c.offset(i) as u8;
            if value == 0 {
                break;
            }
            s.push(value);
            i += 1;
        }
        String::from_utf8(s).ok()
    }
}

pub(crate) fn vec_to_rust(buf: Vec<i8>) -> Option<String> {
    String::from_utf8(
        buf.into_iter()
            .flat_map(|b| if b > 0 { Some(b as u8) } else { None })
            .collect(),
    )
    .ok()
}