Skip to content

Commit 7d50919

Browse files
authored
Merge pull request #35 from alexcrichton/windows
Windows support: Rebase #26
2 parents 9a0e6a1 + 27e735c commit 7d50919

File tree

5 files changed

+131
-24
lines changed

5 files changed

+131
-24
lines changed

.github/workflows/main.yml

+15-2
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,34 @@ on: [push, pull_request]
44
jobs:
55
test:
66
name: Test
7-
runs-on: ubuntu-latest
7+
runs-on: ${{ matrix.os }}
88
strategy:
99
matrix:
10-
rust: [stable, beta, nightly]
10+
include:
11+
- os: ubuntu-latest
12+
rust: stable
13+
- os: ubuntu-latest
14+
rust: beta
15+
- os: ubuntu-latest
16+
rust: nightly
17+
- os: macos-latest
18+
rust: stable
19+
- os: windows-latest
20+
rust: stable
1121
steps:
1222
- uses: actions/checkout@v4
1323
- name: Install Rust (
1424
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
25+
shell: bash
1526
- run: cargo test
1627
- run: cargo test --features debug
1728
- run: cargo test --features global
1829
- run: cargo test --release
1930
- run: cargo test --features debug --release
2031
- run: RUSTFLAGS='--cfg test_lots' cargo test --release
32+
shell: bash
2133
- run: RUSTFLAGS='--cfg test_lots' cargo test --release --features debug
34+
shell: bash
2235

2336
rustfmt:
2437
name: Rustfmt

Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ libc = { version = "0.2", default-features = false }
3232
# `src/tools/rustc-std-workspace` folder
3333
core = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-core' }
3434
compiler_builtins = { version = '0.1.0', optional = true }
35+
cfg-if = "1.0"
36+
37+
[target.'cfg(target_os = "windows")'.dependencies.windows-sys]
38+
version = "0.52.0"
39+
features = [
40+
"Win32_Foundation",
41+
"Win32_System_Memory",
42+
"Win32_System_Threading",
43+
"Win32_System_SystemInformation",
44+
]
3545

3646
[dev-dependencies]
3747
arbitrary = "1.3.2"

src/lib.rs

+18-20
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,24 @@ pub unsafe trait Allocator: Send {
7373
/// lingering memory back to the OS. That may happen eventually though!
7474
pub struct Dlmalloc<A = System>(dlmalloc::Dlmalloc<A>);
7575

76-
#[cfg(target_family = "wasm")]
77-
#[path = "wasm.rs"]
78-
mod sys;
79-
80-
#[cfg(any(target_os = "linux", target_os = "macos"))]
81-
#[path = "unix.rs"]
82-
mod sys;
83-
84-
#[cfg(target_os = "xous")]
85-
#[path = "xous.rs"]
86-
mod sys;
87-
88-
#[cfg(not(any(
89-
target_os = "linux",
90-
target_os = "macos",
91-
target_os = "xous",
92-
target_family = "wasm"
93-
)))]
94-
#[path = "dummy.rs"]
95-
mod sys;
76+
cfg_if::cfg_if! {
77+
if #[cfg(target_family = "wasm")] {
78+
#[path = "wasm.rs"]
79+
mod sys;
80+
} else if #[cfg(target_os = "windows")] {
81+
#[path = "windows.rs"]
82+
mod sys;
83+
} else if #[cfg(target_os = "xous")] {
84+
#[path = "xous.rs"]
85+
mod sys;
86+
} else if #[cfg(any(target_os = "linux", target_os = "macos"))] {
87+
#[path = "unix.rs"]
88+
mod sys;
89+
} else {
90+
#[path = "dummy.rs"]
91+
mod sys;
92+
}
93+
}
9694

9795
impl Dlmalloc<System> {
9896
/// Creates a new instance of an allocator

src/unix.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
extern crate libc;
2-
31
use crate::Allocator;
42
use core::ptr;
53

src/windows.rs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use crate::Allocator;
2+
use core::mem::MaybeUninit;
3+
use core::ptr;
4+
use windows_sys::Win32::System::Memory::*;
5+
use windows_sys::Win32::System::SystemInformation::*;
6+
#[cfg(feature = "global")]
7+
use windows_sys::Win32::System::Threading::*;
8+
9+
pub struct System {
10+
_priv: (),
11+
}
12+
13+
impl System {
14+
pub const fn new() -> System {
15+
System { _priv: () }
16+
}
17+
}
18+
19+
unsafe impl Allocator for System {
20+
fn alloc(&self, size: usize) -> (*mut u8, usize, u32) {
21+
let addr = unsafe {
22+
VirtualAlloc(
23+
ptr::null_mut(),
24+
size,
25+
MEM_RESERVE | MEM_COMMIT,
26+
PAGE_READWRITE,
27+
)
28+
};
29+
30+
if addr.is_null() {
31+
(ptr::null_mut(), 0, 0)
32+
} else {
33+
(addr.cast(), size, 0)
34+
}
35+
}
36+
37+
fn remap(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool) -> *mut u8 {
38+
ptr::null_mut()
39+
}
40+
41+
fn free_part(&self, ptr: *mut u8, oldsize: usize, newsize: usize) -> bool {
42+
unsafe { VirtualFree(ptr.add(newsize).cast(), oldsize - newsize, MEM_DECOMMIT) != 0 }
43+
}
44+
45+
fn free(&self, ptr: *mut u8, _size: usize) -> bool {
46+
unsafe { VirtualFree(ptr.cast(), 0, MEM_DECOMMIT) != 0 }
47+
}
48+
49+
fn can_release_part(&self, _flags: u32) -> bool {
50+
true
51+
}
52+
53+
fn allocates_zeros(&self) -> bool {
54+
true
55+
}
56+
57+
fn page_size(&self) -> usize {
58+
unsafe {
59+
let mut info = MaybeUninit::uninit();
60+
GetSystemInfo(info.as_mut_ptr());
61+
info.assume_init_ref().dwPageSize as usize
62+
}
63+
}
64+
}
65+
66+
// NB: `SRWLOCK_INIT` doesn't appear to be in `windows-sys`
67+
#[cfg(feature = "global")]
68+
static mut LOCK: SRWLOCK = SRWLOCK {
69+
Ptr: ptr::null_mut(),
70+
};
71+
72+
#[cfg(feature = "global")]
73+
pub fn acquire_global_lock() {
74+
unsafe {
75+
AcquireSRWLockExclusive(ptr::addr_of_mut!(LOCK));
76+
}
77+
}
78+
79+
#[cfg(feature = "global")]
80+
pub fn release_global_lock() {
81+
unsafe {
82+
ReleaseSRWLockExclusive(ptr::addr_of_mut!(LOCK));
83+
}
84+
}
85+
86+
/// Not needed on Windows
87+
#[cfg(feature = "global")]
88+
pub unsafe fn enable_alloc_after_fork() {}

0 commit comments

Comments
 (0)