mozglue/linker/Mappable.h

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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 file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #ifndef Mappable_h
michael@0 6 #define Mappable_h
michael@0 7
michael@0 8 #include <sys/types.h>
michael@0 9 #include <pthread.h>
michael@0 10 #include "Zip.h"
michael@0 11 #include "SeekableZStream.h"
michael@0 12 #include "mozilla/RefPtr.h"
michael@0 13 #include "mozilla/Scoped.h"
michael@0 14 #include "zlib.h"
michael@0 15
michael@0 16 /**
michael@0 17 * Abstract class to handle mmap()ing from various kind of entities, such as
michael@0 18 * plain files or Zip entries. The virtual members are meant to act as the
michael@0 19 * equivalent system functions, except mapped memory is always MAP_PRIVATE,
michael@0 20 * even though a given implementation may use something different internally.
michael@0 21 */
michael@0 22 class Mappable: public mozilla::RefCounted<Mappable>
michael@0 23 {
michael@0 24 public:
michael@0 25 MOZ_DECLARE_REFCOUNTED_TYPENAME(Mappable)
michael@0 26 virtual ~Mappable() { }
michael@0 27
michael@0 28 virtual MemoryRange mmap(const void *addr, size_t length, int prot, int flags,
michael@0 29 off_t offset) = 0;
michael@0 30
michael@0 31 enum Kind {
michael@0 32 MAPPABLE_FILE,
michael@0 33 MAPPABLE_EXTRACT_FILE,
michael@0 34 MAPPABLE_DEFLATE,
michael@0 35 MAPPABLE_SEEKABLE_ZSTREAM
michael@0 36 };
michael@0 37
michael@0 38 virtual Kind GetKind() const = 0;
michael@0 39
michael@0 40 private:
michael@0 41 virtual void munmap(void *addr, size_t length) {
michael@0 42 ::munmap(addr, length);
michael@0 43 }
michael@0 44 /* Limit use of Mappable::munmap to classes that keep track of the address
michael@0 45 * and size of the mapping. This allows to ignore ::munmap return value. */
michael@0 46 friend class Mappable1stPagePtr;
michael@0 47 friend class LibHandle;
michael@0 48
michael@0 49 public:
michael@0 50 /**
michael@0 51 * Ensures the availability of the memory pages for the page(s) containing
michael@0 52 * the given address. Returns whether the pages were successfully made
michael@0 53 * available.
michael@0 54 */
michael@0 55 virtual bool ensure(const void *addr) { return true; }
michael@0 56
michael@0 57 /**
michael@0 58 * Indicate to a Mappable instance that no further mmap is going to happen.
michael@0 59 */
michael@0 60 virtual void finalize() = 0;
michael@0 61
michael@0 62 /**
michael@0 63 * Shows some stats about the Mappable instance.
michael@0 64 * Meant for MappableSeekableZStream only.
michael@0 65 * As Mappables don't keep track of what they are instanciated for, the name
michael@0 66 * argument is used to make the stats logging useful to the reader. The when
michael@0 67 * argument is to be used by the caller to give an identifier of the when
michael@0 68 * the stats call is made.
michael@0 69 */
michael@0 70 virtual void stats(const char *when, const char *name) const { }
michael@0 71
michael@0 72 /**
michael@0 73 * Returns the maximum length that can be mapped from this Mappable for
michael@0 74 * offset = 0.
michael@0 75 */
michael@0 76 virtual size_t GetLength() const = 0;
michael@0 77 };
michael@0 78
michael@0 79 /**
michael@0 80 * Mappable implementation for plain files
michael@0 81 */
michael@0 82 class MappableFile: public Mappable
michael@0 83 {
michael@0 84 public:
michael@0 85 ~MappableFile() { }
michael@0 86
michael@0 87 /**
michael@0 88 * Create a MappableFile instance for the given file path.
michael@0 89 */
michael@0 90 static Mappable *Create(const char *path);
michael@0 91
michael@0 92 /* Inherited from Mappable */
michael@0 93 virtual MemoryRange mmap(const void *addr, size_t length, int prot, int flags, off_t offset);
michael@0 94 virtual void finalize();
michael@0 95 virtual size_t GetLength() const;
michael@0 96
michael@0 97 virtual Kind GetKind() const { return MAPPABLE_FILE; };
michael@0 98 protected:
michael@0 99 MappableFile(int fd): fd(fd) { }
michael@0 100
michael@0 101 private:
michael@0 102 /* File descriptor */
michael@0 103 AutoCloseFD fd;
michael@0 104 };
michael@0 105
michael@0 106 /**
michael@0 107 * Mappable implementation for deflated stream in a Zip archive
michael@0 108 * Inflates the complete stream into a cache file.
michael@0 109 */
michael@0 110 class MappableExtractFile: public MappableFile
michael@0 111 {
michael@0 112 public:
michael@0 113 ~MappableExtractFile();
michael@0 114
michael@0 115 /**
michael@0 116 * Create a MappableExtractFile instance for the given Zip stream. The name
michael@0 117 * argument is used to create the cache file in the cache directory.
michael@0 118 */
michael@0 119 static Mappable *Create(const char *name, Zip *zip, Zip::Stream *stream);
michael@0 120
michael@0 121 /* Override finalize from MappableFile */
michael@0 122 virtual void finalize() {}
michael@0 123
michael@0 124 virtual Kind GetKind() const { return MAPPABLE_EXTRACT_FILE; };
michael@0 125 private:
michael@0 126 MappableExtractFile(int fd, char *path)
michael@0 127 : MappableFile(fd), path(path), pid(getpid()) { }
michael@0 128
michael@0 129 /**
michael@0 130 * AutoUnlinkFile keeps track or a file name and removes (unlinks) the file
michael@0 131 * when the instance is destroyed.
michael@0 132 */
michael@0 133 struct AutoUnlinkFileTraits: public mozilla::ScopedDeleteArrayTraits<char>
michael@0 134 {
michael@0 135 static void release(char *value)
michael@0 136 {
michael@0 137 if (!value)
michael@0 138 return;
michael@0 139 unlink(value);
michael@0 140 mozilla::ScopedDeleteArrayTraits<char>::release(value);
michael@0 141 }
michael@0 142 };
michael@0 143 typedef mozilla::Scoped<AutoUnlinkFileTraits> AutoUnlinkFile;
michael@0 144
michael@0 145 /* Extracted file */
michael@0 146 AutoUnlinkFile path;
michael@0 147
michael@0 148 /* Id of the process that initialized the instance */
michael@0 149 pid_t pid;
michael@0 150 };
michael@0 151
michael@0 152 class _MappableBuffer;
michael@0 153
michael@0 154 /**
michael@0 155 * Mappable implementation for deflated stream in a Zip archive.
michael@0 156 * Inflates the mapped bits in a temporary buffer.
michael@0 157 */
michael@0 158 class MappableDeflate: public Mappable
michael@0 159 {
michael@0 160 public:
michael@0 161 ~MappableDeflate();
michael@0 162
michael@0 163 /**
michael@0 164 * Create a MappableDeflate instance for the given Zip stream. The name
michael@0 165 * argument is used for an appropriately named temporary file, and the Zip
michael@0 166 * instance is given for the MappableDeflate to keep a reference of it.
michael@0 167 */
michael@0 168 static Mappable *Create(const char *name, Zip *zip, Zip::Stream *stream);
michael@0 169
michael@0 170 /* Inherited from Mappable */
michael@0 171 virtual MemoryRange mmap(const void *addr, size_t length, int prot, int flags, off_t offset);
michael@0 172 virtual void finalize();
michael@0 173 virtual size_t GetLength() const;
michael@0 174
michael@0 175 virtual Kind GetKind() const { return MAPPABLE_DEFLATE; };
michael@0 176 private:
michael@0 177 MappableDeflate(_MappableBuffer *buf, Zip *zip, Zip::Stream *stream);
michael@0 178
michael@0 179 /* Zip reference */
michael@0 180 mozilla::RefPtr<Zip> zip;
michael@0 181
michael@0 182 /* Decompression buffer */
michael@0 183 mozilla::ScopedDeletePtr<_MappableBuffer> buffer;
michael@0 184
michael@0 185 /* Zlib data */
michael@0 186 z_stream zStream;
michael@0 187 };
michael@0 188
michael@0 189 /**
michael@0 190 * Mappable implementation for seekable zStreams.
michael@0 191 * Inflates the mapped bits in a temporary buffer, on demand.
michael@0 192 */
michael@0 193 class MappableSeekableZStream: public Mappable
michael@0 194 {
michael@0 195 public:
michael@0 196 ~MappableSeekableZStream();
michael@0 197
michael@0 198 /**
michael@0 199 * Create a MappableSeekableZStream instance for the given Zip stream. The
michael@0 200 * name argument is used for an appropriately named temporary file, and the
michael@0 201 * Zip instance is given for the MappableSeekableZStream to keep a reference
michael@0 202 * of it.
michael@0 203 */
michael@0 204 static Mappable *Create(const char *name, Zip *zip,
michael@0 205 Zip::Stream *stream);
michael@0 206
michael@0 207 /* Inherited from Mappable */
michael@0 208 virtual MemoryRange mmap(const void *addr, size_t length, int prot, int flags, off_t offset);
michael@0 209 virtual void munmap(void *addr, size_t length);
michael@0 210 virtual void finalize();
michael@0 211 virtual bool ensure(const void *addr);
michael@0 212 virtual void stats(const char *when, const char *name) const;
michael@0 213 virtual size_t GetLength() const;
michael@0 214
michael@0 215 virtual Kind GetKind() const { return MAPPABLE_SEEKABLE_ZSTREAM; };
michael@0 216 private:
michael@0 217 MappableSeekableZStream(Zip *zip);
michael@0 218
michael@0 219 /* Zip reference */
michael@0 220 mozilla::RefPtr<Zip> zip;
michael@0 221
michael@0 222 /* Decompression buffer */
michael@0 223 mozilla::ScopedDeletePtr<_MappableBuffer> buffer;
michael@0 224
michael@0 225 /* Seekable ZStream */
michael@0 226 SeekableZStream zStream;
michael@0 227
michael@0 228 /* Keep track of mappings performed with MappableSeekableZStream::mmap so
michael@0 229 * that they can be realized by MappableSeekableZStream::ensure.
michael@0 230 * Values stored in the struct are those passed to mmap */
michael@0 231 struct LazyMap
michael@0 232 {
michael@0 233 const void *addr;
michael@0 234 size_t length;
michael@0 235 int prot;
michael@0 236 off_t offset;
michael@0 237
michael@0 238 /* Returns addr + length, as a pointer */
michael@0 239 const void *end() const {
michael@0 240 return reinterpret_cast<const void *>
michael@0 241 (reinterpret_cast<const unsigned char *>(addr) + length);
michael@0 242 }
michael@0 243
michael@0 244 /* Returns offset + length */
michael@0 245 const off_t endOffset() const {
michael@0 246 return offset + length;
michael@0 247 }
michael@0 248
michael@0 249 /* Returns the offset corresponding to the given address */
michael@0 250 const off_t offsetOf(const void *ptr) const {
michael@0 251 return reinterpret_cast<uintptr_t>(ptr)
michael@0 252 - reinterpret_cast<uintptr_t>(addr) + offset;
michael@0 253 }
michael@0 254
michael@0 255 /* Returns whether the given address is in the LazyMap range */
michael@0 256 const bool Contains(const void *ptr) const {
michael@0 257 return (ptr >= addr) && (ptr < end());
michael@0 258 }
michael@0 259 };
michael@0 260
michael@0 261 /* List of all mappings */
michael@0 262 std::vector<LazyMap> lazyMaps;
michael@0 263
michael@0 264 /* Array keeping track of which chunks have already been decompressed.
michael@0 265 * Each value is the number of pages decompressed for the given chunk. */
michael@0 266 mozilla::ScopedDeleteArray<unsigned char> chunkAvail;
michael@0 267
michael@0 268 /* Number of chunks that have already been decompressed. */
michael@0 269 mozilla::Atomic<size_t> chunkAvailNum;
michael@0 270
michael@0 271 /* Mutex protecting decompression */
michael@0 272 pthread_mutex_t mutex;
michael@0 273 };
michael@0 274
michael@0 275 #endif /* Mappable_h */

mercurial