diff --git a/src/collect.rs b/src/collect.rs index aa6236c..e6ccf59 100644 --- a/src/collect.rs +++ b/src/collect.rs @@ -165,6 +165,11 @@ impl ObjectSpace { Cc::new_in_space(value, self) } + /// Leak all objects allocated in this space + pub fn leak(&self) { + *self.list.borrow_mut() = new_gc_list(); + } + // TODO: Consider implementing "merge" or method to collect multiple spaces // together, to make it easier to support generational collection. } @@ -247,6 +252,11 @@ pub fn count_thread_tracked() -> usize { thread_local!(pub(crate) static THREAD_OBJECT_SPACE: ObjectSpace = ObjectSpace::default()); +/// Acquire reference to thread-local global object space +pub fn with_thread_object_space(handler: impl FnOnce(&ObjectSpace) -> R) -> R { + THREAD_OBJECT_SPACE.with(handler) +} + /// Create an empty linked list with a dummy GcHeader. pub(crate) fn new_gc_list() -> Pin> { let pinned = Box::pin(GcHeader::empty()); diff --git a/src/lib.rs b/src/lib.rs index 2a0a5c7..f0b877c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -275,7 +275,9 @@ mod trace; mod trace_impls; pub use cc::{Cc, RawCc, RawWeak, Weak}; -pub use collect::{collect_thread_cycles, count_thread_tracked, ObjectSpace}; +pub use collect::{ + collect_thread_cycles, count_thread_tracked, with_thread_object_space, ObjectSpace, +}; pub use trace::{Trace, Tracer}; #[cfg(feature = "sync")] diff --git a/src/tests.rs b/src/tests.rs index 86b6e36..516e13c 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,6 +1,6 @@ -use crate::debug; use crate::testutil::test_small_graph; use crate::{collect, Cc, Trace, Tracer}; +use crate::{debug, with_thread_object_space}; use std::cell::Cell; use std::cell::RefCell; use std::ops::Deref; @@ -509,6 +509,17 @@ fn test_trace_impl_double_visits() { } } +#[test] +#[ignore = "causes memory leak, thus causing valgrind to error"] +fn leak() { + let a = Cc::new(1); + let b = Cc::new((a.clone(), 1)); + with_thread_object_space(|s| s.leak()); + assert_eq!(crate::count_thread_tracked(), 0); + assert_eq!(*a, 1); + let _ = b; +} + #[cfg(not(miri))] quickcheck::quickcheck! { fn test_quickcheck_16_vertex_graph(edges: Vec, atomic_bits: u16, collect_bits: u16) -> bool {