From e1d00fe2f14a08fa3c109dd1a225d82d6da409c1 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Wed, 3 Jul 2024 08:12:21 -0500 Subject: [PATCH] linux take 3 --- crates/libs/strings/src/hstring_header.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/libs/strings/src/hstring_header.rs b/crates/libs/strings/src/hstring_header.rs index 5425bf627d..798fd1ecc2 100644 --- a/crates/libs/strings/src/hstring_header.rs +++ b/crates/libs/strings/src/hstring_header.rs @@ -14,7 +14,6 @@ pub struct HStringHeader { } impl HStringHeader { - #[inline] pub fn alloc(len: u32, zero_memory: bool) -> Result<*mut Self> { if len == 0 { return Ok(core::ptr::null_mut()); @@ -24,9 +23,19 @@ impl HStringHeader { // The space for the terminating null character is already accounted for inside of `HStringHeader`. let bytes = core::mem::size_of::() + 2 * len as usize; + #[cfg(windows)] let header = unsafe { bindings::HeapAlloc(bindings::GetProcessHeap(), 0, bytes) } as *mut Self; + #[cfg(not(windows))] + let header = unsafe { + extern "C" { + fn malloc(bytes: usize) -> *mut core::ffi::c_void; + } + + malloc(bytes) as *mut Self + }; + if header.is_null() { return Err(Error::from_hresult(HRESULT(bindings::E_OUTOFMEMORY))); } @@ -46,13 +55,22 @@ impl HStringHeader { Ok(header) } - #[inline] pub unsafe fn free(header: *mut Self) { if header.is_null() { return; } + #[cfg(windows)] bindings::HeapFree(bindings::GetProcessHeap(), 0, header as *mut _); + + #[cfg(not(windows))] + { + extern "C" { + fn free(ptr: *mut core::ffi::c_void); + } + + free(header as *mut _); + } } pub fn duplicate(&self) -> Result<*mut Self> {