26 std::unique_lock<Mutex> lock(mutex_);
27 DCHECK(objects_.count(
object) == 0 || to_delete_.count(
object) == 1);
28 objects_.emplace(
object, 0u);
29 to_delete_.erase(
object);
36 std::unique_lock<Mutex> lock(mutex_);
42 std::unique_lock<Mutex> lock(mutex_);
43 auto* key =
const_cast<Traceable*
>(object);
44 DCHECK_EQ(objects_.count(key), 1u);
53 std::unique_lock<Mutex> lock(mutex_);
54 auto* key =
const_cast<Traceable*
>(object);
55 DCHECK_EQ(objects_.count(key), 1u);
56 CHECK_GT(objects_[key], 0u);
62 if (last_alive_time_.count(key) > 0)
68 std::unique_lock<Mutex> lock(mutex_);
69 std::unordered_set<const Traceable*> ret;
70 ret.reserve(objects_.size());
71 for (
auto& pair : objects_) {
72 if (pair.second != 0 || IsJsAlive(pair.first))
73 ret.insert(pair.first);
79 const std::unordered_set<const Traceable*>& alive) {
80 std::unique_lock<Mutex> lock(mutex_);
81 std::unordered_set<Traceable*> to_delete;
82 to_delete.reserve(objects_.size());
83 for (
auto pair : objects_) {
87 if (pair.second == 0u && alive.count(pair.first) == 0 &&
88 !IsJsAlive(pair.first)) {
89 to_delete.insert(pair.first);
92 to_delete_ = to_delete;
94 DestroyObjects(to_delete, &lock);
98 : mutex_(
"ObjectTracker"), tracer_(tracer) {}
101 CHECK(objects_.empty());
104 bool ObjectTracker::IsJsAlive(
Traceable*
object)
const {
107 if (last_alive_time_.count(
object) == 0)
112 return object->IsRootedAlive();
115 uint32_t ObjectTracker::GetRefCount(
Traceable*
object)
const {
116 std::unique_lock<Mutex> lock(mutex_);
117 DCHECK_EQ(1u, objects_.count(
object));
118 return objects_.at(
object);
122 std::unique_lock<Mutex> lock(mutex_);
123 while (!objects_.empty()) {
124 std::unordered_set<Traceable*> to_delete;
125 for (
auto& pair : objects_)
126 to_delete.insert(pair.first);
127 to_delete_ = to_delete;
129 DestroyObjects(to_delete, &lock);
133 void ObjectTracker::DestroyObjects(
134 const std::unordered_set<Traceable*>& to_delete,
135 std::unique_lock<Mutex>* lock) {
136 DCHECK(lock->owns_lock());
143 VLOG(1) <<
"Deleted " << to_delete.size() <<
" object(s).";
147 for (
auto it = objects_.begin(); it != objects_.end();) {
148 if (to_delete_.count(it->first) > 0) {
149 last_alive_time_.erase(it->first);
150 it = objects_.erase(it);
static constexpr const uint64_t kShortLiveDurationMs
void AddRef(const Traceable *object)
std::unordered_set< const Traceable * > GetAliveObjects() const
void RegisterObject(Traceable *object)
void ForceAlive(const Traceable *ptr)
static const Clock Instance
virtual uint64_t GetMonotonicTime() const
void FreeDeadObjects(const std::unordered_set< const Traceable *> &alive)
virtual bool IsShortLived() const
ObjectTracker(HeapTracer *tracer)
void RemoveRef(const Traceable *object)
void ForceAlive(const Traceable *ptr)