Skip to content

Commit

Permalink
Use malloc on non-Windows platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Arlie Davis committed Jun 12, 2024
1 parent 8e0e96d commit 25918dc
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions crates/libs/core/src/imp/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ use core::ffi::c_void;
/// This function will fail in OOM situations, if the heap is otherwise corrupt,
/// or if getting a handle to the process heap fails.
pub fn heap_alloc(bytes: usize) -> crate::Result<*mut c_void> {
let ptr = unsafe { HeapAlloc(GetProcessHeap(), 0, bytes) };
#[cfg(windows)]
let ptr: *mut c_void = unsafe { HeapAlloc(GetProcessHeap(), 0, bytes) };

#[cfg(not(windows))]
let ptr: *mut c_void = unsafe {
extern "C" {
fn malloc(bytes: usize) -> *mut c_void;
}

malloc(bytes)
};

if ptr.is_null() {
Err(E_OUTOFMEMORY.into())
Expand All @@ -32,5 +42,19 @@ pub fn heap_alloc(bytes: usize) -> crate::Result<*mut c_void> {
///
/// `ptr` must be a valid pointer to memory allocated by `HeapAlloc` or `HeapReAlloc`
pub unsafe fn heap_free(ptr: *mut c_void) {
HeapFree(GetProcessHeap(), 0, ptr);
#[cfg(windows)]
{
HeapFree(GetProcessHeap(), 0, ptr);
}

#[cfg(not(windows))]
{
extern "C" {
fn free(ptr: *mut c_void);
}

if !ptr.is_null() {
free(ptr);
}
}
}

0 comments on commit 25918dc

Please sign in to comment.