-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support for windows #26
Changes from 4 commits
d36c017
28edfa0
6686d07
1cc8068
3c2dde8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,10 @@ | |
#![no_std] | ||
#![deny(missing_docs)] | ||
#![cfg_attr(target_arch = "wasm64", feature(simd_wasm64))] | ||
#[macro_use] | ||
extern crate once_cell; | ||
#[cfg(target_os = "windows")] | ||
extern crate windows; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think neither of these should be necessary with Rust 2018 |
||
|
||
use core::cmp; | ||
use core::ptr; | ||
|
@@ -81,7 +85,11 @@ mod sys; | |
#[path = "unix.rs"] | ||
mod sys; | ||
|
||
#[cfg(not(any(target_os = "linux", target_os = "macos", target_family = "wasm")))] | ||
#[cfg(target_os = "windows")] | ||
#[path = "windows.rs"] | ||
mod sys; | ||
|
||
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows", target_family = "wasm")))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this grows more complicated, can the |
||
#[path = "dummy.rs"] | ||
mod sys; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use core::ptr; | ||
use core::ffi::{c_void}; | ||
use once_cell::sync::Lazy; | ||
use Allocator; | ||
|
||
pub struct System { | ||
_priv: (), | ||
} | ||
|
||
|
||
impl System { | ||
pub const fn new() -> System { System { _priv: () } } | ||
} | ||
|
||
unsafe impl Allocator for System { | ||
fn alloc(&self, size: usize) -> (*mut u8, usize, u32) { | ||
let addr = unsafe { | ||
windows::Win32::System::Memory::VirtualAlloc( | ||
ptr::null_mut(), | ||
size, | ||
windows::Win32::System::Memory::MEM_RESERVE | windows::Win32::System::Memory::MEM_COMMIT, | ||
windows::Win32::System::Memory::PAGE_READWRITE, | ||
) | ||
}; | ||
|
||
if addr == ptr::null_mut() { | ||
(ptr::null_mut(), 0, 0) | ||
} else { | ||
(addr as *mut u8, size, 0) | ||
} | ||
} | ||
|
||
fn remap(&self, ptr: *mut u8, oldsize: usize, newsize: usize, can_move: bool) -> *mut u8 { | ||
ptr::null_mut() | ||
} | ||
|
||
fn free_part(&self, ptr: *mut u8, oldsize: usize, newsize: usize) -> bool { false } | ||
|
||
fn free(&self, ptr: *mut u8, size: usize) -> bool { | ||
unsafe { | ||
windows::Win32::System::Memory::VirtualFree( | ||
ptr as *mut c_void, | ||
0, | ||
windows::Win32::System::Memory::MEM_RELEASE).0 != 0 | ||
} | ||
} | ||
|
||
fn can_release_part(&self, _flags: u32) -> bool { true } | ||
|
||
fn allocates_zeros(&self) -> bool { true } | ||
|
||
fn page_size(&self) -> usize { 4096 } | ||
} | ||
|
||
#[cfg(feature = "global")] | ||
static LOCK: Lazy<windows::Win32::Foundation::HANDLE> = unsafe { | ||
Lazy::new(|| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given how low-level this crate is I think it would be best to not use an external crate to perform this initialization and instead internally use locking/synchronization as necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The thing is that I actually tried to initialize the lock without relying on external crate, but rust compiler didn't allow me to proceed displaying the following error message: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I don't know how best to fix that, but regardless I don't think that using an external crate is appropriate in this case for a lock around the allocator. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the best way to do this is to set the lock to |
||
windows::Win32::System::Threading::CreateMutexA( | ||
ptr::null_mut(), | ||
false, | ||
windows::core::PCSTR::default()) | ||
}) | ||
}; | ||
|
||
#[cfg(feature = "global")] | ||
pub fn acquire_global_lock() { | ||
unsafe { assert_ne!(windows::Win32::System::Threading::WaitForSingleObject(*LOCK, u32::MAX), u32::MAX) }; | ||
} | ||
|
||
#[cfg(feature = "global")] | ||
pub fn release_global_lock() { | ||
unsafe { assert_ne!(windows::Win32::System::Threading::ReleaseMutex(*LOCK).0, 0) }; | ||
} | ||
|
||
/// Under consideration | ||
#[cfg(feature = "global")] | ||
pub unsafe fn enable_alloc_after_fork() { | ||
// TODO(threadedstream): do I need to implement it? | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be added to the matrix above instead of duplicating the CI config?