Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions oscars/src/collectors/mark_sweep/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,19 @@ unsafe impl allocator_api2::alloc::Allocator for MarkSweepGarbageCollector {

// raw byte allocations skip ensure_capacity
// and go straight to try_alloc_bytes
let result = self
.allocator
.borrow_mut()
.try_alloc_bytes(layout)
.map_err(|_| allocator_api2::alloc::AllocError)?;
let (result, needs_collect) = {
let mut alloc = self.allocator.borrow_mut();
let result = alloc
.try_alloc_bytes(layout)
.map_err(|_| allocator_api2::alloc::AllocError)?;
let needs_collect = !alloc.is_below_threshold();
(result, needs_collect)
};

// Keep deferred-collection behavior consistent with GC-node allocations.
if needs_collect {
self.collect_needed.set(true);
}

// debug only: track raw allocations for leak detection
#[cfg(all(debug_assertions, feature = "gc_allocator"))]
Expand Down
25 changes: 25 additions & 0 deletions oscars/src/collectors/mark_sweep/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,31 @@ fn gc_recursion() {
collector.collect();
}

#[cfg(feature = "gc_allocator")]
#[test]
fn external_allocations_set_deferred_collection_flag_when_threshold_exceeded() {
let collector = MarkSweepGarbageCollector::default()
.with_page_size(128)
.with_heap_threshold(1_024);

assert!(
!collector.collect_needed.get(),
"collect_needed should start false"
);

let mut external = allocator_api2::vec::Vec::<u8, &MarkSweepGarbageCollector>::with_capacity_in(
2_048, &collector,
);
external.push(1);

assert!(
collector.collect_needed.get(),
"raw allocator pressure should set deferred collection"
);

drop(external);
}

#[test]
fn drop_gc() {
let collector = &mut MarkSweepGarbageCollector::default()
Expand Down