Skip to content

Commit eef6e8c

Browse files
committed
Refactor memory management in Java runtime handling and update build configuration for Windows 7
Assisted-by: codex:gpt-5.4
1 parent 065d4de commit eef6e8c

7 files changed

Lines changed: 52 additions & 360 deletions

File tree

.cargo/config.toml

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,12 @@ include = [{ path = "local.toml", optional = true }]
33
[build]
44
target = "i686-win7-windows-msvc"
55

6-
[target.i686-win7-windows-gnu]
7-
# MinGW uses -municode so the CRT dispatches to wWinMain.
8-
rustflags = ["-C", "link-arg=-municode"]
9-
106
[target.i686-win7-windows-msvc]
117
# Match the legacy CMake /MT build so Windows 7 does not need the VC++ CRT/UCRT
12-
# installed separately, while still using the wide GUI entrypoint.
13-
rustflags = [
14-
"-C",
15-
"link-arg=/ENTRY:wWinMainCRTStartup",
16-
"-C",
17-
"target-feature=+crt-static",
18-
]
8+
# installed separately while letting Rust's normal `std` entrypoint own process
9+
# startup.
10+
rustflags = ["-C", "target-feature=+crt-static"]
1911

2012
[unstable]
21-
build-std = ["core", "compiler_builtins"]
13+
build-std = ["std", "panic_abort"]
14+
panic-abort-tests = true

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ windows-sys = { version = "0.59.0", features = [
1212
"Win32_System_Console",
1313
"Win32_System_Environment",
1414
"Win32_System_LibraryLoader",
15-
"Win32_System_Memory",
1615
"Win32_System_Registry",
1716
"Win32_System_SystemInformation",
1817
"Win32_System_Threading",

src/heap.rs

Lines changed: 0 additions & 193 deletions
This file was deleted.

src/java.rs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use windows_sys::core::{PCWSTR, w};
1717

1818
use crate::HMCL_EXPECTED_JAVA_MAJOR_VERSION;
1919
use crate::debug::{log_fmt, log_verbose_fmt};
20-
use crate::heap::{HeapVec, alloc_bytes, free_bytes};
2120
use crate::platform::is_regular_file;
2221
use crate::wide::{
2322
WideDisplay, WideString, is_dot_or_dot_dot, trim_wide_whitespace, wide_contains,
@@ -55,43 +54,31 @@ impl JavaVersion {
5554
return Self::invalid();
5655
}
5756

58-
let data = unsafe { alloc_bytes(size as usize) };
59-
if data.is_null() {
60-
return Self::invalid();
61-
}
62-
63-
let result = unsafe { GetFileVersionInfoW(path.as_pcwstr(), 0, size, data.cast()) };
57+
let mut data = vec![0u8; size as usize];
58+
let result =
59+
unsafe { GetFileVersionInfoW(path.as_pcwstr(), 0, size, data.as_mut_ptr().cast()) };
6460
if result == 0 {
65-
unsafe {
66-
free_bytes(data);
67-
}
6861
return Self::invalid();
6962
}
7063

7164
// Query the root `VS_FIXEDFILEINFO` block to extract the four numeric
7265
// version components in the same format as the upstream launcher.
7366
let mut info_ptr = ptr::null_mut();
7467
let mut info_len = 0u32;
75-
let result = unsafe { VerQueryValueW(data.cast(), w!("\\"), &mut info_ptr, &mut info_len) };
68+
let result = unsafe {
69+
VerQueryValueW(data.as_ptr().cast(), w!("\\"), &mut info_ptr, &mut info_len)
70+
};
7671
if result == 0 || info_ptr.is_null() || info_len < size_of::<VS_FIXEDFILEINFO>() as u32 {
77-
unsafe {
78-
free_bytes(data);
79-
}
8072
return Self::invalid();
8173
}
8274

8375
let info = unsafe { &*(info_ptr as *const VS_FIXEDFILEINFO) };
84-
let version = Self {
76+
Self {
8577
major: ((info.dwFileVersionMS >> 16) & 0xFFFF) as u16,
8678
minor: (info.dwFileVersionMS & 0xFFFF) as u16,
8779
build: ((info.dwFileVersionLS >> 16) & 0xFFFF) as u16,
8880
revision: (info.dwFileVersionLS & 0xFFFF) as u16,
89-
};
90-
91-
unsafe {
92-
free_bytes(data);
9381
}
94-
version
9582
}
9683

9784
#[cfg(test)]
@@ -159,14 +146,14 @@ pub struct JavaRuntime {
159146

160147
/// Own the set of discovered Java runtimes before launch selection.
161148
pub struct JavaList {
162-
pub runtimes: HeapVec<JavaRuntime>,
149+
pub runtimes: Vec<JavaRuntime>,
163150
}
164151

165152
impl JavaList {
166153
/// Create an empty runtime list.
167154
pub fn new() -> Self {
168155
Self {
169-
runtimes: HeapVec::new(),
156+
runtimes: Vec::new(),
170157
}
171158
}
172159

@@ -208,14 +195,14 @@ impl JavaList {
208195
self.runtimes.push(JavaRuntime {
209196
version,
210197
executable_path: java_executable,
211-
})
198+
});
199+
true
212200
}
213201

214202
/// Sort runtimes from lowest to highest version so callers can try the best
215203
/// match last-to-first.
216204
pub fn sort_by_version(&mut self) {
217-
self.runtimes
218-
.sort_by(|left, right| left.version.cmp(&right.version));
205+
self.runtimes.sort_by(|left, right| left.version.cmp(&right.version));
219206
}
220207
}
221208

src/lib.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
#![no_std]
21
#![allow(non_snake_case)]
32

43
#[cfg(not(target_os = "windows"))]
54
compile_error!("This crate only works on Windows");
65

7-
#[cfg(test)]
8-
extern crate std;
9-
106
mod debug;
11-
mod heap;
127
mod i18n;
138
mod java;
149
mod launcher;
1510
pub mod platform;
1611
mod wide;
1712

18-
use windows_sys::Win32::System::Threading::ExitProcess;
19-
2013
/// Mirror HMCL's current minimum required Java major version.
2114
pub(crate) const HMCL_EXPECTED_JAVA_MAJOR_VERSION: u16 = 17;
2215
/// Expose the build-script-generated launcher version string to the runtime.
@@ -26,9 +19,3 @@ pub(crate) const HMCL_LAUNCHER_VERSION: &str = env!("HMCL_LAUNCHER_VERSION");
2619
pub fn run() -> i32 {
2720
launcher::run()
2821
}
29-
30-
/// Terminate the process without unwinding because the binary uses
31-
/// `panic = "abort"` and no Rust runtime.
32-
pub fn abort(exit_code: u32) -> ! {
33-
unsafe { ExitProcess(exit_code) }
34-
}

0 commit comments

Comments
 (0)