Skip to content

Commit

Permalink
Use malloc on non-Windows platforms (#3095)
Browse files Browse the repository at this point in the history
  • Loading branch information
sivadeilra authored Jun 13, 2024
1 parent 8e0e96d commit 66d8769
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 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,17 @@ 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);
}

free(ptr);
}
}

0 comments on commit 66d8769

Please sign in to comment.