Skip to content

Commit 70eae8d

Browse files
committed
Rust: Improved code size a tiny bit further
1 parent 132aa41 commit 70eae8d

6 files changed

Lines changed: 24 additions & 16 deletions

File tree

src-rust/src/buffers.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::utilities::disable_write_xor_execute::{
99
};
1010
use crate::utilities::icache_clear::clear_instruction_cache;
1111
use crate::utilities::mathematics::round_up;
12-
use core::cmp::max;
1312
use core::ptr::{copy_nonoverlapping, NonNull};
1413
use core::u8;
1514

src-rust/src/internal/locator_header_finder.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,19 @@ impl LocatorHeaderFinder {
5252
fn open_or_create_memory_mapped_file() -> Box<dyn MemoryMappedFile> {
5353
// no_std
5454
let mut name = String::from("/Reloaded.Memory.Buffers.MemoryBuffer, PID ");
55-
name.push_str(&get_sys_info().this_process_id.to_string());
55+
let sys_info = get_sys_info();
56+
name.push_str(&sys_info.this_process_id.to_string());
5657

5758
#[cfg(target_os = "windows")]
5859
return Box::new(WindowsMemoryMappedFile::new(
5960
&name,
60-
get_sys_info().allocation_granularity as usize,
61+
sys_info.allocation_granularity as usize,
6162
));
6263

6364
#[cfg(unix)]
6465
return Box::new(UnixMemoryMappedFile::new(
6566
&name,
66-
get_sys_info().allocation_granularity as usize,
67+
sys_info.allocation_granularity as usize,
6768
));
6869
}
6970

src-rust/src/structs/internal/locator_header.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,11 @@ impl LocatorHeader {
338338
}
339339

340340
// Allocate the next locator.
341-
let alloc_size = get_sys_info().allocation_granularity;
341+
let sys_info = get_sys_info();
342+
let alloc_size = sys_info.allocation_granularity;
342343
unsafe {
343344
let addr = alloc::alloc::alloc(
344-
Layout::from_size_align(alloc_size as usize, get_sys_info().page_size as usize)
345-
.unwrap(),
345+
Layout::from_size_align(alloc_size as usize, sys_info.page_size as usize).unwrap(),
346346
);
347347
if addr.is_null() {
348348
self.unlock();

src-rust/src/structs/params/buffer_allocator_settings.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ pub struct BufferAllocatorSettings {
4040
impl BufferAllocatorSettings {
4141
/// Initializes the buffer allocator with default settings.
4242
pub fn new() -> Self {
43+
let sys_info = get_sys_info();
4344
Self {
4445
min_address: 0,
45-
max_address: get_sys_info().max_address,
46+
max_address: sys_info.max_address,
4647
size: 4096,
47-
target_process_id: get_sys_info().this_process_id,
48+
target_process_id: sys_info.this_process_id,
4849
retry_count: 8,
4950
brute_force: true,
5051
}
@@ -73,15 +74,15 @@ impl BufferAllocatorSettings {
7374
/// Sanitizes the input values.
7475
pub fn sanitize(&mut self) {
7576
// On Windows, VirtualAlloc treats 0 as 'any address', we might aswell avoid this out the gate.
76-
if cfg!(windows) && (self.min_address < get_sys_info().allocation_granularity as usize) {
77-
self.min_address = get_sys_info().allocation_granularity as usize;
77+
let sys_info = get_sys_info();
78+
if cfg!(windows) && (self.min_address < sys_info.allocation_granularity as usize) {
79+
self.min_address = sys_info.allocation_granularity as usize;
7880
}
7981

8082
self.size = max(self.size, 1);
81-
self.size = mathematics::round_up(
82-
self.size as usize,
83-
get_sys_info().allocation_granularity as usize,
84-
) as u32;
83+
self.size =
84+
mathematics::round_up(self.size as usize, sys_info.allocation_granularity as usize)
85+
as u32;
8586
}
8687
}
8788

src-rust/src/utilities/cached.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ pub fn get_sys_info() -> &'static Cached {
1313
return CACHED.as_ref().unwrap_unchecked();
1414
}
1515

16-
CACHED = Some(Cached::new());
16+
make_sys_info();
1717
return CACHED.as_ref().unwrap_unchecked();
1818
}
1919
}
2020

21+
#[inline(never)] // bloats the binary
22+
fn make_sys_info() {
23+
unsafe { CACHED = Some(Cached::new()) };
24+
}
25+
2126
pub struct Cached {
2227
pub max_address: usize,
2328
pub allocation_granularity: i32,

src-rust/src/utilities/map_parser_utilities.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ impl MemoryMapEntryTrait for MemoryMapEntry {
4242
/// # Arguments
4343
///
4444
/// * `regions` - A slice of MemoryMapEntry that contains the regions.
45+
46+
#[cfg_attr(feature = "size_opt", optimize(size))]
4547
pub fn get_free_regions<T: MemoryMapEntryTrait>(regions: &[T]) -> Vec<MemoryMapEntry> {
4648
let mut last_end_address: usize = 0;
4749
let mut free_regions = Vec::with_capacity(regions.len() + 2); // +2 for start and finish

0 commit comments

Comments
 (0)