Sha256: 646e31462f37f96a9d6928997c2bbacc333eabb3989e9999e89ff95e9565e4ba

Contents?: true

Size: 1.16 KB

Versions: 19

Compression:

Stored size: 1.16 KB

Contents

use super::*;

/// Allocate memory of size `bytes` using `HeapAlloc`.
///
/// The memory allocated by this function is uninitialized.
///
/// 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 std::ffi::c_void> {
    let ptr = unsafe { HeapAlloc(GetProcessHeap(), 0, bytes) };

    if ptr.is_null() {
        Err(E_OUTOFMEMORY.into())
    } else {
        // HeapAlloc is not guaranteed to return zero memory but usually does. This just ensures that
        // it predictably returns non-zero memory for testing purposes. This is similar to what MSVC's
        // debug allocator does for the same reason.
        #[cfg(debug_assertions)]
        unsafe {
            std::ptr::write_bytes(ptr, 0xCC, bytes);
        }
        Ok(ptr)
    }
}

/// Free memory allocated by `HeapAlloc` or `HeapReAlloc`.
///
/// The pointer is allowed to be null.
///
/// # Safety
///
/// `ptr` must be a valid pointer to memory allocated by `HeapAlloc` or `HeapReAlloc`
pub unsafe fn heap_free(ptr: *mut std::ffi::c_void) {
    HeapFree(GetProcessHeap(), 0, ptr);
}

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-28.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-27.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-26.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-25.0.2 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-25.0.1 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-25.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-24.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-23.0.2 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-22.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-21.0.1 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-20.0.2 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-20.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-18.0.3 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-17.0.1 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-17.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-16.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-15.0.1 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs
wasmtime-15.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/heap.rs