aligned_vmem/try_page_size.rs
1use crate::error::VmemError;
2use crate::page_size::{page_size_or_poison, PAGE_SIZE_QUERY_FAILED};
3
4/// Fallible [`page_size`](crate::page_size::page_size): the same cached
5/// one-time OS page-size query, with a channel for the one thing `page_size`
6/// cannot report — the query itself having failed.
7///
8/// [`page_size`](crate::page_size::page_size) is deliberately infallible: it
9/// returns the conservative [`MIN_PAGE`](crate::MIN_PAGE) floor when the
10/// one-time OS query produced an unusable answer, and the crate fails every
11/// page-granular state operation closed from then on (see `page_size`'s own
12/// "If the one-time OS query fails" paragraph). This twin is the upfront
13/// detector: a caller that wants to choose a strategy at startup — rather
14/// than discover the degraded state through `try_decommit` errors — asks
15/// here once.
16///
17/// # Errors
18///
19/// [`VmemError::os_refusal_unknown_code`] if the OS page-size query failed
20/// (not observed on any supported platform; see `page_size`'s rustdoc for
21/// why). The error is an OS-side no-code failure, NOT
22/// [`VmemError::invalid_argument`] — the caller did nothing wrong.
23///
24/// On success, the returned value is identical to `page_size()`'s: a power
25/// of two `>= MIN_PAGE`, stable for the process lifetime.
26pub fn try_page_size() -> Result<usize, VmemError> {
27 let v = page_size_or_poison();
28 if v == PAGE_SIZE_QUERY_FAILED {
29 Err(VmemError::os_refusal_unknown_code())
30 } else {
31 Ok(v)
32 }
33}