brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_PROFILING_BACKTRACE_STORAGE_H_ |
| 6 | #define CHROME_PROFILING_BACKTRACE_STORAGE_H_ |
| 7 | |
| 8 | #include <unordered_set> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/macros.h" |
| 12 | #include "base/memory/ref_counted.h" |
| 13 | #include "base/synchronization/lock.h" |
| 14 | #include "chrome/profiling/backtrace.h" |
| 15 | |
| 16 | namespace profiling { |
| 17 | |
| 18 | // This class is threadsafe. |
| 19 | class BacktraceStorage { |
| 20 | public: |
| 21 | using Container = std::unordered_set<Backtrace>; |
| 22 | using Key = Container::iterator; |
| 23 | |
| 24 | BacktraceStorage(); |
| 25 | ~BacktraceStorage(); |
| 26 | |
| 27 | // Adds the given backtrace to the storage and returns a key to it. If a |
| 28 | // matching backtrace already exists, a key to the existing one will be |
| 29 | // returned. |
| 30 | // |
| 31 | // The returned key will have a reference count associated with it, call |
| 32 | // Free when the key is no longer needed. |
| 33 | Key Insert(std::vector<Address>&& bt); |
| 34 | |
| 35 | // Frees one reference to a backtrace. |
| 36 | void Free(const Key& key); |
| 37 | void Free(const std::vector<Key>& keys); |
| 38 | |
| 39 | // Returns the backtrace associated with the given key. Assumes the caller |
| 40 | // holds a key to it that will keep the backtrace in scope. |
| 41 | const Backtrace& GetBacktraceForKey(const Key& key) const; |
| 42 | |
| 43 | private: |
| 44 | mutable base::Lock lock_; |
| 45 | |
| 46 | // List of live backtraces for de-duping. Protected by the lock_. |
| 47 | Container backtraces_; |
| 48 | }; |
| 49 | |
| 50 | } // namespace profiling |
| 51 | |
| 52 | #endif // CHROME_PROFILING_BACKTRACE_STORAGE_H_ |