#[cfg(feature = "watermark")]
use core::cmp;
use crate::platform;
#[cfg(feature = "watermark")]
const WORD: usize = size_of::<usize>();
#[cfg(feature = "watermark")]
const MARKER_BYTE: u8 = 0xA5;
#[cfg(feature = "watermark")]
const MARKER_WORD: usize = usize::from_ne_bytes([MARKER_BYTE; WORD]);
#[cfg(feature = "watermark")]
const TOP_MARGIN: usize = 512;
#[cfg(feature = "watermark")]
const BOTTOM_MARGIN: usize = 4096;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Stack {
base: usize,
limit: usize,
}
impl Stack {
pub const fn from_bounds(limit: usize, base: usize) -> Option<Self> {
if base > limit {
Some(Self { base, limit })
} else {
None
}
}
pub const fn from_limit_and_size(limit: usize, size: usize) -> Option<Self> {
if size == 0 {
return None;
}
match limit.checked_add(size) {
Some(base) => Some(Self { base, limit }),
None => None,
}
}
#[cfg(feature = "std")]
pub fn current() -> Option<Self> {
let (base, limit) = platform::current_bounds()?;
Self::from_bounds(limit, base)
}
pub const fn base(&self) -> usize {
self.base
}
pub const fn limit(&self) -> usize {
self.limit
}
pub const fn size(&self) -> usize {
self.base - self.limit
}
pub const fn contains(&self, addr: usize) -> bool {
addr >= self.limit && addr < self.base
}
#[inline]
pub fn depth(&self) -> Option<usize> {
self.depth_at(platform::sp())
}
pub const fn depth_at(&self, sp: usize) -> Option<usize> {
if self.contains(sp) {
Some(self.base - sp)
} else {
None
}
}
#[inline]
pub fn headroom(&self) -> Option<usize> {
self.headroom_at(platform::sp())
}
pub const fn headroom_at(&self, sp: usize) -> Option<usize> {
if self.contains(sp) {
Some(sp - self.limit)
} else {
None
}
}
#[cfg(feature = "watermark")]
pub unsafe fn paint(&self, max_depth: usize) -> Option<Paint> {
let sp = platform::sp();
if !self.contains(sp) {
return None;
}
let hi = align_down(sp.checked_sub(TOP_MARGIN)?);
let floor = self.limit.saturating_add(BOTTOM_MARGIN);
let lo = align_up(cmp::max(floor, hi.saturating_sub(max_depth)));
if hi < lo.saturating_add(WORD) {
return None;
}
unsafe {
core::ptr::write_bytes(core::hint::black_box(lo as *mut u8), MARKER_BYTE, hi - lo);
}
Some(Paint { lo, hi })
}
#[cfg(feature = "watermark")]
pub unsafe fn peak(&self, paint: &Paint) -> Peak {
match unsafe { deepest_touched(paint) } {
Some(addr) if addr == paint.lo => Peak::AtLeast(self.base.saturating_sub(addr)),
Some(addr) => Peak::Exact(self.base.saturating_sub(addr)),
None => Peak::Exact(self.base.saturating_sub(paint.hi)),
}
}
}
#[cfg(feature = "watermark")]
pub(crate) unsafe fn deepest_touched(paint: &Paint) -> Option<usize> {
let mut addr = paint.lo;
while addr < paint.hi {
let word = unsafe { (addr as *const usize).read_volatile() };
if word != MARKER_WORD {
return Some(addr);
}
addr += WORD;
}
None
}
#[cfg(feature = "watermark")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Paint {
lo: usize,
hi: usize,
}
#[cfg(feature = "watermark")]
impl Paint {
pub const fn from_bounds(lo: usize, hi: usize) -> Option<Self> {
if lo % WORD != 0 || hi % WORD != 0 {
return None;
}
match lo.checked_add(WORD) {
Some(min_hi) if hi >= min_hi => Some(Self { lo, hi }),
_ => None,
}
}
pub const fn lo(&self) -> usize {
self.lo
}
pub const fn hi(&self) -> usize {
self.hi
}
pub const fn bytes(&self) -> usize {
self.hi - self.lo
}
}
#[cfg(feature = "watermark")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum Peak {
Exact(usize),
AtLeast(usize),
}
#[cfg(feature = "watermark")]
impl Peak {
pub const fn bytes(&self) -> usize {
match self {
Self::Exact(bytes) | Self::AtLeast(bytes) => *bytes,
}
}
pub const fn is_saturated(&self) -> bool {
matches!(self, Self::AtLeast(_))
}
}
#[cfg(feature = "watermark")]
const fn align_down(addr: usize) -> usize {
addr & !(WORD - 1)
}
#[cfg(feature = "watermark")]
const fn align_up(addr: usize) -> usize {
align_down(addr.saturating_add(WORD - 1))
}