Skip to content

Commit 59ff447

Browse files
committed
Replace OnAllocationFail with boolean options.
1 parent cb673a1 commit 59ff447

File tree

4 files changed

+46
-42
lines changed

4 files changed

+46
-42
lines changed

src/policy/lockfreeimmortalspace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<VM: VMBinding> Space<VM> for LockFreeImmortalSpace<VM> {
151151
})
152152
.expect("update cursor failed");
153153
if start + bytes > self.limit {
154-
if alloc_options.on_fail.allow_oom_call() {
154+
if alloc_options.allow_oom_call() {
155155
panic!("OutOfMemory");
156156
} else {
157157
return Address::ZERO;

src/policy/space.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub trait Space<VM: VMBinding>: 'static + SFT + Sync + Downcast {
8585
alloc_options: AllocationOptions,
8686
) -> bool {
8787
if self.will_oom_on_acquire(size) {
88-
if alloc_options.on_fail.allow_oom_call() {
88+
if alloc_options.allow_oom_call() {
8989
VM::VMCollection::out_of_memory(
9090
tls,
9191
crate::util::alloc::AllocationError::HeapOutOfMemory,
@@ -111,18 +111,18 @@ pub trait Space<VM: VMBinding>: 'static + SFT + Sync + Downcast {
111111
// Should we poll to attempt to GC?
112112
// - If tls is collector, we shall not poll.
113113
// - If gc is disabled, we shall not poll.
114-
let should_poll = VM::VMActivePlan::is_mutator(tls)
115-
&& VM::VMCollection::is_collection_enabled();
114+
let should_poll =
115+
VM::VMActivePlan::is_mutator(tls) && VM::VMCollection::is_collection_enabled();
116116

117117
// Can we continue to allocate even if GC is triggered?
118-
let allow_overcommit = alloc_options.on_fail.allow_overcommit();
118+
let allow_overcommit = alloc_options.allow_overcommit;
119119

120120
// Can we block for GC if polling triggers GC?
121121
// - If the MMTk instance is not initialized, there is no GC workers, and we cannot block for GC.
122122
// - If the on_fail option does not allow blocking, we do not block for GC, either.
123123
let allow_blocking_for_gc = should_poll
124124
&& self.common().global_state.is_initialized()
125-
&& alloc_options.on_fail.allow_blocking_for_gc();
125+
&& alloc_options.at_safepoint;
126126

127127
trace!("Reserving pages");
128128
let pr = self.get_page_resource();

src/util/alloc/allocator.rs

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,47 +28,54 @@ pub enum AllocationError {
2828
MmapOutOfMemory,
2929
}
3030

31-
/// Behavior when an allocation fails, and a GC is expected.
32-
#[repr(u8)]
33-
#[derive(Copy, Clone, Default, PartialEq, bytemuck::NoUninit, Debug)]
34-
pub enum OnAllocationFail {
35-
/// Request the GC and block until GC finishes. This is the default behavior.
36-
#[default]
37-
RequestGC,
38-
/// Request the GC. But instead of blocking for GC, the allocation request returns with a
39-
/// failure value.
40-
ReturnFailure,
41-
/// Request the GC. But instead of blocking for GC, the allocating thread continues to
42-
/// allocate, overcommitting the memory. GC will be scheduled asynchronously by the GC worker
43-
/// threads, and the current mutator may stop at a safepoint as soon as possible.
44-
OverCommit,
45-
}
46-
47-
impl OnAllocationFail {
48-
pub(crate) fn allow_oom_call(&self) -> bool {
49-
*self == Self::RequestGC
50-
}
51-
pub(crate) fn allow_overcommit(&self) -> bool {
52-
*self == Self::OverCommit
53-
}
54-
pub(crate) fn allow_blocking_for_gc(&self) -> bool {
55-
*self == Self::RequestGC
56-
}
57-
}
58-
5931
/// Allow specifying different behaviors with [`Allocator::alloc_with_options`].
6032
#[repr(C)]
61-
#[derive(Copy, Clone, Default, PartialEq, bytemuck::NoUninit, Debug)]
33+
#[derive(Copy, Clone, PartialEq, bytemuck::NoUninit, Debug)]
6234
pub struct AllocationOptions {
63-
/// When the allocation fails and a GC is originally expected, on_fail
64-
/// allows a different behavior to avoid the GC.
65-
pub on_fail: OnAllocationFail,
35+
/// Whether polling is allowed.
36+
///
37+
/// If true, the allocation attempt will give the GC trigger a chance to schedule a collection
38+
/// if the GC trigger considers it needed.
39+
///
40+
/// If false, the allocation attempt will not notify the GC trigger, and GC will not be
41+
/// scheduled.
42+
pub allow_polling: bool,
43+
/// Whether the allocation is at a safepoint.
44+
///
45+
/// If true, the allocation attempt will block for GC if GC is triggered. It will also call
46+
/// [`Collection::out_of_memory`] when out of memory.
47+
///
48+
/// If false, the allocation attempt will never block for GC, and it will never call
49+
/// [`Collection::out_of_memory`]. Instead it returns an null address when GC is triggerted.
50+
pub at_safepoint: bool,
51+
/// Whether over-committing is allowed at this allocation site. Only meaningful if
52+
/// `allow_polling == false`.
53+
///
54+
/// If true, the allocation attempt will still try to allocate even if the GC trigger has
55+
/// triggered a GC.
56+
pub allow_overcommit: bool,
57+
}
58+
59+
/// The default value for `AllocationOptions` has the same semantics as calling [`Allocator::alloc`]
60+
/// directly.
61+
impl Default for AllocationOptions {
62+
fn default() -> Self {
63+
Self {
64+
allow_polling: true,
65+
at_safepoint: true,
66+
allow_overcommit: false,
67+
}
68+
}
6669
}
6770

6871
impl AllocationOptions {
6972
pub(crate) fn is_default(&self) -> bool {
7073
*self == AllocationOptions::default()
7174
}
75+
76+
pub(crate) fn allow_oom_call(&self) -> bool {
77+
self.at_safepoint
78+
}
7279
}
7380

7481
pub fn align_allocation_no_fill<VM: VMBinding>(
@@ -380,9 +387,7 @@ pub trait Allocator<VM: VMBinding>: Downcast {
380387
return result;
381388
}
382389

383-
if result.is_zero()
384-
&& self.get_context().get_alloc_options().on_fail == OnAllocationFail::ReturnFailure
385-
{
390+
if result.is_zero() && !self.get_context().get_alloc_options().allow_oom_call() {
386391
return result;
387392
}
388393

src/util/alloc/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ pub use allocator::fill_alignment_gap;
66
pub use allocator::AllocationError;
77
pub use allocator::AllocationOptions;
88
pub use allocator::Allocator;
9-
pub use allocator::OnAllocationFail;
109

1110
/// A list of all the allocators, embedded in Mutator
1211
pub(crate) mod allocators;

0 commit comments

Comments
 (0)