From 351b4feeeb4842172da5f3913b309f9b253571c5 Mon Sep 17 00:00:00 2001 From: Mackenzie Clark Date: Fri, 8 Feb 2019 10:32:20 -0800 Subject: [PATCH] implement with_size_protect (#163) * implement with_size_protect * no more conditional compilation --- lib/runtime-core/src/sys/windows/memory.rs | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/runtime-core/src/sys/windows/memory.rs b/lib/runtime-core/src/sys/windows/memory.rs index 1274cb6ac..baf4d27a5 100644 --- a/lib/runtime-core/src/sys/windows/memory.rs +++ b/lib/runtime-core/src/sys/windows/memory.rs @@ -18,6 +18,32 @@ pub struct Memory { } impl Memory { + pub fn with_size_protect(size: usize, protection: Protect) -> Result { + 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 { if size == 0 { return Ok(Self {