1// Copyright (c) 2013 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// This file defines structs to accumulate memory allocation and deallocation
6// counts.  These structs are commonly used for malloc (in HeapProfileTable)
7// and mmap (in MemoryRegionMap).
8
9// A bucket is data structure for heap profiling to store a pair of a stack
10// trace and counts of (de)allocation.  Buckets are stored in a hash table
11// which is declared as "HeapProfileBucket**".
12//
13// A hash value is computed from a stack trace.  Collision in the hash table
14// is resolved by separate chaining with linked lists.  The links in the list
15// are implemented with the member "HeapProfileBucket* next".
16//
17// A structure of a hash table HeapProfileBucket** bucket_table would be like:
18// bucket_table[0] => NULL
19// bucket_table[1] => HeapProfileBucket() => HeapProfileBucket() => NULL
20// ...
21// bucket_table[i] => HeapProfileBucket() => NULL
22// ...
23// bucket_table[n] => HeapProfileBucket() => NULL
24
25#ifndef HEAP_PROFILE_STATS_H_
26#define HEAP_PROFILE_STATS_H_
27
28struct HeapProfileStats {
29  // Returns true if the two HeapProfileStats are semantically equal.
30  bool Equivalent(const HeapProfileStats& other) const {
31    return allocs - frees == other.allocs - other.frees &&
32        alloc_size - free_size == other.alloc_size - other.free_size;
33  }
34
35  int32 allocs;      // Number of allocation calls.
36  int32 frees;       // Number of free calls.
37  int64 alloc_size;  // Total size of all allocated objects so far.
38  int64 free_size;   // Total size of all freed objects so far.
39};
40
41// Allocation and deallocation statistics per each stack trace.
42struct HeapProfileBucket : public HeapProfileStats {
43  // Longest stack trace we record.
44  static const int kMaxStackDepth = 32;
45
46  uintptr_t hash;           // Hash value of the stack trace.
47  int depth;                // Depth of stack trace.
48  const void** stack;       // Stack trace.
49  HeapProfileBucket* next;  // Next entry in hash-table.
50};
51
52#endif  // HEAP_PROFILE_STATS_H_
53