Skip to content

Commit abd38ec

Browse files
authored
Use BumpPointer::default() (#993)
This PR changes the code snippet in our doc to use `BumpPointer::default()` to create a new zero-initialized bump pointer struct. It also changes our internal implementation to use `BumpPointer::default()` instead of `BumpPointer::new(zero, zero)`.
1 parent feb5471 commit abd38ec

File tree

3 files changed

+13
-27
lines changed

3 files changed

+13
-27
lines changed

src/util/alloc/bumpallocator.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ pub struct BumpAllocator<VM: VMBinding> {
2626

2727
/// A common fast-path bump-pointer allocator shared across different allocator implementations
2828
/// that use bump-pointer allocation.
29+
/// A `BumpPointer` is always initialized with cursor = 0, limit = 0, so the first allocation
30+
/// always fails the check of `cursor + size < limit` and goes to the slowpath. A binding
31+
/// can also take advantage of this design to zero-initialize the a bump pointer.
2932
#[repr(C)]
3033
#[derive(Copy, Clone)]
3134
pub struct BumpPointer {
@@ -34,24 +37,17 @@ pub struct BumpPointer {
3437
}
3538

3639
impl BumpPointer {
37-
pub const fn new(start: Address, end: Address) -> Self {
38-
BumpPointer {
39-
cursor: start,
40-
limit: end,
41-
}
42-
}
43-
4440
pub fn reset(&mut self, start: Address, end: Address) {
4541
self.cursor = start;
4642
self.limit = end;
4743
}
4844
}
4945

5046
impl std::default::Default for BumpPointer {
47+
/// Defaults to 0,0. In this case, the first
48+
/// allocation would naturally fail the check
49+
/// `cursor + size < limit`, and go to the slowpath.
5150
fn default() -> Self {
52-
// Defaults to 0,0. In this case, the first
53-
// allocation would naturally fail the check
54-
// `cursor + size < limit`, and go to the slowpath.
5551
BumpPointer {
5652
cursor: Address::ZERO,
5753
limit: Address::ZERO,
@@ -180,7 +176,7 @@ impl<VM: VMBinding> BumpAllocator<VM> {
180176
) -> Self {
181177
BumpAllocator {
182178
tls,
183-
bump_pointer: unsafe { BumpPointer::new(Address::zero(), Address::zero()) },
179+
bump_pointer: BumpPointer::default(),
184180
space,
185181
context,
186182
}

src/util/alloc/immix_allocator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,10 @@ impl<VM: VMBinding> ImmixAllocator<VM> {
174174
tls,
175175
space: space.unwrap().downcast_ref::<ImmixSpace<VM>>().unwrap(),
176176
context,
177-
bump_pointer: BumpPointer::new(Address::ZERO, Address::ZERO),
177+
bump_pointer: BumpPointer::default(),
178178
hot: false,
179179
copy,
180-
large_bump_pointer: BumpPointer::new(Address::ZERO, Address::ZERO),
180+
large_bump_pointer: BumpPointer::default(),
181181
request_for_large: false,
182182
line: None,
183183
}

vmbindings/dummyvm/src/tests/doc_mutator_storage.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,9 @@ pub fn embed_fastpath_struct() {
7676

7777
// Bind an MMTk mutator
7878
let mutator = mmtk::memory_manager::bind_mutator(&fixture.mmtk, tls_opaque_pointer);
79-
// Create a fastpath BumpPointer
80-
let default_bump_pointer = {
81-
// First find the allocator
82-
let selector = mmtk::memory_manager::get_allocator_mapping(
83-
&fixture.mmtk,
84-
AllocationSemantics::Default,
85-
);
86-
let default_allocator = unsafe {
87-
mutator.allocator_impl::<mmtk::util::alloc::BumpAllocator<DummyVM>>(selector)
88-
};
89-
// Copy the bump pointer struct
90-
default_allocator.bump_pointer
91-
};
79+
// Create a fastpath BumpPointer with default(). The BumpPointer from default() will guarantee to fail on the first allocation
80+
// so the allocation goes to the slowpath and we will get an allocation buffer from MMTk.
81+
let default_bump_pointer = BumpPointer::default();
9282
// Store the fastpath BumpPointer along with the mutator
9383
let mut storage = MutatorInTLS {
9484
default_bump_pointer,
@@ -119,7 +109,7 @@ pub fn embed_fastpath_struct() {
119109
default_allocator.bump_pointer = storage.default_bump_pointer;
120110
// Do slow path allocation with MMTk
121111
let addr = default_allocator.alloc_slow(size, 8, 0);
122-
// Copy bump pointer values to the fastpath BumpPointer
112+
// Copy bump pointer values to the fastpath BumpPointer so we will have an allocation buffer.
123113
storage.default_bump_pointer = default_allocator.bump_pointer;
124114
addr
125115
}

0 commit comments

Comments
 (0)