Shaka Player Embedded
heap_tracer.h
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 #ifndef SHAKA_EMBEDDED_MEMORY_HEAP_TRACER_H_
16 #define SHAKA_EMBEDDED_MEMORY_HEAP_TRACER_H_
17 
18 #include <glog/logging.h>
19 
20 #include <list>
21 #include <string>
22 #include <type_traits>
23 #include <unordered_map>
24 #include <unordered_set>
25 #include <vector>
26 
27 #include "shaka/optional.h"
28 #include "shaka/variant.h"
29 #include "src/debug/mutex.h"
30 #include "src/util/templates.h"
31 
32 namespace shaka {
33 class BackingObject;
34 
35 namespace memory {
36 class HeapTracer;
37 
43 class Traceable {
44  public:
49  static constexpr const uint64_t kShortLiveDurationMs = 5000;
50 
51 
52  virtual ~Traceable() {}
53 
58  virtual void Trace(HeapTracer* tracer) const = 0;
59 
64  virtual bool IsRootedAlive() const;
65 
77  virtual bool IsShortLived() const;
78 };
79 
80 
85 class HeapTracer {
86  public:
87  HeapTracer();
88  ~HeapTracer();
89 
91  const std::unordered_set<const Traceable*>& alive() const {
92  return alive_;
93  }
94 
100  void ForceAlive(const Traceable* ptr);
101 
106  void Trace(const Traceable* ptr);
107 
108  template <typename T>
109  void Trace(const std::vector<T>* array) {
110  for (const T& item : *array) {
111  Trace(&item);
112  }
113  }
114  template <typename T>
115  void Trace(const std::list<T>* array) {
116  for (const T& item : *array) {
117  Trace(&item);
118  }
119  }
120  template <typename Key, typename Value>
121  void Trace(const std::unordered_map<Key, Value>* map) {
122  for (const auto& pair : *map) {
123  Trace(&pair.first);
124  Trace(&pair.second);
125  }
126  }
127  template <typename T>
128  void Trace(const optional<T>* opt) {
129  if (opt->has_value())
130  Trace(&opt->value());
131  }
132  template <typename... Types>
134  VariantHelper<0, Types...>::Trace(this, variant);
135  }
136 
137  // Allow passing other types, just ignore it. This simplifies generic
138  // converter templates so they don't have to check if the type is special
139  // or not.
140  void Trace(const std::string*) {}
141  void Trace(const bool*) {}
142  template <typename T,
144  std::is_enum<T>::value>>
145  void Trace(const T*) {}
146 
148  void BeginPass();
149 
154  void TraceAll(const std::unordered_set<const Traceable*>& ref_alive);
155 
157  void ResetState();
158 
159  private:
160  template <size_t I, typename... Types>
161  struct VariantHelper {
162  static void Trace(HeapTracer* tracer, const variant<Types...>* variant) {
163  if (variant->index() == I)
164  tracer->Trace(&get<I>(*variant));
165  else
166  VariantHelper<I + 1, Types...>::Trace(tracer, variant);
167  }
168  };
169  template <typename... Types>
170  struct VariantHelper<sizeof...(Types), Types...> {
171  static void Trace(HeapTracer* tracer, const variant<Types...>* variant) {
172  CHECK_EQ(variant->index(), sizeof...(Types) - 1);
173  tracer->Trace(&get<sizeof...(Types) - 1>(*variant));
174  }
175  };
176 
177  Mutex mutex_;
178  std::unordered_set<const Traceable*> alive_;
179  std::unordered_set<const Traceable*> pending_;
180 };
181 
182 } // namespace memory
183 } // namespace shaka
184 
185 #endif // SHAKA_EMBEDDED_MEMORY_HEAP_TRACER_H_
void Trace(const optional< T > *opt)
Definition: heap_tracer.h:128
void Trace(const std::string *)
Definition: heap_tracer.h:140
void Trace(const std::vector< T > *array)
Definition: heap_tracer.h:109
void Trace(const std::unordered_map< Key, Value > *map)
Definition: heap_tracer.h:121
const std::unordered_set< const Traceable * > & alive() const
Definition: heap_tracer.h:91
void Trace(const T *)
Definition: heap_tracer.h:145
const T & value() const &
Definition: optional.h:147
static constexpr const uint64_t kShortLiveDurationMs
Definition: heap_tracer.h:49
typename std::enable_if< B, T >::type enable_if_t
Definition: templates.h:56
void Trace(const Traceable *ptr)
Definition: heap_tracer.cc:43
void Trace(const bool *)
Definition: heap_tracer.h:141
void Trace(const variant< Types... > *variant)
Definition: heap_tracer.h:133
void Trace(const std::list< T > *array)
Definition: heap_tracer.h:115
size_t index() const
Definition: variant.h:340
virtual bool IsRootedAlive() const
Definition: heap_tracer.cc:27
virtual void Trace(HeapTracer *tracer) const =0
virtual bool IsShortLived() const
Definition: heap_tracer.cc:30
bool has_value() const
Definition: optional.h:143