Tue, 06 Jan 2015 21:39:09 +0100
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.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef SHARED_LIBRARIES_H_ |
michael@0 | 8 | #define SHARED_LIBRARIES_H_ |
michael@0 | 9 | |
michael@0 | 10 | #ifndef MOZ_ENABLE_PROFILER_SPS |
michael@0 | 11 | #error This header does not have a useful implementation on your platform! |
michael@0 | 12 | #endif |
michael@0 | 13 | |
michael@0 | 14 | #include <algorithm> |
michael@0 | 15 | #include <vector> |
michael@0 | 16 | #include <string> |
michael@0 | 17 | #include <stdlib.h> |
michael@0 | 18 | #include <stdint.h> |
michael@0 | 19 | #include <nsID.h> |
michael@0 | 20 | |
michael@0 | 21 | class SharedLibrary { |
michael@0 | 22 | public: |
michael@0 | 23 | |
michael@0 | 24 | SharedLibrary(uintptr_t aStart, |
michael@0 | 25 | uintptr_t aEnd, |
michael@0 | 26 | uintptr_t aOffset, |
michael@0 | 27 | const std::string& aBreakpadId, |
michael@0 | 28 | const std::string& aName) |
michael@0 | 29 | : mStart(aStart) |
michael@0 | 30 | , mEnd(aEnd) |
michael@0 | 31 | , mOffset(aOffset) |
michael@0 | 32 | , mBreakpadId(aBreakpadId) |
michael@0 | 33 | , mName(aName) |
michael@0 | 34 | {} |
michael@0 | 35 | |
michael@0 | 36 | SharedLibrary(const SharedLibrary& aEntry) |
michael@0 | 37 | : mStart(aEntry.mStart) |
michael@0 | 38 | , mEnd(aEntry.mEnd) |
michael@0 | 39 | , mOffset(aEntry.mOffset) |
michael@0 | 40 | , mBreakpadId(aEntry.mBreakpadId) |
michael@0 | 41 | , mName(aEntry.mName) |
michael@0 | 42 | {} |
michael@0 | 43 | |
michael@0 | 44 | SharedLibrary& operator=(const SharedLibrary& aEntry) |
michael@0 | 45 | { |
michael@0 | 46 | // Gracefully handle self assignment |
michael@0 | 47 | if (this == &aEntry) return *this; |
michael@0 | 48 | |
michael@0 | 49 | mStart = aEntry.mStart; |
michael@0 | 50 | mEnd = aEntry.mEnd; |
michael@0 | 51 | mOffset = aEntry.mOffset; |
michael@0 | 52 | mBreakpadId = aEntry.mBreakpadId; |
michael@0 | 53 | mName = aEntry.mName; |
michael@0 | 54 | return *this; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | bool operator==(const SharedLibrary& other) const |
michael@0 | 58 | { |
michael@0 | 59 | return (mStart == other.mStart) && |
michael@0 | 60 | (mEnd == other.mEnd) && |
michael@0 | 61 | (mOffset == other.mOffset) && |
michael@0 | 62 | (mName == other.mName) && |
michael@0 | 63 | (mBreakpadId == other.mBreakpadId); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | uintptr_t GetStart() const { return mStart; } |
michael@0 | 67 | uintptr_t GetEnd() const { return mEnd; } |
michael@0 | 68 | uintptr_t GetOffset() const { return mOffset; } |
michael@0 | 69 | const std::string &GetBreakpadId() const { return mBreakpadId; } |
michael@0 | 70 | const std::string &GetName() const { return mName; } |
michael@0 | 71 | |
michael@0 | 72 | private: |
michael@0 | 73 | SharedLibrary() {} |
michael@0 | 74 | |
michael@0 | 75 | uintptr_t mStart; |
michael@0 | 76 | uintptr_t mEnd; |
michael@0 | 77 | uintptr_t mOffset; |
michael@0 | 78 | std::string mBreakpadId; |
michael@0 | 79 | std::string mName; |
michael@0 | 80 | }; |
michael@0 | 81 | |
michael@0 | 82 | static bool |
michael@0 | 83 | CompareAddresses(const SharedLibrary& first, const SharedLibrary& second) |
michael@0 | 84 | { |
michael@0 | 85 | return first.GetStart() < second.GetStart(); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | class SharedLibraryInfo { |
michael@0 | 89 | public: |
michael@0 | 90 | static SharedLibraryInfo GetInfoForSelf(); |
michael@0 | 91 | SharedLibraryInfo() {} |
michael@0 | 92 | |
michael@0 | 93 | void AddSharedLibrary(SharedLibrary entry) |
michael@0 | 94 | { |
michael@0 | 95 | mEntries.push_back(entry); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | const SharedLibrary& GetEntry(size_t i) const |
michael@0 | 99 | { |
michael@0 | 100 | return mEntries[i]; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | // Removes items in the range [first, last) |
michael@0 | 104 | // i.e. element at the "last" index is not removed |
michael@0 | 105 | void RemoveEntries(size_t first, size_t last) |
michael@0 | 106 | { |
michael@0 | 107 | mEntries.erase(mEntries.begin() + first, mEntries.begin() + last); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | bool Contains(const SharedLibrary& searchItem) const |
michael@0 | 111 | { |
michael@0 | 112 | return (mEntries.end() != |
michael@0 | 113 | std::find(mEntries.begin(), mEntries.end(), searchItem)); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | size_t GetSize() const |
michael@0 | 117 | { |
michael@0 | 118 | return mEntries.size(); |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | void SortByAddress() |
michael@0 | 122 | { |
michael@0 | 123 | std::sort(mEntries.begin(), mEntries.end(), CompareAddresses); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | void Clear() |
michael@0 | 127 | { |
michael@0 | 128 | mEntries.clear(); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | private: |
michael@0 | 132 | std::vector<SharedLibrary> mEntries; |
michael@0 | 133 | }; |
michael@0 | 134 | |
michael@0 | 135 | #endif |