Shaka Player Embedded
heap_tracer.cc
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/memory/heap_tracer.h"
16 
17 #include <utility>
18 
22 #include "src/util/utils.h"
23 
24 namespace shaka {
25 namespace memory {
26 
28  return false;
29 }
31  return false;
32 }
33 
34 
35 HeapTracer::HeapTracer() : mutex_("HeapTracer") {}
37 
39  std::unique_lock<Mutex> lock(mutex_);
40  pending_.insert(ptr);
41 }
42 
43 void HeapTracer::Trace(const Traceable* ptr) {
44  std::unique_lock<Mutex> lock(mutex_);
45  pending_.insert(ptr);
46 }
47 
49  ResetState();
50 }
51 
53  const std::unordered_set<const Traceable*>& ref_alive) {
54  {
55  std::unique_lock<Mutex> lock(mutex_);
56  pending_.insert(ref_alive.begin(), ref_alive.end());
57  }
58 
59  while (true) {
60  std::unordered_set<const Traceable*> to_trace;
61 
62  {
63  std::unique_lock<Mutex> lock(mutex_);
64  to_trace = std::move(pending_);
65 
66  // We need to be careful about circular dependencies. Only traverse if we
67  // have not seen it before.
68  for (auto it = to_trace.begin(); it != to_trace.end();) {
69  if (*it && alive_.count(*it) == 0) {
70  alive_.insert(*it);
71  ++it;
72  } else {
73  it = to_trace.erase(it);
74  }
75  }
76  }
77 
78  if (to_trace.empty())
79  break;
80  for (const Traceable* ptr : to_trace) {
81  ptr->Trace(this);
82  }
83  }
84 }
85 
87  std::unique_lock<Mutex> lock(mutex_);
88 
89  alive_.clear();
90  pending_.clear();
91 }
92 
93 } // namespace memory
94 } // namespace shaka
void Trace(const Traceable *ptr)
Definition: heap_tracer.cc:43
void ForceAlive(const Traceable *ptr)
Definition: heap_tracer.cc:38
virtual bool IsRootedAlive() const
Definition: heap_tracer.cc:27
virtual bool IsShortLived() const
Definition: heap_tracer.cc:30
void TraceAll(const std::unordered_set< const Traceable *> &ref_alive)
Definition: heap_tracer.cc:52