Skip to content

Commit

Permalink
linux take 3
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Jul 3, 2024
1 parent f7f904c commit e1d00fe
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions crates/libs/strings/src/hstring_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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::<Self>() + 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)));
}
Expand All @@ -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> {
Expand Down

0 comments on commit e1d00fe

Please sign in to comment.