|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include <stdlib.h> |
|
6 |
|
7 #define ADLOG_ENTRY_BLOCK_SIZE 4096 |
|
8 |
|
9 class ADLog { |
|
10 |
|
11 public: |
|
12 |
|
13 // Use typedef in case somebody wants to process 64-bit output on a |
|
14 // 32-bit machine someday. |
|
15 typedef const char* Pointer; |
|
16 |
|
17 struct Entry { |
|
18 Pointer address; |
|
19 const char *type; |
|
20 |
|
21 const char *data; // The contents of the memory. |
|
22 size_t datasize; |
|
23 |
|
24 const char *allocation_stack; |
|
25 }; |
|
26 |
|
27 ADLog(); |
|
28 ~ADLog(); |
|
29 |
|
30 /* |
|
31 * Returns false on failure and true on success. |
|
32 */ |
|
33 bool Read(const char *aFilename); |
|
34 |
|
35 private: |
|
36 // Link structure for a circularly linked list. |
|
37 struct EntryBlock; |
|
38 struct EntryBlockLink { |
|
39 EntryBlock *mPrev; |
|
40 EntryBlock *mNext; |
|
41 }; |
|
42 |
|
43 struct EntryBlock : public EntryBlockLink { |
|
44 Entry entries[ADLOG_ENTRY_BLOCK_SIZE]; |
|
45 }; |
|
46 |
|
47 size_t mEntryCount; |
|
48 EntryBlockLink mEntries; |
|
49 |
|
50 public: |
|
51 |
|
52 class const_iterator { |
|
53 private: |
|
54 // Only |ADLog| member functions can construct iterators. |
|
55 friend class ADLog; |
|
56 const_iterator(EntryBlock *aBlock, size_t aOffset); |
|
57 |
|
58 public: |
|
59 const Entry* operator*() { return mCur; } |
|
60 const Entry* operator->() { return mCur; } |
|
61 |
|
62 const_iterator& operator++(); |
|
63 const_iterator& operator--(); |
|
64 |
|
65 bool operator==(const const_iterator& aOther) const { |
|
66 return mCur == aOther.mCur; |
|
67 } |
|
68 |
|
69 bool operator!=(const const_iterator& aOther) const { |
|
70 return mCur != aOther.mCur; |
|
71 } |
|
72 |
|
73 private: |
|
74 void SetBlock(EntryBlock *aBlock) { |
|
75 mBlock = aBlock; |
|
76 mBlockStart = aBlock->entries; |
|
77 mBlockEnd = aBlock->entries + ADLOG_ENTRY_BLOCK_SIZE; |
|
78 } |
|
79 |
|
80 EntryBlock *mBlock; |
|
81 Entry *mCur, *mBlockStart, *mBlockEnd; |
|
82 |
|
83 // Not to be implemented. |
|
84 const_iterator operator++(int); |
|
85 const_iterator operator--(int); |
|
86 }; |
|
87 |
|
88 const_iterator begin() { |
|
89 return const_iterator(mEntries.mNext, 0); |
|
90 } |
|
91 const_iterator end() { |
|
92 return const_iterator(mEntries.mPrev, |
|
93 mEntryCount % ADLOG_ENTRY_BLOCK_SIZE); |
|
94 } |
|
95 |
|
96 size_t count() { return mEntryCount; } |
|
97 }; |