Skip to content

Commit 4b03bc9

Browse files
committed
Fix: A lot of compile warnings on various platforms.
1 parent f04fd9e commit 4b03bc9

File tree

9 files changed

+44
-16
lines changed

9 files changed

+44
-16
lines changed

src-rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ no_format = [] # Removes string formatting (less detailed errors) for binary siz
1919
all_private = [] # No memory mapped files, memory is not shared.
2020
size_opt = ["nightly"]
2121
nightly = [] # Optimizations for nightly builds.
22+
mmap-rs = [] # Uses mmap-rs for memory mapping. This is auto activated during build.
2223

2324
[dependencies]
2425
concat-string = "1.0.1"

src-rust/build.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() {
2+
// This defines a fallback to mmap-rs if one of the explicit memory mapped file implementations
3+
// is not available.
4+
if !cfg!(any(
5+
target_os = "macos",
6+
target_os = "windows",
7+
target_os = "linux"
8+
)) {
9+
println!("cargo:rustc-cfg=feature=\"mmap-rs\"");
10+
}
11+
}

src-rust/src/buffers.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ mod tests {
234234
structs::params::{BufferAllocatorSettings, BufferSearchSettings},
235235
utilities::cached::get_sys_info,
236236
};
237-
use std;
238237

239238
#[cfg(not(target_os = "macos"))]
240239
#[test]
@@ -418,7 +417,7 @@ mod tests {
418417
}
419418

420419
let item = Buffers::get_buffer(&BufferSearchSettings::from_proximity(
421-
std::i32::MAX as usize,
420+
i32::MAX as usize,
422421
base_address,
423422
SIZE,
424423
));

src-rust/src/internal/buffer_allocator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn allocate(
2222
return crate::internal::buffer_allocator_osx::allocate_osx(settings);
2323

2424
// Fallback for non-hot-path OSes.
25-
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
25+
#[cfg(feature = "mmap-rs")]
2626
crate::internal::buffer_allocator_mmap_rs::allocate_mmap_rs(settings)
2727
}
2828

src-rust/src/internal/buffer_allocator_windows.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl Kernel32 for RemoteKernel32 {
6969
lp_buffer: &mut MEMORY_BASIC_INFORMATION,
7070
) -> usize {
7171
unsafe {
72-
VirtualQueryEx(
72+
windows_sys::Win32::System::Memory::VirtualQueryEx(
7373
self.handle,
7474
lp_address,
7575
lp_buffer,
@@ -80,7 +80,7 @@ impl Kernel32 for RemoteKernel32 {
8080

8181
fn virtual_alloc(&self, lp_address: *const c_void, dw_size: usize) -> *mut c_void {
8282
unsafe {
83-
VirtualAllocEx(
83+
windows_sys::Win32::System::Memory::VirtualAllocEx(
8484
self.handle,
8585
lp_address,
8686
dw_size,
@@ -91,7 +91,14 @@ impl Kernel32 for RemoteKernel32 {
9191
}
9292

9393
fn virtual_free(&self, lp_address: *mut c_void, dw_size: usize) -> bool {
94-
unsafe { VirtualFreeEx(self.handle, lp_address, dw_size, MEM_RELEASE) != 0 }
94+
unsafe {
95+
windows_sys::Win32::System::Memory::VirtualFreeEx(
96+
self.handle,
97+
lp_address,
98+
dw_size,
99+
MEM_RELEASE,
100+
) != 0
101+
}
95102
}
96103
}
97104

@@ -111,6 +118,11 @@ impl ProcessHandle {
111118
pub fn is_valid(&self) -> bool {
112119
self.handle != 0
113120
}
121+
122+
#[cfg(feature = "external_processes")]
123+
pub fn get_raw_handle(&self) -> HANDLE {
124+
self.handle
125+
}
114126
}
115127

116128
impl Drop for ProcessHandle {
@@ -172,11 +184,11 @@ pub fn allocate_windows(
172184

173185
#[cfg(feature = "external_processes")]
174186
{
175-
return if get_sys_info().this_process_id == settings.target_process_id {
176-
allocate_fast(&LocalKernel32 {}, max_address, &settings)
187+
if get_sys_info().this_process_id == settings.target_process_id {
188+
allocate_fast(&LocalKernel32 {}, max_address, settings)
177189
} else {
178-
allocate_fast(&RemoteKernel32 { handle }, max_address, &settings)
179-
};
190+
allocate_fast(&RemoteKernel32 { handle }, max_address, settings)
191+
}
180192
}
181193

182194
#[cfg(not(feature = "external_processes"))]

src-rust/src/internal/memory_mapped_file_windows.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ mod tests {
9292
let file_length = get_sys_info().allocation_granularity as usize;
9393
let mmf = WindowsMemoryMappedFile::new(&file_name, file_length);
9494

95-
assert_eq!(mmf.already_existed, false);
95+
assert!(!mmf.already_existed);
9696
assert_eq!(mmf.length, file_length);
9797

9898
// Assert the file can be opened again (i.e., it exists)
9999
let mmf_existing = WindowsMemoryMappedFile::new(&file_name, file_length);
100-
assert_eq!(mmf_existing.already_existed, true);
100+
assert!(mmf_existing.already_existed);
101101
}
102102

103103
#[test]

src-rust/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ pub(crate) mod utilities {
132132
pub mod address_range;
133133
pub mod cached;
134134
pub mod icache_clear;
135+
136+
#[cfg(any(target_os = "linux", feature = "mmap-rs"))]
135137
pub mod map_parser_utilities;
138+
136139
pub mod mathematics;
137140
pub mod wrappers;
138141

src-rust/src/structs/private_allocation.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ impl PrivateAllocation {
9898
unsafe {
9999
#[cfg(feature = "external_processes")]
100100
{
101+
use crate::internal::buffer_allocator_windows::ProcessHandle;
102+
use windows_sys::Win32::System::Memory::VirtualFreeEx;
103+
101104
if self._this_process_id == get_sys_info().this_process_id {
102105
let result =
103106
VirtualFree(self.base_address.as_ptr() as *mut c_void, 0, MEM_RELEASE);
@@ -107,7 +110,7 @@ impl PrivateAllocation {
107110
} else {
108111
let process_handle = ProcessHandle::open_process(self._this_process_id);
109112
let result = VirtualFreeEx(
110-
process_handle.get_handle(),
113+
process_handle.get_raw_handle(),
111114
self.base_address.as_ptr() as *mut c_void,
112115
0,
113116
MEM_RELEASE,
@@ -165,7 +168,7 @@ impl PrivateAllocation {
165168
}
166169

167170
/// Frees the allocated memory when the `PrivateAllocation` instance is dropped.
168-
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
171+
#[cfg(feature = "mmap-rs")]
169172
pub(crate) fn drop_mmap_rs(&mut self) {
170173
use mmap_rs_with_map_from_existing::MmapOptions;
171174
let _map = unsafe {
@@ -193,7 +196,7 @@ impl Drop for PrivateAllocation {
193196
return PrivateAllocation::drop_macos(self);
194197

195198
// non-hot-path-os
196-
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
199+
#[cfg(feature = "mmap-rs")]
197200
return PrivateAllocation::drop_mmap_rs(self);
198201
}
199202
}

src-rust/src/utilities/map_parser_utilities.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ impl MemoryMapEntryTrait for MemoryMapEntry {
4444
/// # Arguments
4545
///
4646
/// * `regions` - A slice of MemoryMapEntry that contains the regions.
47-
4847
#[cfg_attr(feature = "size_opt", optimize(size))]
4948
pub fn get_free_regions<T: MemoryMapEntryTrait>(regions: &[T]) -> Vec<MemoryMapEntry> {
5049
let mut last_end_address: usize = 0;

0 commit comments

Comments
 (0)