| Index: third_party/tcmalloc/chromium/src/leak_detector_impl.h
|
| diff --git a/third_party/tcmalloc/chromium/src/leak_detector_impl.h b/third_party/tcmalloc/chromium/src/leak_detector_impl.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a0387baa3a8fd4c281a3452950f25bb7afe6c7bd
|
| --- /dev/null
|
| +++ b/third_party/tcmalloc/chromium/src/leak_detector_impl.h
|
| @@ -0,0 +1,149 @@
|
| +// 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.
|
| +
|
| +// ---
|
| +// Author: Simon Que
|
| +
|
| +#ifndef _LEAK_DETECTOR_IMPL_H_
|
| +#define _LEAK_DETECTOR_IMPL_H_
|
| +
|
| +#include "addressmap-inl.h"
|
| +#include "base/basictypes.h"
|
| +#include "heap-profile-stats.h"
|
| +#include "leak_analyzer.h"
|
| +
|
| +namespace leak_detector {
|
| +
|
| +class CallStackTable;
|
| +
|
| +//----------------------------------------------------------------------
|
| +// Class that contains the actual leak detection mechanism.
|
| +//----------------------------------------------------------------------
|
| +class LeakDetectorImpl {
|
| + public:
|
| + // Memory (de)allocator interface we'll use.
|
| + typedef void* (*Allocator)(size_t size);
|
| + typedef void (*DeAllocator)(void* ptr);
|
| +
|
| + // Used for tracking allocation stats.
|
| + using Stats = HeapProfileStats;
|
| +
|
| + LeakDetectorImpl(Allocator alloc, DeAllocator dealloc, uint64 mapping_addr,
|
| + uint64 mapping_size);
|
| + ~LeakDetectorImpl();
|
| +
|
| + // Indicates whether the given allocation size has an associated call stack
|
| + // table, and thus requires a stack unwind.
|
| + bool ShouldGetStackTraceForSize(size_t size) const;
|
| +
|
| + // Record allocs and frees.
|
| + void RecordAlloc(const void* ptr, size_t size,
|
| + int stack_depth, const void* const call_stack[]);
|
| + void RecordFree(const void* ptr);
|
| +
|
| + const Stats& stats() const {
|
| + return stats_;
|
| + }
|
| +
|
| + // Run check for possible leaks based on the current profiling data.
|
| + void TestForLeaks();
|
| +
|
| + // Dump current profiling statistics to log.
|
| + void DumpStats() const;
|
| +
|
| + private:
|
| + // Used for tracking unique call stacks.
|
| + using Bucket = HeapProfileBucket;
|
| +
|
| + // A record of allocations for a particular size.
|
| + struct AllocSizeEntry {
|
| + // Number of allocations and frees for this size.
|
| + uint32 num_allocs;
|
| + uint32 num_frees;
|
| +
|
| + // A stack table, if this size is being profiled for stack as well.
|
| + CallStackTable* stack_table;
|
| + };
|
| +
|
| + // Info stored in the address map
|
| + struct AllocInfo {
|
| + AllocInfo() : bucket(NULL) {}
|
| +
|
| + // Number of bytes in this allocation.
|
| + size_t bytes;
|
| +
|
| + // Points to a hash table bucket for a unique call stack.
|
| + Bucket* bucket;
|
| + };
|
| +
|
| + // Used for recording size and call stack info for each allocation.
|
| + using AllocationMap = AddressMap<AllocInfo>;
|
| +
|
| + // Number of entries in the alloc size table. As sizes are aligned to 32-bits
|
| + // the max supported allocation size is (kNumSizeEntries * 4 - 1). Any larger
|
| + // sizes are ignored. This value is chosen high enough that such large sizes
|
| + // are rare if not nonexistent.
|
| + static const int kNumSizeEntries = 2048;
|
| +
|
| + // The number of entries in the hash table for storing call stack buckets.
|
| + // This does not represent the maximum number of unique call stacks that can
|
| + // be supported. If there is a collision, multiple call stacks can be stored
|
| + // in the same slot as a linked list.
|
| + static const int kHashTableSize = 9973;
|
| +
|
| + // Converts an allocation size to/from the array index used for |entries_|.
|
| + static int SizeToIndex(size_t size);
|
| + static size_t IndexToSize(int index);
|
| +
|
| + // Accessor for the entry table.
|
| + inline AllocSizeEntry* GetEntryForSize(size_t size) {
|
| + return &entries_[SizeToIndex(size)];
|
| + }
|
| + inline const AllocSizeEntry& GetConstEntryForSize(size_t size) const {
|
| + return entries_[SizeToIndex(size)];
|
| + }
|
| +
|
| + // Returns a hash table bucket for a call stack. Each unique call stack has a
|
| + // unique bucket. If the given call stack bucket has already been created by a
|
| + // previous call to GetBucket(), return a pointer to that same call stack
|
| + // bucket.
|
| + Bucket* GetBucket(int depth, const void* const key[]);
|
| +
|
| + // Returns the offset of |ptr| within the current binary. If it is not in the
|
| + // current binary, just return |ptr| as an integer.
|
| + uint64 GetOffset(const void *ptr) const;
|
| +
|
| + // Functions for local allocations.
|
| + Allocator alloc_;
|
| + DeAllocator dealloc_;
|
| +
|
| + // Bucket hash table for tracking unique call stacks.
|
| + Bucket* bucket_table_[kHashTableSize];
|
| + // Counter for the number of unique call stacks.
|
| + int num_buckets_;
|
| +
|
| + // For tracking allocation stats.
|
| + Stats stats_;
|
| + Stats call_stack_stats_;
|
| + int num_stack_tables_;
|
| +
|
| + // Stores all individual recorded allocations.
|
| + AllocationMap address_map_;
|
| +
|
| + // Used to analyze potential leak patterns in the allocation sizes.
|
| + LeakAnalyzer<uint32> size_leak_analyzer_;
|
| +
|
| + // Allocation stats for each size.
|
| + AllocSizeEntry entries_[kNumSizeEntries];
|
| +
|
| + // Address mapping info of the current binary.
|
| + uint64 mapping_addr_;
|
| + uint64 mapping_size_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(LeakDetectorImpl);
|
| +};
|
| +
|
| +} // namespace leak_detector
|
| +
|
| +#endif // _LEAK_DETECTOR_IMPL_H_
|
|
|