tools/trace-malloc/adreader.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial