-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathos.rs
57 lines (50 loc) · 1.76 KB
/
os.rs
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
#[cfg(windows)]
mod os_defs {
pub use winapi::shared::{
ntdef::{HRESULT, LPCSTR, LPCWSTR, LPSTR, LPWSTR, WCHAR},
wtypes::BSTR,
};
pub use winapi::um::combaseapi::CoTaskMemFree;
pub use winapi::um::oleauto::{SysFreeString, SysStringLen};
}
#[cfg(not(windows))]
mod os_defs {
pub type CHAR = i8;
pub type UINT = u32;
pub type WCHAR = widestring::WideChar;
pub type OLECHAR = WCHAR;
pub type LPSTR = *mut CHAR;
pub type LPWSTR = *mut WCHAR;
pub type LPCSTR = *const CHAR;
pub type LPCWSTR = *const WCHAR;
pub type BSTR = *mut OLECHAR;
pub type LPBSTR = *mut BSTR;
pub type HRESULT = i32;
#[allow(non_snake_case)]
pub unsafe fn CoTaskMemFree(p: *mut libc::c_void) {
// https://github.com/microsoft/DirectXShaderCompiler/blob/a8d9780046cb64a1cea842fa6fc28a250e3e2c09/include/dxc/Support/WinAdapter.h#L46
libc::free(p)
}
#[allow(non_snake_case)]
pub unsafe fn SysFreeString(p: BSTR) {
// TODO: Update link
libc::free((p as *mut libc::c_char).offset(-4) as *mut _)
}
/// Returns the size of `p` in bytes, without terminating NULL character
#[allow(non_snake_case)]
pub unsafe fn SysStringByteLen(p: BSTR) -> UINT {
// The first four bytes before the pointer contain the length prefix:
// https://docs.microsoft.com/en-us/previous-versions/windows/desktop/automat/bstr
if p.is_null() {
0
} else {
*(p as *const UINT).offset(-1)
}
}
/// Returns the size of `p` in characters, without terminating NULL character
#[allow(non_snake_case)]
pub unsafe fn SysStringLen(p: BSTR) -> UINT {
SysStringByteLen(p) / std::mem::size_of::<OLECHAR>() as UINT
}
}
pub use os_defs::*;