Index: third_party/tcmalloc/chromium/src/call_stack_table.cc |
diff --git a/third_party/tcmalloc/chromium/src/call_stack_table.cc b/third_party/tcmalloc/chromium/src/call_stack_table.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3677ba322b4702923f229acdd33a2e5624f8bdbe |
--- /dev/null |
+++ b/third_party/tcmalloc/chromium/src/call_stack_table.cc |
@@ -0,0 +1,145 @@ |
+#include "call_stack_table.h" |
+ |
+#include <cstring> |
+ |
+#include "base/commandlineflags.h" |
+#include "heap-profile-stats.h" |
+ |
+// A call stack must be suspected this many times to be reported as a leak |
+// suspect. |
+DECLARE_int32(call_stack_size_suspicion_threshold); |
+ |
+namespace leak_detector { |
+ |
+namespace { |
+ |
+using Bucket = CallStackTable::Bucket; |
+ |
+// Get the top |kRankedListSize| entries. |
+const int kRankedListSize = 16; |
+ |
+// Initial number of hash table buckets. |
+const int kInitialHashTableSize = 1999; |
+ |
+// Used for printing call stacks in LeakAnalyzer. |
+class StringPrint : public LeakAnalyzer<const Bucket*>::StringPrint { |
+ public: |
+ // Gets the string representation of a value. |
+ virtual const char* ValueToString(const Bucket* const& ptr, bool spacing_on) { |
+ snprintf(buffer_, sizeof(buffer_), spacing_on ? "%16p" : "%p", ptr); |
+ return buffer_; |
+ } |
+ |
+ // Gets the word that describes the value type. |
+ virtual const char* ValueTypeName(bool is_plural) { |
+ return is_plural ? "buckets" : "bucket"; |
+ } |
+} string_print; |
+ |
+} // namespace |
+ |
+CallStackTable::CallStackTable(Allocator alloc, DeAllocator dealloc) |
+ : num_allocs_(0), |
+ num_frees_(0), |
+ alloc_(alloc), |
+ dealloc_(dealloc), |
+ entry_map_(kInitialHashTableSize), |
+ leak_analyzer_(kRankedListSize, FLAGS_call_stack_size_suspicion_threshold, |
+ alloc, dealloc, &string_print) { |
+} |
+ |
+CallStackTable::~CallStackTable() {} |
+ |
+void CallStackTable::Add(const Bucket* ptr) { |
+ auto iter = entry_map_.find(ptr); |
+ Entry* entry = NULL; |
+ if (iter == entry_map_.end()) { |
+ entry = &entry_map_[ptr]; |
+ } else { |
+ entry = &iter->second; |
+ } |
+ |
+ ++entry->num_allocs; |
+ ++num_allocs_; |
+} |
+ |
+void CallStackTable::Remove(const Bucket* ptr) { |
+ auto iter = entry_map_.find(ptr); |
+ if (iter == entry_map_.end()) |
+ return; |
+ Entry* entry = &iter->second; |
+ ++entry->num_frees; |
+ ++num_frees_; |
+ |
+ // If there are no net allocs, delete the entry. |
+ if (entry->num_allocs == entry->num_frees) |
+ entry_map_.erase(iter); |
+} |
+ |
+int CallStackTable::Dump(char* buffer, const int buffer_size) const { |
+ int size_left = buffer_size; |
+ |
+ if (entry_map_.empty()) |
+ return size_left; |
+ |
+ int attempted_size = |
+ snprintf(buffer, size_left, |
+ "Total number of allocations: %u\n" |
+ "Total number of frees: %u\n" |
+ "Net number of allocations: %u\n" |
+ "Total number of distinct stack traces: %u\n", |
+ num_allocs_, num_frees_, num_allocs_ - num_frees_, |
+ entry_map_.size()); |
+ size_left -= attempted_size; |
+ buffer += attempted_size; |
+ |
+ if (size_left > 0) { |
+ int attempted_size = leak_analyzer_.Dump(buffer, size_left); |
+ size_left -= attempted_size; |
+ buffer += attempted_size; |
+ } |
+ |
+ if (size_left > 0) |
+ return buffer_size - size_left; |
+ |
+ return buffer_size; |
+} |
+ |
+void CallStackTable::TestForLeaks() { |
+ // Add all entries to the ranked list. |
+ RankedList<const Bucket*> ranked_list(kRankedListSize, alloc_, dealloc_); |
+ |
+ for (const auto& entry_pair : entry_map_) { |
+ const Entry& entry = entry_pair.second; |
+ if (entry.num_allocs != entry.num_frees) |
+ ranked_list.Add(entry_pair.first, entry.num_allocs - entry.num_frees); |
+ } |
+ leak_analyzer_.AddSample(ranked_list); |
+} |
+ |
+// static |
+void CallStackTable::Alloc::Init(Allocator alloc, DeAllocator dealloc) { |
+ alloc_ = alloc; |
+ dealloc_ = dealloc; |
+} |
+ |
+// static |
+void* CallStackTable::Alloc::Allocate(size_t size) { |
+ if (alloc_) |
+ return alloc_(size); |
+ return NULL; |
+} |
+ |
+// static |
+void CallStackTable::Alloc::Free(void* ptr, size_t size) { |
+ if (free) |
gpike
2015/07/01 21:38:21
why free?
Simon Que
2015/07/10 00:09:06
Meant dealloc_.
|
+ dealloc_(ptr); |
+} |
+ |
+// static |
+CallStackTable::Allocator CallStackTable::Alloc::alloc_ = NULL; |
+// static |
+CallStackTable::DeAllocator CallStackTable::Alloc::dealloc_ = NULL; |
+ |
+ |
+} // namespace leak_detector |