use super::*;
use bindings::*;
pub fn heap_alloc(bytes: usize) -> Result<*mut core::ffi::c_void> {
let ptr = unsafe { HeapAlloc(GetProcessHeap()?, HEAP_NONE, bytes) };
if ptr.is_null() {
Err(E_OUTOFMEMORY.into())
} else {
#[cfg(debug_assertions)]
unsafe {
std::ptr::write_bytes(ptr, 0xCC, bytes);
}
Ok(ptr)
}
}
pub unsafe fn heap_free(ptr: *mut core::ffi::c_void) {
if let Ok(heap) = GetProcessHeap() {
HeapFree(heap, HEAP_NONE, ptr);
}
}
pub fn alloc_from_iter<I, T>(iter: I, len: usize) -> *const T
where
I: Iterator<Item = T>,
T: Copy,
{
assert!(std::mem::align_of::<T>() <= 8, "T alignment surpasses HeapAlloc alignment");
let ptr = heap_alloc(len * std::mem::size_of::<T>()).expect("could not allocate string") as *mut T;
for (offset, c) in iter.take(len).enumerate() {
unsafe {
ptr.add(offset).write(c);
}
}
ptr
}