Skip to content
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

Merged
merged 5 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ jobs:
- run: RUSTFLAGS='--cfg test_lots' cargo test --release
- run: RUSTFLAGS='--cfg test_lots' cargo test --release --features debug

windows-test:
name: Test-windows
runs-on: windows-latest
Copy link
Owner

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?

strategy:
matrix:
rust: [stable, beta, nightly]
steps:
- uses: actions/checkout@master
- name: Install Rust (
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
- run: cargo test
- run: cargo test --features debug
- run: cargo test --features global
- run: cargo test --release
- run: cargo test --features debug --release

rustfmt:
name: Rustfmt
runs-on: ubuntu-latest
Expand Down
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ libc = { version = "0.2", default-features = false }
# `src/tools/rustc-std-workspace` folder
core = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-core' }
compiler_builtins = { version = '0.1.0', optional = true }
once_cell = "1.9.0"
[target.'cfg(target_os = "windows")'.dependencies]
windows = { version = "0.33.0", features = [
"alloc",
"Win32_System_Memory",
"Win32_Foundation",
"Win32_System_Threading",
"Win32_Security",
"Win32_System_Diagnostics",
"Win32_System_Diagnostics_Debug"
] }

[dev-dependencies]
rand = "0.3"
Expand Down
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Owner

Choose a reason for hiding this comment

The 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;
Expand Down Expand Up @@ -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")))]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this grows more complicated, can the cfg_if crate be used to avoid a blowup of conditions here?

#[path = "dummy.rs"]
mod sys;

Expand Down
79 changes: 79 additions & 0 deletions src/windows.rs
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(|| {
Copy link
Owner

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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: calls in statics are limited to constant functions, tuple structs and tuple variants. So, I was advised to lazy-initialize the lock with the help of this crate. I'd be glad if you suggest an alternative

Copy link
Owner

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

The 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 Handle(0) at first. Then, in the lock functions, check if its equal to that and create the mutex if it is.

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?
}