1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#![no_std]
#![cfg_attr(not(feature = "no-std"), doc = " ``` ")]
#![cfg_attr(feature = "no-std", doc = " ```no_run ")]
#![cfg_attr(feature = "no-std", feature(const_fn))]
#[cfg(feature = "no-std")]
extern crate spin;
#[cfg(feature = "no-std")]
use spin::Once;
#[cfg(not(feature = "no-std"))]
extern crate std;
#[cfg(not(feature = "no-std"))]
use std::sync::{Once, ONCE_INIT};
#[cfg_attr(not(feature = "no-std"), doc = " ``` ")]
#[cfg_attr(feature = "no-std", doc = " ```no_run ")]
pub fn get() -> usize {
get_helper()
}
#[cfg(all(unix, feature = "no-std"))]
#[inline]
fn get_helper() -> usize {
static INIT: Once<usize> = Once::new();
*INIT.call_once(unix::get_unix)
}
#[cfg(all(unix, not(feature = "no-std")))]
#[inline]
fn get_helper() -> usize {
static INIT: Once = ONCE_INIT;
static mut PAGE_SIZE: usize = 0;
unsafe {
INIT.call_once(|| PAGE_SIZE = unix::get_unix());
PAGE_SIZE
}
}
#[cfg(unix)]
extern crate libc;
#[cfg(unix)]
mod unix {
use super::*;
#[inline]
pub fn get_unix() -> usize {
unsafe {
libc::sysconf(libc::_SC_PAGESIZE) as usize
}
}
}
#[cfg(windows)]
extern crate winapi;
#[cfg(all(windows, feature = "no-std"))]
#[inline]
fn get_helper() -> usize {
static INIT: Once<usize> = Once::new();
*INIT.call_once(windows::get_windows)
}
#[cfg(all(windows, not(feature = "no-std")))]
#[inline]
fn get_helper() -> usize {
static INIT: Once = ONCE_INIT;
static mut PAGE_SIZE: usize = 0;
unsafe {
INIT.call_once(|| PAGE_SIZE = windows::get_windows());
PAGE_SIZE
}
}
#[cfg(windows)]
mod windows {
use super::*;
#[inline]
pub fn get_windows() -> usize {
unsafe {
let mut info: winapi::SYSTEM_INFO = mem::zeroed();
winapi::kernel32::GetSystemInfo(&mut info);
info.dwPageSize as usize
}
}
}
#[cfg(not(any(unix, windows)))]
#[inline]
fn get_helper() -> usize {
4096
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get() {
#[allow(unused_variables)]
let page_size = get();
}
}