tools/trace-malloc/adreader.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     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/. */
     5 #include <stdlib.h>
     7 #define ADLOG_ENTRY_BLOCK_SIZE 4096
     9 class ADLog {
    11 public:
    13     // Use typedef in case somebody wants to process 64-bit output on a
    14     // 32-bit machine someday.
    15     typedef const char* Pointer;
    17     struct Entry {
    18         Pointer address;
    19         const char *type;
    21         const char *data;         // The contents of the memory.
    22         size_t datasize;
    24         const char *allocation_stack;
    25     };
    27     ADLog();
    28     ~ADLog();
    30     /*
    31      * Returns false on failure and true on success.
    32      */
    33     bool Read(const char *aFilename);
    35 private:
    36     // Link structure for a circularly linked list.
    37     struct EntryBlock;
    38     struct EntryBlockLink {
    39         EntryBlock *mPrev;
    40         EntryBlock *mNext;
    41     };
    43     struct EntryBlock : public EntryBlockLink {
    44         Entry entries[ADLOG_ENTRY_BLOCK_SIZE];
    45     };
    47     size_t mEntryCount;
    48     EntryBlockLink mEntries;
    50 public:
    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);
    58         public:
    59             const Entry* operator*() { return mCur; }
    60             const Entry* operator->() { return mCur; }
    62             const_iterator& operator++();
    63             const_iterator& operator--();
    65             bool operator==(const const_iterator& aOther) const {
    66                 return mCur == aOther.mCur;
    67             }
    69             bool operator!=(const const_iterator& aOther) const {
    70                 return mCur != aOther.mCur;
    71             }
    73         private:
    74             void SetBlock(EntryBlock *aBlock) {
    75                 mBlock = aBlock;
    76                 mBlockStart = aBlock->entries;
    77                 mBlockEnd = aBlock->entries + ADLOG_ENTRY_BLOCK_SIZE;
    78             }
    80             EntryBlock *mBlock;
    81             Entry *mCur, *mBlockStart, *mBlockEnd;
    83             // Not to be implemented.
    84             const_iterator operator++(int);
    85             const_iterator operator--(int);
    86     };
    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     }
    96     size_t count() { return mEntryCount; }
    97 };

mercurial