Skip to content

Commit 1161cb4

Browse files
committed
rename from platform_* to *_impl
Signed-off-by: Christian Jordan <[email protected]>
1 parent 9eeacaf commit 1161cb4

File tree

7 files changed

+114
-5
lines changed

7 files changed

+114
-5
lines changed

src/platform/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#[cfg(any(target_os = "linux", target_os = "macos"))]
2-
mod platform_nix {
2+
mod nix_impl {
33
pub mod addr_validate;
44
pub mod profiler;
55
pub mod timer;
66
}
77

88
#[cfg(target_os = "windows")]
9-
mod platform_windows {
9+
mod windows_impl {
1010
pub mod addr_validate;
1111
pub mod profiler;
1212
pub mod timer;
1313
}
1414

1515
#[cfg(any(target_os = "linux", target_os = "macos"))]
16-
pub use platform_nix::*;
16+
pub use nix_impl::*;
1717

1818
#[cfg(target_os = "windows")]
19-
pub use platform_windows::*;
19+
pub use windows_impl::*;

src/platform/platform_nix/profiler.rs renamed to src/platform/nix_impl/profiler.rs

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use smallvec::SmallVec;
66

77
use nix::sys::signal;
88

9-
use crate::error::{Error, Result};
9+
use crate::error::Result;
1010
use crate::profiler::PROFILER;
1111
use crate::{MAX_DEPTH, MAX_THREAD_NAME};
1212

@@ -183,3 +183,112 @@ extern "C" fn perf_signal_handler(
183183
}
184184
}
185185
}
186+
187+
#[cfg(test)]
188+
mod tests {
189+
use super::*;
190+
191+
use std::cell::RefCell;
192+
use std::ffi::c_void;
193+
use std::ptr::null_mut;
194+
195+
#[cfg(not(target_env = "gnu"))]
196+
#[allow(clippy::wrong_self_convention)]
197+
#[allow(non_upper_case_globals)]
198+
static mut __malloc_hook: Option<extern "C" fn(size: usize) -> *mut c_void> = None;
199+
200+
extern "C" {
201+
#[cfg(target_env = "gnu")]
202+
static mut __malloc_hook: Option<extern "C" fn(size: usize) -> *mut c_void>;
203+
204+
fn malloc(size: usize) -> *mut c_void;
205+
}
206+
207+
thread_local! {
208+
static FLAG: RefCell<bool> = RefCell::new(false);
209+
}
210+
211+
extern "C" fn malloc_hook(size: usize) -> *mut c_void {
212+
unsafe {
213+
__malloc_hook = None;
214+
}
215+
216+
FLAG.with(|flag| {
217+
flag.replace(true);
218+
});
219+
let p = unsafe { malloc(size) };
220+
221+
unsafe {
222+
__malloc_hook = Some(malloc_hook);
223+
}
224+
225+
p
226+
}
227+
228+
#[inline(never)]
229+
fn is_prime_number(v: usize, prime_numbers: &[usize]) -> bool {
230+
if v < 10000 {
231+
let r = prime_numbers.binary_search(&v);
232+
return r.is_ok();
233+
}
234+
235+
for n in prime_numbers {
236+
if v % n == 0 {
237+
return false;
238+
}
239+
}
240+
241+
true
242+
}
243+
244+
#[inline(never)]
245+
fn prepare_prime_numbers() -> Vec<usize> {
246+
// bootstrap: Generate a prime table of 0..10000
247+
let mut prime_number_table: [bool; 10000] = [true; 10000];
248+
prime_number_table[0] = false;
249+
prime_number_table[1] = false;
250+
for i in 2..10000 {
251+
if prime_number_table[i] {
252+
let mut v = i * 2;
253+
while v < 10000 {
254+
prime_number_table[v] = false;
255+
v += i;
256+
}
257+
}
258+
}
259+
let mut prime_numbers = vec![];
260+
for (i, item) in prime_number_table.iter().enumerate().skip(2) {
261+
if *item {
262+
prime_numbers.push(i);
263+
}
264+
}
265+
prime_numbers
266+
}
267+
268+
#[cfg(target_os = "linux")]
269+
#[test]
270+
fn malloc_free() {
271+
trigger_lazy();
272+
273+
let prime_numbers = prepare_prime_numbers();
274+
275+
let mut _v = 0;
276+
277+
unsafe {
278+
__malloc_hook = Some(malloc_hook);
279+
}
280+
for i in 2..50000 {
281+
if is_prime_number(i, &prime_numbers) {
282+
_v += 1;
283+
perf_signal_handler(27, null_mut(), null_mut());
284+
}
285+
}
286+
unsafe {
287+
__malloc_hook = None;
288+
}
289+
290+
FLAG.with(|flag| {
291+
assert!(!*flag.borrow());
292+
});
293+
}
294+
}
File renamed without changes.

0 commit comments

Comments
 (0)