Skip to content

Commit f4a946c

Browse files
committed
Making unlikely and likely for stable compile
1 parent 43cb948 commit f4a946c

File tree

6 files changed

+51
-19
lines changed

6 files changed

+51
-19
lines changed

crates/libmwemu/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(likely_unlikely)]
21
#![allow(non_snake_case)]
32
#![allow(dead_code)]
43
#![allow(unused_variables)]
@@ -45,6 +44,7 @@ pub mod winapi;
4544

4645
#[cfg(test)]
4746
mod tests;
47+
mod utils;
4848

4949
use config::Config;
5050
use emu::Emu;

crates/libmwemu/src/maps/heap_allocation.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use std::cell::RefCell;
22
use std::collections::HashMap;
33
use std::ffi::c_char;
44
use std::rc::{Rc, Weak};
5-
use std::{cmp, hint};
5+
use std::{cmp};
6+
use crate::utils::{likely, unlikely};
67

78
type PRFrag = Option<Rc<RefCell<Fragment>>>;
89
type PWFrag = Option<Weak<RefCell<Fragment>>>;
@@ -163,12 +164,12 @@ impl O1Heap {
163164
/// * `Some(usize)` - An offset into the heap's memory arena if successful
164165
/// * `None` - If there is insufficient memory or the request is invalid
165166
pub fn allocate(&mut self, amount: usize) -> Option<u64> {
166-
if hint::unlikely(amount == 0) {
167+
if unlikely(amount == 0) {
167168
return None;
168169
}
169170

170171
// Update peak request size
171-
if hint::likely(self.diagnostics.peak_request_size < amount) {
172+
if likely(self.diagnostics.peak_request_size < amount) {
172173
self.diagnostics.peak_request_size = amount;
173174
}
174175

@@ -185,7 +186,7 @@ impl O1Heap {
185186
let suitable_bins = self.nonempty_bin_mask & candidate_bin_mask;
186187

187188
// Find smallest suitable bin
188-
if hint::likely(suitable_bins != 0) {
189+
if likely(suitable_bins != 0) {
189190
let smallest_bin_index = suitable_bins.trailing_zeros() as usize;
190191

191192
if smallest_bin_index < NUM_BINS_MAX {
@@ -198,7 +199,7 @@ impl O1Heap {
198199
frag_rc.borrow_mut().size = fragment_size as u32;
199200
// Split if necessary
200201
let leftover = frag_size - fragment_size as u32;
201-
if hint::likely(leftover >= FRAGMENT_SIZE_MIN as u32) {
202+
if likely(leftover >= FRAGMENT_SIZE_MIN as u32) {
202203
let new_frag = Rc::new(RefCell::new(Fragment::new(frag_offset + fragment_size as u32, leftover)));
203204
// Link the new fragment in the chain
204205
let next_rc = frag_rc.borrow().next.clone();
@@ -236,12 +237,12 @@ impl O1Heap {
236237
// remove fragment from the bin
237238
fn unbin(&mut self, fragment: &Rc<RefCell<Fragment>>) {
238239
let size = fragment.borrow().size;
239-
if hint::unlikely(size < FRAGMENT_SIZE_MIN as u32) {
240+
if unlikely(size < FRAGMENT_SIZE_MIN as u32) {
240241
return;
241242
}
242243

243244
let idx = self.log2_floor(size as usize / FRAGMENT_SIZE_MIN);
244-
if hint::unlikely(idx >= NUM_BINS_MAX) {
245+
if unlikely(idx >= NUM_BINS_MAX) {
245246
return;
246247
}
247248

crates/libmwemu/src/utils.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// TODO: remove the code when 'likely' and 'unlikely' are stable
2+
#[inline(always)]
3+
#[cold]
4+
fn cold_path() {}
5+
6+
#[inline(always)]
7+
pub(crate) fn likely(b: bool) -> bool {
8+
if b {
9+
true
10+
} else {
11+
cold_path();
12+
false
13+
}
14+
}
15+
16+
#[inline(always)]
17+
pub(crate) fn unlikely(b: bool) -> bool {
18+
if b {
19+
cold_path();
20+
true
21+
} else {
22+
false
23+
}
24+
}

crates/libmwemu/src/winapi/winapi32/kernel32/heap_alloc.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,23 @@ pub fn HeapAlloc(emu: &mut emu::Emu) {
1515
.read_dword(emu.regs().get_esp() + 8)
1616
.expect("kernel32!HeapAlloc cannot read the size") as u64;
1717

18-
emu.regs_mut().rax = emu.maps.alloc(size).unwrap_or_default();
18+
let heap_addr: u64 = if size < 0x8000 {
19+
let heap_manage = emu.heap_management.as_mut().unwrap();
20+
heap_manage.allocate(size as usize).expect("failed to allocate heap")
21+
} else {
22+
let allocation = emu.maps.alloc(size).unwrap_or_default();
23+
emu.maps
24+
.create_map(
25+
format!("alloc_{:x}", allocation).as_str(),
26+
allocation,
27+
size,
28+
Permission::READ_WRITE,
29+
)
30+
.expect("kernel32!HeapAlloc out of memory");
31+
allocation
32+
};
1933

20-
emu.maps
21-
.create_map(
22-
format!("alloc_{:x}", emu.regs().get_eax() as u32).as_str(),
23-
emu.regs().get_eax(),
24-
size,
25-
Permission::READ_WRITE,
26-
)
27-
.expect("kernel32!HeapAlloc out of memory");
34+
emu.regs_mut().rax = heap_addr;
2835

2936
log_red!(
3037
emu,

crates/libmwemu/src/winapi/winapi64/kernel32/heap_alloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn HeapAlloc(emu: &mut emu::Emu) {
2222
allocation
2323
};
2424

25-
emu.regs_mut().rax = heap_addr;
25+
emu.regs_mut().rax = heap_addr;
2626

2727
log_red!(
2828
emu,

crates/libmwemu/src/winapi/winapi64/kernel32/heap_free.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::hint::likely;
1+
use crate::utils::likely;
22
use crate::emu;
33

44
pub fn HeapFree(emu: &mut emu::Emu) {

0 commit comments

Comments
 (0)