Skip to content

Commit 45c8d2f

Browse files
wan9chicodex
andcommitted
refactor(fspy): own Windows shared-memory mapping
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Co-authored-by: GPT-5 Codex <codex@openai.com>
1 parent e4e82df commit 45c8d2f

7 files changed

Lines changed: 640 additions & 11 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ wax = "0.7.0"
168168
which = "8.0.0"
169169
widestring = "1.2.0"
170170
winapi = "0.3.9"
171+
windows-sys = "0.61"
171172
winsafe = { version = "0.0.27", features = ["kernel"] }
172173
xxhash-rust = { version = "0.8.15", features = ["const_xxh3"] }
173174
ntest = "0.9.5"

crates/fspy_preload_windows/src/windows/winapi_utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,13 @@ pub const fn access_mask_to_mode(desired_access: ACCESS_MASK) -> AccessMode {
9999
}
100100
}
101101

102+
#[link(name = "kernel32")]
102103
unsafe extern "system" {
103104
fn LocalFree(hmem: HLOCAL) -> HLOCAL;
105+
}
106+
107+
#[link(name = "pathcch")]
108+
unsafe extern "system" {
104109
fn PathAllocCombine(
105110
pszpathin: PCWSTR,
106111
pszmore: PCWSTR,

crates/fspy_shm/Cargo.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ license.workspace = true
77
publish = false
88
rust-version.workspace = true
99

10-
[target.'cfg(not(target_os = "linux"))'.dependencies]
10+
[target.'cfg(not(any(target_os = "linux", target_os = "windows")))'.dependencies]
1111
shared_memory = { workspace = true, features = ["logging"] }
1212

1313
[target.'cfg(target_os = "linux")'.dependencies]
@@ -19,6 +19,16 @@ tokio-util = { workspace = true }
1919
tracing = { workspace = true }
2020
uuid = { workspace = true, features = ["v4"] }
2121

22+
[target.'cfg(target_os = "windows")'.dependencies]
23+
base64 = { workspace = true }
24+
uuid = { workspace = true, features = ["v4"] }
25+
windows-sys = { workspace = true, features = [
26+
"Win32_Foundation",
27+
"Win32_Security",
28+
"Win32_Storage_FileSystem",
29+
"Win32_System_Memory",
30+
] }
31+
2232
[dev-dependencies]
2333
ctor = { workspace = true }
2434
subprocess_test = { workspace = true }

crates/fspy_shm/src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,23 @@
2323
//! On Linux the mapping is an anonymous memfd handed out by a broker task, so
2424
//! [`create`] must be called within a tokio runtime there.
2525
26-
#[cfg(not(target_os = "linux"))]
26+
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
2727
use std::io;
2828

2929
#[cfg(target_os = "linux")]
3030
mod linux;
31+
#[cfg(target_os = "windows")]
32+
mod windows;
3133

3234
#[cfg(target_os = "linux")]
3335
pub use linux::{Shm, create, open};
34-
#[cfg(not(target_os = "linux"))]
36+
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
3537
use shared_memory::{Shmem, ShmemConf};
38+
#[cfg(target_os = "windows")]
39+
pub use windows::{Shm, create, open};
3640

3741
/// An owned shared-memory mapping.
38-
#[cfg(not(target_os = "linux"))]
42+
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
3943
pub struct Shm {
4044
inner: Shmem,
4145
}
@@ -49,11 +53,9 @@ pub struct Shm {
4953
/// # Errors
5054
///
5155
/// Returns an error if the platform cannot create or map the region.
52-
#[cfg(not(target_os = "linux"))]
56+
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
5357
pub fn create(size: usize) -> io::Result<Shm> {
5458
let conf = ShmemConf::new().size(size);
55-
#[cfg(target_os = "windows")]
56-
let conf = conf.allow_raw(true);
5759

5860
let inner = conf.create().map_err(io::Error::other)?;
5961
Ok(Shm { inner })
@@ -68,17 +70,15 @@ pub fn create(size: usize) -> io::Result<Shm> {
6870
/// # Errors
6971
///
7072
/// Returns an error if the mapping does not exist or cannot be mapped.
71-
#[cfg(not(target_os = "linux"))]
73+
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
7274
pub fn open(id: &str, size: usize) -> io::Result<Shm> {
7375
let conf = ShmemConf::new().size(size).os_id(id);
74-
#[cfg(target_os = "windows")]
75-
let conf = conf.allow_raw(true);
7676

7777
let inner = conf.open().map_err(io::Error::other)?;
7878
Ok(Shm { inner })
7979
}
8080

81-
#[cfg(not(target_os = "linux"))]
81+
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
8282
#[expect(clippy::len_without_is_empty, reason = "shared-memory mappings are always non-empty")]
8383
impl Shm {
8484
/// Returns this mapping's opaque platform identifier.

0 commit comments

Comments
 (0)