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/. */
9 #include "mozilla/RefPtr.h"
10 #include "mozilla/UniquePtr.h"
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
> {
21 MOZ_DECLARE_REFCOUNTED_TYPENAME(Mappable
)
22 virtual ~Mappable() {}
24 virtual MemoryRange
mmap(const void* addr
, size_t length
, int prot
, int flags
,
29 MAPPABLE_EXTRACT_FILE
,
31 MAPPABLE_SEEKABLE_ZSTREAM
34 virtual Kind
GetKind() const = 0;
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
;
45 * Indicate to a Mappable instance that no further mmap is going to happen.
47 virtual void finalize() = 0;
50 * Returns the maximum length that can be mapped from this Mappable for
53 virtual size_t GetLength() const = 0;
57 * Mappable implementation for plain files
59 class MappableFile
: public Mappable
{
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
,
71 virtual void finalize();
72 virtual size_t GetLength() const;
74 virtual Kind
GetKind() const { return MAPPABLE_FILE
; };
77 explicit MappableFile(int fd
) : fd(fd
) {}
85 * Mappable implementation for deflated stream in a Zip archive
86 * Inflates the complete stream into a cache file.
88 class MappableExtractFile
: public MappableFile
{
90 ~MappableExtractFile() = default;
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
; };
105 * AutoUnlinkFile keeps track of a file name and removes (unlinks) the file
106 * when the instance is destroyed.
109 void operator()(char* 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
{
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
,
143 virtual void finalize();
144 virtual size_t GetLength() const;
146 virtual Kind
GetKind() const { return MAPPABLE_DEFLATE
; };
149 MappableDeflate(_MappableBuffer
* buf
, Zip
* zip
, Zip::Stream
* stream
);
154 /* Decompression buffer */
155 mozilla::UniquePtr
<_MappableBuffer
> buffer
;
161 #endif /* Mappable_h */