@@ -28,47 +28,54 @@ pub enum AllocationError {
2828MmapOutOfMemory , 
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 ) ]  
6234pub  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
6871impl  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
7481pub  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
0 commit comments