| 53 | } |
| 54 | |
| 55 | StackTraceTable::StackTraceTable() |
| 56 | : error_(false), |
| 57 | depth_total_(0), |
| 58 | bucket_total_(0), |
| 59 | table_(new Bucket*[kHashTableSize]()) { |
| 60 | memset(table_, 0, kHashTableSize * sizeof(Bucket*)); |
| 61 | } |
| 62 | |
| 63 | StackTraceTable::~StackTraceTable() { |
| 64 | delete[] table_; |
| 65 | } |
| 66 | |
| 67 | void StackTraceTable::AddTrace(const StackTrace& t) { |
| 68 | if (error_) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | // Hash function borrowed from base/heap-profile-table.cc |
| 73 | uintptr_t h = 0; |
| 74 | for (int i = 0; i < t.depth; ++i) { |
| 75 | h += reinterpret_cast<uintptr_t>(t.stack[i]); |
| 76 | h += h << 10; |
| 77 | h ^= h >> 6; |
| 78 | } |
| 79 | h += h << 3; |
| 80 | h ^= h >> 11; |
| 81 | |
| 82 | const int idx = h % kHashTableSize; |
| 83 | |
| 84 | Bucket* b = table_[idx]; |
| 85 | while (b != NULL && !b->KeyEqual(h, t)) { |
| 86 | b = b->next; |
| 87 | } |
| 88 | if (b != NULL) { |
| 89 | b->count++; |
| 90 | b->trace.size += t.size; // keep cumulative size |
| 91 | } else { |
| 92 | depth_total_ += t.depth; |
| 93 | bucket_total_++; |
| 94 | b = Static::bucket_allocator()->New(); |
| 95 | if (b == NULL) { |
| 96 | Log(kLog, __FILE__, __LINE__, |
| 97 | "tcmalloc: could not allocate bucket", sizeof(*b)); |
| 98 | error_ = true; |
| 99 | } else { |
| 100 | b->hash = h; |
| 101 | b->trace = t; |
| 102 | b->count = 1; |
| 103 | b->next = table_[idx]; |
| 104 | table_[idx] = b; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void** StackTraceTable::ReadStackTracesAndClear() { |
| 110 | if (error_) { |
| 111 | return NULL; |
| 112 | } |