#![no_std]
#[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;
#[cfg(unix)]
extern crate libc;
#[cfg(windows)]
extern crate winapi;
pub fn get() -> usize {
get_helper()
}
pub fn get_granularity() -> usize {
get_granularity_helper()
}
#[cfg(all(unix, feature = "no_std"))]
#[inline]
fn get_helper() -> usize {
static INIT: Once<usize> = Once::new();
*INIT.call_once(unix::get)
}
#[cfg(all(unix, not(feature = "no_std")))]
#[inline]
fn get_helper() -> usize {
static INIT: Once = Once::new();
static mut PAGE_SIZE: usize = 0;
unsafe {
INIT.call_once(|| PAGE_SIZE = unix::get());
PAGE_SIZE
}
}
#[cfg(unix)]
#[inline]
fn get_granularity_helper() -> usize {
get_helper()
}
#[cfg(unix)]
mod unix {
use libc::{sysconf, _SC_PAGESIZE};
#[inline]
pub fn get() -> usize {
unsafe { sysconf(_SC_PAGESIZE) as usize }
}
}
#[cfg(all(not(target_os = "emscripten"), any(target_arch = "wasm32", target_arch = "wasm64")))]
#[inline]
fn get_granularity_helper() -> usize {
65536
}
#[cfg(all(windows, feature = "no_std"))]
#[inline]
fn get_helper() -> usize {
static INIT: Once<usize> = Once::new();
*INIT.call_once(windows::get)
}
#[cfg(all(windows, not(feature = "no_std")))]
#[inline]
fn get_helper() -> usize {
static INIT: Once = Once::new();
static mut PAGE_SIZE: usize = 0;
unsafe {
INIT.call_once(|| PAGE_SIZE = windows::get());
PAGE_SIZE
}
}
#[cfg(all(windows, feature = "no_std"))]
#[inline]
fn get_granularity_helper() -> usize {
static GRINIT: Once<usize> = Once::new();
*GRINIT.call_once(windows::get_granularity)
}
#[cfg(all(windows, not(feature = "no_std")))]
#[inline]
fn get_granularity_helper() -> usize {
static GRINIT: Once = Once::new();
static mut GRANULARITY: usize = 0;
unsafe {
GRINIT.call_once(|| GRANULARITY = windows::get_granularity());
GRANULARITY
}
}
#[cfg(windows)]
mod windows {
#[cfg(feature = "no_std")]
use core::mem;
#[cfg(not(feature = "no_std"))]
use std::mem;
use winapi::um::sysinfoapi::GetSystemInfo;
use winapi::um::sysinfoapi::{LPSYSTEM_INFO, SYSTEM_INFO};
#[inline]
pub fn get() -> usize {
unsafe {
let mut info: SYSTEM_INFO = mem::zeroed();
GetSystemInfo(&mut info as LPSYSTEM_INFO);
info.dwPageSize as usize
}
}
#[inline]
pub fn get_granularity() -> usize {
unsafe {
let mut info: SYSTEM_INFO = mem::zeroed();
GetSystemInfo(&mut info as LPSYSTEM_INFO);
info.dwAllocationGranularity 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();
}
#[test]
fn test_get_granularity() {
#[allow(unused_variables)]
let granularity = get_granularity();
}
}