Bug 1684463 - [devtools] Part 1: Shorten the _createAttribute function by refactoring...
[gecko.git] / mozglue / linker / Mappable.h
blob8468aaaccb124ddf21c38758deeedd626c619e70
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 file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef Mappable_h
6 #define Mappable_h
8 #include "Zip.h"
9 #include "mozilla/RefPtr.h"
10 #include "mozilla/UniquePtr.h"
11 #include "zlib.h"
13 /**
14 * Abstract class to handle mmap()ing from various kind of entities, such as
15 * plain files or Zip entries. The virtual members are meant to act as the
16 * equivalent system functions, except mapped memory is always MAP_PRIVATE,
17 * even though a given implementation may use something different internally.
19 class Mappable : public mozilla::RefCounted<Mappable> {
20 public:
21 MOZ_DECLARE_REFCOUNTED_TYPENAME(Mappable)
22 virtual ~Mappable() {}
24 virtual MemoryRange mmap(const void* addr, size_t length, int prot, int flags,
25 off_t offset) = 0;
27 enum Kind {
28 MAPPABLE_FILE,
29 MAPPABLE_EXTRACT_FILE,
30 MAPPABLE_DEFLATE,
31 MAPPABLE_SEEKABLE_ZSTREAM
34 virtual Kind GetKind() const = 0;
36 private:
37 virtual void munmap(void* addr, size_t length) { ::munmap(addr, length); }
38 /* Limit use of Mappable::munmap to classes that keep track of the address
39 * and size of the mapping. This allows to ignore ::munmap return value. */
40 friend class Mappable1stPagePtr;
41 friend class LibHandle;
43 public:
44 /**
45 * Indicate to a Mappable instance that no further mmap is going to happen.
47 virtual void finalize() = 0;
49 /**
50 * Returns the maximum length that can be mapped from this Mappable for
51 * offset = 0.
53 virtual size_t GetLength() const = 0;
56 /**
57 * Mappable implementation for plain files
59 class MappableFile : public Mappable {
60 public:
61 ~MappableFile() {}
63 /**
64 * Create a MappableFile instance for the given file path.
66 static Mappable* Create(const char* path);
68 /* Inherited from Mappable */
69 virtual MemoryRange mmap(const void* addr, size_t length, int prot, int flags,
70 off_t offset);
71 virtual void finalize();
72 virtual size_t GetLength() const;
74 virtual Kind GetKind() const { return MAPPABLE_FILE; };
76 protected:
77 explicit MappableFile(int fd) : fd(fd) {}
79 private:
80 /* File descriptor */
81 AutoCloseFD fd;
84 /**
85 * Mappable implementation for deflated stream in a Zip archive
86 * Inflates the complete stream into a cache file.
88 class MappableExtractFile : public MappableFile {
89 public:
90 ~MappableExtractFile() = default;
92 /**
93 * Create a MappableExtractFile instance for the given Zip stream. The name
94 * argument is used to create the cache file in the cache directory.
96 static Mappable* Create(const char* name, Zip* zip, Zip::Stream* stream);
98 /* Override finalize from MappableFile */
99 virtual void finalize() {}
101 virtual Kind GetKind() const { return MAPPABLE_EXTRACT_FILE; };
103 private:
105 * AutoUnlinkFile keeps track of a file name and removes (unlinks) the file
106 * when the instance is destroyed.
108 struct UnlinkFile {
109 void operator()(char* value) {
110 unlink(value);
111 delete[] value;
114 typedef mozilla::UniquePtr<char[], UnlinkFile> AutoUnlinkFile;
116 MappableExtractFile(int fd, const char* path)
117 : MappableFile(fd), path(path) {}
119 /* Extracted file path */
120 mozilla::UniquePtr<const char[]> path;
123 class _MappableBuffer;
126 * Mappable implementation for deflated stream in a Zip archive.
127 * Inflates the mapped bits in a temporary buffer.
129 class MappableDeflate : public Mappable {
130 public:
131 ~MappableDeflate();
134 * Create a MappableDeflate instance for the given Zip stream. The name
135 * argument is used for an appropriately named temporary file, and the Zip
136 * instance is given for the MappableDeflate to keep a reference of it.
138 static Mappable* Create(const char* name, Zip* zip, Zip::Stream* stream);
140 /* Inherited from Mappable */
141 virtual MemoryRange mmap(const void* addr, size_t length, int prot, int flags,
142 off_t offset);
143 virtual void finalize();
144 virtual size_t GetLength() const;
146 virtual Kind GetKind() const { return MAPPABLE_DEFLATE; };
148 private:
149 MappableDeflate(_MappableBuffer* buf, Zip* zip, Zip::Stream* stream);
151 /* Zip reference */
152 RefPtr<Zip> zip;
154 /* Decompression buffer */
155 mozilla::UniquePtr<_MappableBuffer> buffer;
157 /* Zlib data */
158 z_stream zStream;
161 #endif /* Mappable_h */