Skip to content

Commit

Permalink
Document heap functions (#1670)
Browse files Browse the repository at this point in the history
  • Loading branch information
rylev authored Apr 8, 2022
1 parent f570d10 commit 03f1f40
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion crates/libs/windows/src/core/heap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use super::*;
use bindings::*;

// TODO: why not Option<RawPtr>
/// 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.
// TODO: why not return a `Option<RawPtr>`
pub fn heap_alloc(bytes: usize) -> Result<RawPtr> {
let ptr = unsafe { HeapAlloc(GetProcessHeap()?, HEAP_NONE, bytes) };

Expand All @@ -12,7 +18,14 @@ pub fn heap_alloc(bytes: usize) -> Result<RawPtr> {
}
}

/// Free memory allocated by `HeapAlloc` or `HeapReAlloc`.
///
/// The pointer is allowed to be null. If there is an error getting the process heap,
/// the memory will be leaked.
///
/// # Safety
///
/// `ptr` must be a valid pointer to memory allocated by `HeapAlloc` or `HeapReAlloc`
pub unsafe fn heap_free(ptr: RawPtr) {
if let Ok(heap) = GetProcessHeap() {
HeapFree(heap, HEAP_NONE, ptr);
Expand Down

0 comments on commit 03f1f40

Please sign in to comment.