mmtk/util/heap/
heap_meta.rs

1use crate::util::heap::layout::vm_layout::vm_layout;
2use crate::util::heap::layout::Mmapper;
3use crate::util::os::{HugePageSupport, MmapAnnotation, MmapResult};
4use crate::util::Address;
5
6pub struct HeapMeta {
7    pub heap_cursor: Address,
8    pub heap_limit: Address,
9}
10
11impl HeapMeta {
12    pub fn new() -> Self {
13        HeapMeta {
14            heap_cursor: vm_layout().heap_start,
15            heap_limit: vm_layout().heap_end,
16        }
17    }
18
19    pub fn reserve_quarantined(
20        &mut self,
21        extent: usize,
22        align: Option<usize>,
23        top: bool,
24        mmapper: &dyn Mmapper,
25        huge_page_option: HugePageSupport,
26        anno: &MmapAnnotation,
27    ) -> MmapResult<Address> {
28        let start = if top {
29            let raw_start = self.heap_limit - extent;
30            if let Some(align) = align {
31                raw_start.align_down(align)
32            } else {
33                raw_start
34            }
35        } else {
36            let raw_start = self.heap_cursor;
37            if let Some(align) = align {
38                raw_start.align_up(align)
39            } else {
40                raw_start
41            }
42        };
43
44        // TODO: The following call do an fixed mmap. We should try to allow the OS to choose the address if the fixed mmap fails.
45        mmapper.quarantine_address_range(
46            start,
47            crate::util::conversions::bytes_to_pages_up(extent),
48            huge_page_option,
49            anno,
50        )?;
51
52        if top {
53            self.heap_limit = start;
54        } else {
55            self.heap_cursor = start + extent;
56        }
57
58        assert!(
59            self.heap_cursor <= self.heap_limit,
60            "Out of virtual address space after quarantining [{}, {})",
61            start,
62            start + extent,
63        );
64
65        Ok(start)
66    }
67
68    pub fn get_discontig_start(&self) -> Address {
69        self.heap_cursor
70    }
71
72    pub fn get_discontig_end(&self) -> Address {
73        self.heap_limit - 1
74    }
75}
76
77// make clippy happy
78impl Default for HeapMeta {
79    fn default() -> Self {
80        Self::new()
81    }
82}