1#![no_std]
9
10#[macro_use]
11extern crate log;
12extern crate alloc;
13
14use core::fmt;
15use strum::{IntoStaticStr, VariantArray};
16
17const PAGE_SIZE: usize = 0x1000;
18
19mod page;
20pub use page::GlobalPage;
21
22#[cfg(feature = "tracking")]
24pub mod tracking;
25
26#[repr(u8)]
28#[derive(Debug, Clone, Copy, PartialEq, Eq, VariantArray, IntoStaticStr)]
29pub enum UsageKind {
30 RustHeap,
32 VirtMem,
34 PageCache,
36 PageTable,
38 Dma,
40 Global,
42}
43
44#[derive(Clone, Copy)]
46pub struct Usages([usize; UsageKind::VARIANTS.len()]);
47
48impl Usages {
49 const fn new() -> Self {
50 Self([0; UsageKind::VARIANTS.len()])
51 }
52
53 fn alloc(&mut self, kind: UsageKind, size: usize) {
54 self.0[kind as usize] += size;
55 }
56
57 fn dealloc(&mut self, kind: UsageKind, size: usize) {
58 self.0[kind as usize] -= size;
59 }
60
61 pub fn get(&self, kind: UsageKind) -> usize {
63 self.0[kind as usize]
64 }
65}
66
67impl fmt::Debug for Usages {
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 let mut d = f.debug_struct("UsageStats");
70 for &kind in UsageKind::VARIANTS {
71 d.field(kind.into(), &self.0[kind as usize]);
72 }
73 d.finish()
74 }
75}
76
77#[cfg(feature = "hv")]
80mod axvisor_impl;
81#[cfg(feature = "hv")]
82use axvisor_impl as imp;
83
84#[cfg(not(feature = "hv"))]
86mod default_impl;
87#[cfg(not(feature = "hv"))]
88use default_impl as imp;
89
90pub use imp::{GlobalAllocator, global_add_memory, global_init};
92
93pub use imp::DefaultByteAllocator;
95
96#[cfg(feature = "hv")]
98pub use imp::AddrTranslator;
99
100pub fn global_allocator() -> &'static GlobalAllocator {
102 imp::global_allocator()
103}