Skip to content

Commit

Permalink
implement with_size_protect (#163)
Browse files Browse the repository at this point in the history
* implement with_size_protect

* no more conditional compilation
  • Loading branch information
xmclark authored and lachlansneff committed Feb 8, 2019
1 parent 46ac4e9 commit 351b4fe
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/runtime-core/src/sys/windows/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,32 @@ pub struct Memory {
}

impl Memory {
pub fn with_size_protect(size: usize, protection: Protect) -> Result<Self, String> {
if size == 0 {
return Ok(Self {
ptr: ptr::null_mut(),
size: 0,
protection,
});
}

let size = round_up_to_page_size(size, page_size::get());

let protect = protection.to_protect_const();

let ptr = unsafe { VirtualAlloc(ptr::null_mut(), size, MEM_RESERVE, protect) };

if ptr.is_null() {
Err("unable to allocate memory".to_string())
} else {
Ok(Self {
ptr: ptr as *mut u8,
size,
protection,
})
}
}

pub fn with_size(size: usize) -> Result<Self, String> {
if size == 0 {
return Ok(Self {
Expand Down

0 comments on commit 351b4fe

Please sign in to comment.