Skip to content

Commit b066199

Browse files
authored
Add missing docs for the rest of the util module (#1026)
This PR is a step towards #309. * Deny `missing_docs` for the `util` module and the `vm` module. * Change some items from `pub` to `pub(crate)`. * Remove some unused constants.
1 parent 9786ce8 commit b066199

30 files changed

+247
-175
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ pub mod build_info;
6161
pub mod memory_manager;
6262
pub mod plan;
6363
pub mod scheduler;
64+
#[deny(missing_docs)]
6465
pub mod util;
66+
#[deny(missing_docs)]
6567
pub mod vm;
6668

6769
pub use crate::plan::{

src/plan/immix/global.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub const IMMIX_CONSTRAINTS: PlanConstraints = PlanConstraints {
4040
gc_header_bits: 2,
4141
gc_header_words: 0,
4242
num_specialized_scans: 1,
43-
/// Max immix object size is half of a block.
43+
// Max immix object size is half of a block.
4444
max_non_los_default_alloc_bytes: crate::policy::immix::MAX_IMMIX_OBJECT_SIZE,
4545
needs_prepare_mutator: false,
4646
..PlanConstraints::default()

src/plan/plan_constraints.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ impl PlanConstraints {
5151
num_specialized_scans: 0,
5252
max_non_los_default_alloc_bytes: MAX_INT,
5353
max_non_los_copy_bytes: MAX_INT,
54-
needs_linear_scan: SUPPORT_CARD_SCANNING || LAZY_SWEEP,
54+
// As `LAZY_SWEEP` is true, needs_linear_scan is true for all the plans. This is strange.
55+
// https://github.com/mmtk/mmtk-core/issues/1027 trackes the issue.
56+
needs_linear_scan: crate::util::constants::SUPPORT_CARD_SCANNING
57+
|| crate::util::constants::LAZY_SWEEP,
5558
needs_concurrent_workers: false,
5659
generate_gc_trace: false,
5760
may_trace_duplicate_edges: false,

src/policy/sft_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ mod dense_chunk_map {
410410

411411
pub fn new() -> Self {
412412
Self {
413-
/// Empty space is at index 0
413+
// Empty space is at index 0
414414
sft: vec![SFTRefStorage::default()],
415415
index_map: HashMap::new(),
416416
}

src/util/address.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,17 @@ impl Shl<usize> for Address {
129129
}
130130

131131
impl Address {
132+
/// The lowest possible address.
132133
pub const ZERO: Self = Address(0);
134+
/// The highest possible address.
133135
pub const MAX: Self = Address(usize::max_value());
134136

135137
/// creates Address from a pointer
136138
pub fn from_ptr<T>(ptr: *const T) -> Address {
137139
Address(ptr as usize)
138140
}
139141

142+
/// creates Address from a Rust reference
140143
pub fn from_ref<T>(r: &T) -> Address {
141144
Address(r as *const T as usize)
142145
}
@@ -180,10 +183,12 @@ impl Address {
180183
// These const functions are duplicated with the operator traits. But we need them,
181184
// as we need them to declare constants.
182185

186+
/// Get the number of bytes between two addresses. The current address needs to be higher than the other address.
183187
pub const fn get_extent(self, other: Address) -> ByteSize {
184188
self.0 - other.0
185189
}
186190

191+
/// Get the offset from `other` to `self`. The result is negative is `self` is lower than `other`.
187192
pub const fn get_offset(self, other: Address) -> ByteOffset {
188193
self.0 as isize - other.0 as isize
189194
}
@@ -192,6 +197,7 @@ impl Address {
192197
// The add() function is const fn, and we can use it to declare Address constants.
193198
// The Add trait function cannot be const.
194199
#[allow(clippy::should_implement_trait)]
200+
/// Add an offset to the address.
195201
pub const fn add(self, size: usize) -> Address {
196202
Address(self.0 + size)
197203
}
@@ -200,15 +206,17 @@ impl Address {
200206
// The sub() function is const fn, and we can use it to declare Address constants.
201207
// The Sub trait function cannot be const.
202208
#[allow(clippy::should_implement_trait)]
209+
/// Subtract an offset from the address.
203210
pub const fn sub(self, size: usize) -> Address {
204211
Address(self.0 - size)
205212
}
206213

214+
/// Bitwise 'and' with a mask.
207215
pub const fn and(self, mask: usize) -> usize {
208216
self.0 & mask
209217
}
210218

211-
// Perform a saturating subtract on the Address
219+
/// Perform a saturating subtract on the Address
212220
pub const fn saturating_sub(self, size: usize) -> Address {
213221
Address(self.0.saturating_sub(size))
214222
}
@@ -473,6 +481,7 @@ use crate::vm::VMBinding;
473481
pub struct ObjectReference(usize);
474482

475483
impl ObjectReference {
484+
/// The null object reference, represented as zero.
476485
pub const NULL: ObjectReference = ObjectReference(0);
477486

478487
/// Cast the object reference to its raw address. This method is mostly for the convinience of a binding.
@@ -511,6 +520,9 @@ impl ObjectReference {
511520
VM::VMObjectModel::ref_to_header(self)
512521
}
513522

523+
/// Get the start of the allocation address for the object. This method is used by MMTk to get the start of the allocation
524+
/// address originally returned from [`crate::memory_manager::alloc`] for the object.
525+
/// This method is syntactic sugar for [`crate::vm::ObjectModel::ref_to_object_start`]. See comments on [`crate::vm::ObjectModel::ref_to_object_start`].
514526
pub fn to_object_start<VM: VMBinding>(self) -> Address {
515527
use crate::vm::ObjectModel;
516528
let object_start = VM::VMObjectModel::ref_to_object_start(self);
@@ -557,6 +569,7 @@ impl ObjectReference {
557569
}
558570
}
559571

572+
/// Can the object be moved?
560573
pub fn is_movable(self) -> bool {
561574
unsafe { SFT_MAP.get_unchecked(Address(self.0)) }.is_movable()
562575
}
@@ -566,10 +579,12 @@ impl ObjectReference {
566579
unsafe { SFT_MAP.get_unchecked(Address(self.0)) }.get_forwarded_object(self)
567580
}
568581

582+
/// Is the object in any MMTk spaces?
569583
pub fn is_in_any_space(self) -> bool {
570584
unsafe { SFT_MAP.get_unchecked(Address(self.0)) }.is_in_space(self)
571585
}
572586

587+
/// Is the object sane?
573588
#[cfg(feature = "sanity")]
574589
pub fn is_sane(self) -> bool {
575590
unsafe { SFT_MAP.get_unchecked(Address(self.0)) }.is_sane()

src/util/alloc/allocator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub fn align_allocation_inner<VM: VMBinding>(
8383
region + delta
8484
}
8585

86+
/// Fill the specified region with the alignment value.
8687
pub fn fill_alignment_gap<VM: VMBinding>(immut_start: Address, end: Address) {
8788
let mut start = immut_start;
8889

src/util/alloc/allocators.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,27 +167,35 @@ impl<VM: VMBinding> Allocators<VM> {
167167
}
168168
}
169169

170-
// This type describe which allocator in the allocators set.
171-
// For VM binding implementors, this type is equivalent to the following native types:
172-
// #[repr(C)]
173-
// struct AllocatorSelector {
174-
// tag: AllocatorSelectorTag,
175-
// payload: u8,
176-
// }
177-
// #[repr(u8)]
178-
// enum AllocatorSelectorTag {
179-
// BumpPointer,
180-
// LargeObject,
181-
// }
170+
/// This type describe an allocator in the [`crate::Mutator`].
171+
/// For some VM bindings, they may need to access this type from native code. This type is equivalent to the following native types:
172+
/// #[repr(C)]
173+
/// struct AllocatorSelector {
174+
/// tag: AllocatorSelectorTag,
175+
/// payload: u8,
176+
/// }
177+
/// #[repr(u8)]
178+
/// enum AllocatorSelectorTag {
179+
/// BumpPointer,
180+
/// LargeObject,
181+
/// ...
182+
/// }
182183
#[repr(C, u8)]
183184
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
184185
pub enum AllocatorSelector {
186+
/// Represents a [`crate::util::alloc::bumpallocator::BumpAllocator`].
185187
BumpPointer(u8),
188+
/// Represents a [`crate::util::alloc::large_object_allocator::LargeObjectAllocator`].
186189
LargeObject(u8),
190+
/// Represents a [`crate::util::alloc::malloc_allocator::MallocAllocator`].
187191
Malloc(u8),
192+
/// Represents a [`crate::util::alloc::immix_allocator::ImmixAllocator`].
188193
Immix(u8),
194+
/// Represents a [`crate::util::alloc::markcompact_allocator::MarkCompactAllocator`].
189195
MarkCompact(u8),
196+
/// Represents a [`crate::util::alloc::free_list_allocator::FreeListAllocator`].
190197
FreeList(u8),
198+
/// No allocator found.
191199
#[default]
192200
None,
193201
}
@@ -197,11 +205,15 @@ pub enum AllocatorSelector {
197205
#[repr(C, u8)]
198206
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
199207
pub enum AllocatorInfo {
208+
/// This allocator uses a [`crate::util::alloc::bumpallocator::BumpPointer`] as its fastpath.
200209
BumpPointer {
210+
/// The byte offset from the mutator's pointer to the [`crate::util::alloc::bumpallocator::BumpPointer`].
201211
bump_pointer_offset: usize,
202212
},
213+
/// This allocator uses a fastpath, but we haven't implemented it yet.
203214
// FIXME: Add free-list fast-path
204215
Unimplemented,
216+
/// This allocator does not have a fastpath.
205217
#[default]
206218
None,
207219
}

src/util/alloc/bumpallocator.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ use crate::util::Address;
55
use crate::util::alloc::Allocator;
66

77
use crate::policy::space::Space;
8-
use crate::util::conversions::bytes_to_pages;
8+
use crate::util::conversions::bytes_to_pages_up;
99
use crate::util::opaque_pointer::*;
1010
use crate::vm::VMBinding;
1111

1212
const BYTES_IN_PAGE: usize = 1 << 12;
1313
const BLOCK_SIZE: usize = 8 * BYTES_IN_PAGE;
1414
const BLOCK_MASK: usize = BLOCK_SIZE - 1;
1515

16+
/// A bump pointer allocator. It keeps a thread local allocation buffer,
17+
/// and bumps a cursor to allocate from the buffer.
1618
#[repr(C)]
1719
pub struct BumpAllocator<VM: VMBinding> {
1820
/// [`VMThread`] associated with this allocator instance
@@ -32,11 +34,14 @@ pub struct BumpAllocator<VM: VMBinding> {
3234
#[repr(C)]
3335
#[derive(Copy, Clone)]
3436
pub struct BumpPointer {
37+
/// The cursor inside the allocation buffer where the next object will be allocated.
3538
pub cursor: Address,
39+
/// The upperbound of the allocation buffer.
3640
pub limit: Address,
3741
}
3842

3943
impl BumpPointer {
44+
/// Reset the cursor and limit to the given values.
4045
pub fn reset(&mut self, start: Address, end: Address) {
4146
self.cursor = start;
4247
self.limit = end;
@@ -46,7 +51,7 @@ impl BumpPointer {
4651
impl std::default::Default for BumpPointer {
4752
/// Defaults to 0,0. In this case, the first
4853
/// allocation would naturally fail the check
49-
/// `cursor + size < limit`, and go to the slowpath.
54+
/// `cursor + size < limit`, and go to the slowpath.
5055
fn default() -> Self {
5156
BumpPointer {
5257
cursor: Address::ZERO,
@@ -56,16 +61,16 @@ impl std::default::Default for BumpPointer {
5661
}
5762

5863
impl<VM: VMBinding> BumpAllocator<VM> {
59-
pub fn set_limit(&mut self, start: Address, limit: Address) {
64+
pub(crate) fn set_limit(&mut self, start: Address, limit: Address) {
6065
self.bump_pointer.reset(start, limit);
6166
}
6267

63-
pub fn reset(&mut self) {
68+
pub(crate) fn reset(&mut self) {
6469
let zero = unsafe { Address::zero() };
6570
self.bump_pointer.reset(zero, zero);
6671
}
6772

68-
pub fn rebind(&mut self, space: &'static dyn Space<VM>) {
73+
pub(crate) fn rebind(&mut self, space: &'static dyn Space<VM>) {
6974
self.reset();
7075
self.space = space;
7176
}
@@ -194,7 +199,7 @@ impl<VM: VMBinding> BumpAllocator<VM> {
194199
}
195200

196201
let block_size = (size + BLOCK_MASK) & (!BLOCK_MASK);
197-
let acquired_start = self.space.acquire(self.tls, bytes_to_pages(block_size));
202+
let acquired_start = self.space.acquire(self.tls, bytes_to_pages_up(block_size));
198203
if acquired_start.is_zero() {
199204
trace!("Failed to acquire a new block");
200205
acquired_start

src/util/alloc/free_list_allocator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use super::allocator::AllocatorContext;
1515
/// A MiMalloc free list allocator
1616
#[repr(C)]
1717
pub struct FreeListAllocator<VM: VMBinding> {
18+
/// [`VMThread`] associated with this allocator instance
1819
pub tls: VMThread,
1920
space: &'static MarkSweepSpace<VM>,
2021
context: Arc<AllocatorContext<VM>>,

src/util/alloc/immix_allocator.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use crate::vm::*;
1616
/// Immix allocator
1717
#[repr(C)]
1818
pub struct ImmixAllocator<VM: VMBinding> {
19+
/// [`VMThread`] associated with this allocator instance
1920
pub tls: VMThread,
21+
/// The fastpath bump pointer.
2022
pub bump_pointer: BumpPointer,
2123
/// [`Space`](src/policy/space/Space) instance associated with this allocator instance.
2224
space: &'static ImmixSpace<VM>,
@@ -34,7 +36,7 @@ pub struct ImmixAllocator<VM: VMBinding> {
3436
}
3537

3638
impl<VM: VMBinding> ImmixAllocator<VM> {
37-
pub fn reset(&mut self) {
39+
pub(crate) fn reset(&mut self) {
3840
self.bump_pointer.reset(Address::ZERO, Address::ZERO);
3941
self.large_bump_pointer.reset(Address::ZERO, Address::ZERO);
4042
self.request_for_large = false;
@@ -183,7 +185,7 @@ impl<VM: VMBinding> ImmixAllocator<VM> {
183185
}
184186
}
185187

186-
pub fn immix_space(&self) -> &'static ImmixSpace<VM> {
188+
pub(crate) fn immix_space(&self) -> &'static ImmixSpace<VM> {
187189
self.space
188190
}
189191

src/util/alloc/large_object_allocator.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use crate::vm::VMBinding;
99

1010
use super::allocator::AllocatorContext;
1111

12+
/// An allocator that only allocates at page granularity.
13+
/// This is intended for large objects.
1214
#[repr(C)]
1315
pub struct LargeObjectAllocator<VM: VMBinding> {
1416
/// [`VMThread`] associated with this allocator instance

src/util/alloc/malloc_allocator.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use crate::vm::VMBinding;
99

1010
use super::allocator::AllocatorContext;
1111

12+
/// The allocator that internally uses malloc for all the allocation requests.
13+
/// This allocator is only intended for experimental uses.
1214
#[repr(C)]
1315
pub struct MallocAllocator<VM: VMBinding> {
1416
/// [`VMThread`] associated with this allocator instance

src/util/alloc/markcompact_allocator.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ pub struct MarkCompactAllocator<VM: VMBinding> {
1616
}
1717

1818
impl<VM: VMBinding> MarkCompactAllocator<VM> {
19-
pub fn set_limit(&mut self, cursor: Address, limit: Address) {
19+
pub(crate) fn set_limit(&mut self, cursor: Address, limit: Address) {
2020
self.bump_allocator.set_limit(cursor, limit);
2121
}
2222

23-
pub fn reset(&mut self) {
23+
pub(crate) fn reset(&mut self) {
2424
self.bump_allocator.reset();
2525
}
2626

27-
pub fn rebind(&mut self, space: &'static dyn Space<VM>) {
27+
pub(crate) fn rebind(&mut self, space: &'static dyn Space<VM>) {
2828
self.bump_allocator.rebind(space);
2929
}
3030
}
@@ -89,6 +89,7 @@ impl<VM: VMBinding> Allocator<VM> for MarkCompactAllocator<VM> {
8989
}
9090

9191
impl<VM: VMBinding> MarkCompactAllocator<VM> {
92+
/// The number of bytes that the allocator reserves for its own header.
9293
pub const HEADER_RESERVED_IN_BYTES: usize =
9394
crate::policy::markcompactspace::MarkCompactSpace::<VM>::HEADER_RESERVED_IN_BYTES;
9495
pub(crate) fn new(

src/util/alloc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub use malloc_allocator::MallocAllocator;
2727
pub mod immix_allocator;
2828
pub use self::immix_allocator::ImmixAllocator;
2929

30-
// Free list allocator based on Mimalloc
30+
/// Free list allocator based on Mimalloc
3131
pub mod free_list_allocator;
3232
pub use free_list_allocator::FreeListAllocator;
3333

0 commit comments

Comments
 (0)