Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Unified Diff: third_party/tcmalloc/chromium/src/call_stack_table.h

Issue 986503002: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove Author label from new files; Check type in LeakDetectorValueType comparators; Add missing fi… Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/tcmalloc/chromium/src/call_stack_table.h
diff --git a/third_party/tcmalloc/chromium/src/call_stack_table.h b/third_party/tcmalloc/chromium/src/call_stack_table.h
new file mode 100644
index 0000000000000000000000000000000000000000..14fcee60ec45c3cd5352bb3a624154f073cc5503
--- /dev/null
+++ b/third_party/tcmalloc/chromium/src/call_stack_table.h
@@ -0,0 +1,86 @@
+// Copyright (c) 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef _CALL_STACK_TABLE_H_
+#define _CALL_STACK_TABLE_H_
+
+#include <unordered_map>
+#include <functional>
+
+#include "base/basictypes.h"
+#include "base/custom_allocator.h"
+#include "base/stl_allocator.h"
+#include "heap-profile-stats.h"
+#include "leak_analyzer.h"
+
+namespace leak_detector {
+
+// Contains a hash table where the key is the call stack and the value is the
+// number of allocations from that call stack.
+class CallStackTable {
+ public:
+ // Represents a unique call stack.
+ using CallStack = HeapProfileBucket;
+
+ struct Hash {
+ long operator() (const CallStack* call_stack) const {
+ // The call stack object should already have a hash computed when it was
+ // created.
+ return call_stack->hash;
+ }
+ };
+
+ CallStackTable();
+ ~CallStackTable();
+
+ // Add/Remove an allocation for the given call stack.
+ void Add(const CallStack* call_stack);
+ void Remove(const CallStack* call_stack);
+
+ // Dump contents to log buffer |buffer| of size |size|.
+ int Dump(char* buffer, int size) const;
+
+ // Check for leak patterns in the allocation data.
+ void TestForLeaks();
+
+ const LeakAnalyzer& leak_analyzer() const {
+ return leak_analyzer_;
+ }
+
+ private:
+ // Hash table entry used to track number of allocs and frees for a call stack.
+ // Do not use the fields of struct HeapProfileBucket for this purpose; one
+ // call stack object can have different entries in multiple CallStackTables.
+ struct Entry {
+ // Number of allocs/frees for the call stack.
+ uint32 num_allocs;
+ uint32 num_frees;
+ };
+
+ // The total number of recorded allocations when this object was created. This
+ // acts as a timestamp of sorts.
+ uint64_t total_num_allocs_at_creation_;
+
+ // Total number of allocs and frees in this table.
+ uint32 num_allocs_;
+ uint32 num_frees_;
+
+ // Hash table containing entries.
+ using TableEntryAllocator =
+ STL_Allocator<std::pair<const CallStack*, Entry>, CustomAllocator>;
+ std::unordered_map<const CallStack*,
+ Entry,
+ Hash,
+ std::equal_to<const CallStack*>,
+ TableEntryAllocator> entry_map_;
+
+ // For detecting leak patterns in incoming allocations.
+ LeakAnalyzer leak_analyzer_;
+
+ DISALLOW_COPY_AND_ASSIGN(CallStackTable);
+};
+
+} // namespace leak_detector
+
+#endif // _CALL_STACK_TABLE_H_

Powered by Google App Engine
This is Rietveld 408576698