Shaka Player Embedded
object_tracker.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_OBJECT_TRACKER_H_
16 #define SHAKA_EMBEDDED_MEMORY_OBJECT_TRACKER_H_
17 
18 #include <glog/logging.h>
19 
20 #include <memory>
21 #include <unordered_map>
22 #include <unordered_set>
23 #include <vector>
24 
25 #include "src/debug/mutex.h"
26 #include "src/util/macros.h"
28 #include "src/util/templates.h"
29 
30 namespace shaka {
31 
32 class JsManagerImpl;
33 class RefPtrTest;
34 
35 namespace eme {
36 class ClearKeyImplementationTest;
37 } // namespace eme
38 
39 namespace memory {
40 
41 class HeapTracer;
42 class Traceable;
43 
51 class ObjectTracker final : public PseudoSingleton<ObjectTracker> {
52  public:
53  explicit ObjectTracker(HeapTracer* tracer);
54  ~ObjectTracker();
55 
57 
59  void RegisterObject(Traceable* object);
60 
62  void ForceAlive(const Traceable* ptr);
63 
65  void AddRef(const Traceable* object);
66 
68  void RemoveRef(const Traceable* object);
69 
71  std::unordered_set<const Traceable*> GetAliveObjects() const;
72 
77  void FreeDeadObjects(const std::unordered_set<const Traceable*>& alive);
78 
80  void Dispose();
81 
82  private:
83  friend RefPtrTest;
84  friend class ObjectTrackerTest;
85 
87  bool IsJsAlive(Traceable* object) const;
88 
90  uint32_t GetRefCount(Traceable* object) const;
91 
92  void DestroyObjects(const std::unordered_set<Traceable*>& to_delete,
93  std::unique_lock<Mutex>* lock);
94 
95  mutable Mutex mutex_;
96  HeapTracer* tracer_;
97  // A map of object pointer to ref count.
98  std::unordered_map<Traceable*, uint32_t> objects_;
99  std::unordered_map<Traceable*, uint64_t> last_alive_time_;
100  std::unordered_set<Traceable*> to_delete_;
101 };
102 
103 } // namespace memory
104 } // namespace shaka
105 
106 #endif // SHAKA_EMBEDDED_MEMORY_OBJECT_TRACKER_H_
#define SHAKA_NON_COPYABLE_OR_MOVABLE_TYPE(Type)
Definition: macros.h:51