tools/trace-malloc/adreader.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/trace-malloc/adreader.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,97 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include <stdlib.h>
     1.9 +
    1.10 +#define ADLOG_ENTRY_BLOCK_SIZE 4096
    1.11 +
    1.12 +class ADLog {
    1.13 +
    1.14 +public:
    1.15 +
    1.16 +    // Use typedef in case somebody wants to process 64-bit output on a
    1.17 +    // 32-bit machine someday.
    1.18 +    typedef const char* Pointer;
    1.19 +
    1.20 +    struct Entry {
    1.21 +        Pointer address;
    1.22 +        const char *type;
    1.23 +
    1.24 +        const char *data;         // The contents of the memory.
    1.25 +        size_t datasize;
    1.26 +
    1.27 +        const char *allocation_stack;
    1.28 +    };
    1.29 +
    1.30 +    ADLog();
    1.31 +    ~ADLog();
    1.32 +
    1.33 +    /*
    1.34 +     * Returns false on failure and true on success.
    1.35 +     */
    1.36 +    bool Read(const char *aFilename);
    1.37 +
    1.38 +private:
    1.39 +    // Link structure for a circularly linked list.
    1.40 +    struct EntryBlock;
    1.41 +    struct EntryBlockLink {
    1.42 +        EntryBlock *mPrev;
    1.43 +        EntryBlock *mNext;
    1.44 +    };
    1.45 +
    1.46 +    struct EntryBlock : public EntryBlockLink {
    1.47 +        Entry entries[ADLOG_ENTRY_BLOCK_SIZE];
    1.48 +    };
    1.49 +
    1.50 +    size_t mEntryCount;
    1.51 +    EntryBlockLink mEntries;
    1.52 +
    1.53 +public:
    1.54 +
    1.55 +    class const_iterator {
    1.56 +        private:
    1.57 +            // Only |ADLog| member functions can construct iterators.
    1.58 +            friend class ADLog;
    1.59 +            const_iterator(EntryBlock *aBlock, size_t aOffset);
    1.60 +
    1.61 +        public:
    1.62 +            const Entry* operator*() { return mCur; }
    1.63 +            const Entry* operator->() { return mCur; }
    1.64 +
    1.65 +            const_iterator& operator++();
    1.66 +            const_iterator& operator--();
    1.67 +
    1.68 +            bool operator==(const const_iterator& aOther) const {
    1.69 +                return mCur == aOther.mCur;
    1.70 +            }
    1.71 +
    1.72 +            bool operator!=(const const_iterator& aOther) const {
    1.73 +                return mCur != aOther.mCur;
    1.74 +            }
    1.75 +
    1.76 +        private:
    1.77 +            void SetBlock(EntryBlock *aBlock) {
    1.78 +                mBlock = aBlock;
    1.79 +                mBlockStart = aBlock->entries;
    1.80 +                mBlockEnd = aBlock->entries + ADLOG_ENTRY_BLOCK_SIZE;
    1.81 +            }
    1.82 +
    1.83 +            EntryBlock *mBlock;
    1.84 +            Entry *mCur, *mBlockStart, *mBlockEnd;
    1.85 +
    1.86 +            // Not to be implemented.
    1.87 +            const_iterator operator++(int);
    1.88 +            const_iterator operator--(int);
    1.89 +    };
    1.90 +
    1.91 +    const_iterator begin() {
    1.92 +        return const_iterator(mEntries.mNext, 0);
    1.93 +    }
    1.94 +    const_iterator end() {
    1.95 +        return const_iterator(mEntries.mPrev,
    1.96 +                              mEntryCount % ADLOG_ENTRY_BLOCK_SIZE);
    1.97 +    }
    1.98 +
    1.99 +    size_t count() { return mEntryCount; }
   1.100 +};

mercurial