Broom 1.0.0
A thread-local C++ Garbage Collector
Loading...
Searching...
No Matches
garbage-collector.cc
Go to the documentation of this file.
2
3#include "src/allocator-inl.h"
4#include "src/allocator.h"
6#include "src/globals.h"
7#include "src/macros.h"
8#include "src/roots-inl.h"
9#include "src/roots.h"
10
11namespace broom {
12// Atomically mark all the managed pointers. This will never be interrupted.
14 BASSERT_EQ(to_collect_.size(), 0, "to_collect size is not 0");
15 to_collect_.swap(alive_);
16 // First go through the stack to try and identify our roots on the stack.
17 root_visitor_.IterateStackAndVisitRoots();
18 // Now go through the external roots, such as unmanaged pointers that might be
19 // pointing to managed memory as the application told us.
20 for (const ExternalRoot& external_root : external_roots_) {
22 }
23 // Finally, go through the precise roots for which the application will trace
24 // its own pointers for us.
25 for (PreciseRoot& precise_root : precise_roots_) {
26 root_visitor_.Visit<RootKind::kPrecise>(&precise_root);
27 }
28
29 // If we support sharing, mark through the pinned allocations.
30 if (configuration_.support_sharing) {
31 // Now go through the unmarked pointers and check if any of them are pinned.
32 // This can happen through BroomSharingPointers. If the pointer is pinned,
33 // move it to the alive set. Otherwise, we will sweep it later. We do this
34 // during marking because it is easier to reason about in the context of
35 // concurrency. When we implement concurrent and incremental sweeping, we
36 // might choose to do it differently.
37 // FIXME(gc): This is a quite ugly way to do this.
38 auto it = to_collect_.begin();
39 while (it != to_collect_.end()) {
40 if (Allocator::IsPinnedBasePointer(it->Pointer())) {
41 // Scan anything that this root points to.
42 root_visitor_.Visit<RootKind::kRegular>(it->Pointer());
43 it = to_collect_.begin();
44 } else {
45 ++it;
46 }
47 }
48 }
49}
50
52 for (const GcNode& node : alive_) {
55 metadata->UnmarkScanned();
56 }
57 for (auto it = to_collect_.begin(); it != to_collect_.end();) {
58 if (it->HasDestructor()) {
59 it->RunDestructor();
60 }
61
62 allocator_->Dispose(const_cast<void*>(it->Pointer()));
63 it = to_collect_.erase(it);
64 }
65}
66} // namespace broom
static constexpr const int kFrontMetadataSize
Definition allocator.h:238
virtual void Dispose(void *definitely_uintptr)=0
static bool IsPinnedBasePointer(const void *pointer)
Definition allocator.h:291
void IterateStackAndVisitRoots()
virtual void Visit(const void *pointer)
Definition roots.h:98
#define BASSERT_EQ(lhs, rhs, m,...)
Definition macros.h:58
std::queue< T, broom::deque< T > > queue
Definition broom-queue.h:12