2012-01-20 12:48:44 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef Mappable_h
|
|
|
|
#define Mappable_h
|
|
|
|
|
2012-01-20 12:48:50 +04:00
|
|
|
#include <sys/types.h>
|
2012-02-22 11:12:15 +04:00
|
|
|
#include <pthread.h>
|
2012-01-20 12:48:44 +04:00
|
|
|
#include "Zip.h"
|
2012-02-22 11:12:15 +04:00
|
|
|
#include "SeekableZStream.h"
|
2012-01-20 12:48:44 +04:00
|
|
|
#include "mozilla/RefPtr.h"
|
2014-07-22 02:14:11 +04:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2012-01-20 12:48:44 +04:00
|
|
|
#include "zlib.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class to handle mmap()ing from various kind of entities, such as
|
|
|
|
* plain files or Zip entries. The virtual members are meant to act as the
|
2013-07-23 02:26:07 +04:00
|
|
|
* equivalent system functions, except mapped memory is always MAP_PRIVATE,
|
|
|
|
* even though a given implementation may use something different internally.
|
2012-01-20 12:48:44 +04:00
|
|
|
*/
|
2013-06-27 04:35:48 +04:00
|
|
|
class Mappable: public mozilla::RefCounted<Mappable>
|
2012-01-20 12:48:44 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-02-21 18:11:33 +04:00
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(Mappable)
|
2012-01-20 12:48:44 +04:00
|
|
|
virtual ~Mappable() { }
|
|
|
|
|
2013-06-27 04:35:49 +04:00
|
|
|
virtual MemoryRange mmap(const void *addr, size_t length, int prot, int flags,
|
|
|
|
off_t offset) = 0;
|
2012-02-22 11:12:15 +04:00
|
|
|
|
2013-04-12 12:23:12 +04:00
|
|
|
enum Kind {
|
|
|
|
MAPPABLE_FILE,
|
|
|
|
MAPPABLE_EXTRACT_FILE,
|
|
|
|
MAPPABLE_DEFLATE,
|
|
|
|
MAPPABLE_SEEKABLE_ZSTREAM
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual Kind GetKind() const = 0;
|
|
|
|
|
2012-02-22 11:12:15 +04:00
|
|
|
private:
|
2012-01-20 12:48:44 +04:00
|
|
|
virtual void munmap(void *addr, size_t length) {
|
|
|
|
::munmap(addr, length);
|
|
|
|
}
|
2012-02-22 11:12:15 +04:00
|
|
|
/* Limit use of Mappable::munmap to classes that keep track of the address
|
|
|
|
* and size of the mapping. This allows to ignore ::munmap return value. */
|
|
|
|
friend class Mappable1stPagePtr;
|
2013-04-12 12:23:12 +04:00
|
|
|
friend class LibHandle;
|
2012-01-20 12:48:44 +04:00
|
|
|
|
2012-02-22 11:12:15 +04:00
|
|
|
public:
|
2012-02-22 11:12:15 +04:00
|
|
|
/**
|
|
|
|
* Ensures the availability of the memory pages for the page(s) containing
|
|
|
|
* the given address. Returns whether the pages were successfully made
|
|
|
|
* available.
|
|
|
|
*/
|
|
|
|
virtual bool ensure(const void *addr) { return true; }
|
|
|
|
|
2012-01-20 12:48:44 +04:00
|
|
|
/**
|
|
|
|
* Indicate to a Mappable instance that no further mmap is going to happen.
|
|
|
|
*/
|
|
|
|
virtual void finalize() = 0;
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows some stats about the Mappable instance.
|
|
|
|
* Meant for MappableSeekableZStream only.
|
|
|
|
* As Mappables don't keep track of what they are instanciated for, the name
|
|
|
|
* argument is used to make the stats logging useful to the reader. The when
|
|
|
|
* argument is to be used by the caller to give an identifier of the when
|
|
|
|
* the stats call is made.
|
|
|
|
*/
|
|
|
|
virtual void stats(const char *when, const char *name) const { }
|
2013-04-12 12:23:12 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the maximum length that can be mapped from this Mappable for
|
|
|
|
* offset = 0.
|
|
|
|
*/
|
|
|
|
virtual size_t GetLength() const = 0;
|
2012-01-20 12:48:44 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mappable implementation for plain files
|
|
|
|
*/
|
|
|
|
class MappableFile: public Mappable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~MappableFile() { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a MappableFile instance for the given file path.
|
|
|
|
*/
|
2012-02-22 11:12:15 +04:00
|
|
|
static Mappable *Create(const char *path);
|
2012-01-20 12:48:44 +04:00
|
|
|
|
|
|
|
/* Inherited from Mappable */
|
2013-06-27 04:35:49 +04:00
|
|
|
virtual MemoryRange mmap(const void *addr, size_t length, int prot, int flags, off_t offset);
|
2012-01-20 12:48:44 +04:00
|
|
|
virtual void finalize();
|
2013-04-12 12:23:12 +04:00
|
|
|
virtual size_t GetLength() const;
|
2012-01-20 12:48:44 +04:00
|
|
|
|
2013-04-12 12:23:12 +04:00
|
|
|
virtual Kind GetKind() const { return MAPPABLE_FILE; };
|
2012-01-20 12:48:50 +04:00
|
|
|
protected:
|
2012-01-20 12:48:44 +04:00
|
|
|
MappableFile(int fd): fd(fd) { }
|
|
|
|
|
2012-01-20 12:48:50 +04:00
|
|
|
private:
|
2012-01-20 12:48:44 +04:00
|
|
|
/* File descriptor */
|
|
|
|
AutoCloseFD fd;
|
|
|
|
};
|
|
|
|
|
2012-01-20 12:48:50 +04:00
|
|
|
/**
|
|
|
|
* Mappable implementation for deflated stream in a Zip archive
|
|
|
|
* Inflates the complete stream into a cache file.
|
|
|
|
*/
|
|
|
|
class MappableExtractFile: public MappableFile
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~MappableExtractFile();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a MappableExtractFile instance for the given Zip stream. The name
|
|
|
|
* argument is used to create the cache file in the cache directory.
|
|
|
|
*/
|
2012-02-22 11:12:15 +04:00
|
|
|
static Mappable *Create(const char *name, Zip *zip, Zip::Stream *stream);
|
2012-01-20 12:48:50 +04:00
|
|
|
|
2013-06-27 04:35:48 +04:00
|
|
|
/* Override finalize from MappableFile */
|
|
|
|
virtual void finalize() {}
|
|
|
|
|
2013-04-12 12:23:12 +04:00
|
|
|
virtual Kind GetKind() const { return MAPPABLE_EXTRACT_FILE; };
|
2012-01-20 12:48:50 +04:00
|
|
|
private:
|
|
|
|
/**
|
2014-07-22 02:14:11 +04:00
|
|
|
* AutoUnlinkFile keeps track of a file name and removes (unlinks) the file
|
2012-01-20 12:48:50 +04:00
|
|
|
* when the instance is destroyed.
|
|
|
|
*/
|
2014-07-22 02:14:11 +04:00
|
|
|
struct UnlinkFile
|
2012-01-20 12:48:50 +04:00
|
|
|
{
|
2014-07-22 02:14:11 +04:00
|
|
|
void operator()(char *value) {
|
2012-01-20 12:48:50 +04:00
|
|
|
unlink(value);
|
2014-07-22 02:14:11 +04:00
|
|
|
delete [] value;
|
2012-01-20 12:48:50 +04:00
|
|
|
}
|
|
|
|
};
|
2014-07-22 02:14:11 +04:00
|
|
|
typedef mozilla::UniquePtr<char[], UnlinkFile> AutoUnlinkFile;
|
|
|
|
|
|
|
|
MappableExtractFile(int fd, AutoUnlinkFile path)
|
|
|
|
: MappableFile(fd), path(Move(path)), pid(getpid()) { }
|
2012-01-20 12:48:50 +04:00
|
|
|
|
|
|
|
/* Extracted file */
|
|
|
|
AutoUnlinkFile path;
|
|
|
|
|
|
|
|
/* Id of the process that initialized the instance */
|
|
|
|
pid_t pid;
|
|
|
|
};
|
|
|
|
|
2012-01-20 12:48:44 +04:00
|
|
|
class _MappableBuffer;
|
|
|
|
|
|
|
|
/**
|
2012-01-20 12:48:50 +04:00
|
|
|
* Mappable implementation for deflated stream in a Zip archive.
|
|
|
|
* Inflates the mapped bits in a temporary buffer.
|
2012-01-20 12:48:44 +04:00
|
|
|
*/
|
|
|
|
class MappableDeflate: public Mappable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~MappableDeflate();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a MappableDeflate instance for the given Zip stream. The name
|
|
|
|
* argument is used for an appropriately named temporary file, and the Zip
|
|
|
|
* instance is given for the MappableDeflate to keep a reference of it.
|
|
|
|
*/
|
2012-02-22 11:12:15 +04:00
|
|
|
static Mappable *Create(const char *name, Zip *zip, Zip::Stream *stream);
|
2012-01-20 12:48:44 +04:00
|
|
|
|
|
|
|
/* Inherited from Mappable */
|
2013-06-27 04:35:49 +04:00
|
|
|
virtual MemoryRange mmap(const void *addr, size_t length, int prot, int flags, off_t offset);
|
2012-01-20 12:48:44 +04:00
|
|
|
virtual void finalize();
|
2013-04-12 12:23:12 +04:00
|
|
|
virtual size_t GetLength() const;
|
2012-01-20 12:48:44 +04:00
|
|
|
|
2013-04-12 12:23:12 +04:00
|
|
|
virtual Kind GetKind() const { return MAPPABLE_DEFLATE; };
|
2012-01-20 12:48:44 +04:00
|
|
|
private:
|
|
|
|
MappableDeflate(_MappableBuffer *buf, Zip *zip, Zip::Stream *stream);
|
|
|
|
|
|
|
|
/* Zip reference */
|
|
|
|
mozilla::RefPtr<Zip> zip;
|
|
|
|
|
|
|
|
/* Decompression buffer */
|
2014-07-22 02:14:11 +04:00
|
|
|
mozilla::UniquePtr<_MappableBuffer> buffer;
|
2012-01-20 12:48:44 +04:00
|
|
|
|
|
|
|
/* Zlib data */
|
2014-10-28 10:45:17 +03:00
|
|
|
zxx_stream zStream;
|
2012-01-20 12:48:44 +04:00
|
|
|
};
|
|
|
|
|
2012-02-22 11:12:15 +04:00
|
|
|
/**
|
|
|
|
* Mappable implementation for seekable zStreams.
|
|
|
|
* Inflates the mapped bits in a temporary buffer, on demand.
|
|
|
|
*/
|
|
|
|
class MappableSeekableZStream: public Mappable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~MappableSeekableZStream();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a MappableSeekableZStream instance for the given Zip stream. The
|
|
|
|
* name argument is used for an appropriately named temporary file, and the
|
|
|
|
* Zip instance is given for the MappableSeekableZStream to keep a reference
|
|
|
|
* of it.
|
|
|
|
*/
|
2012-02-22 11:12:15 +04:00
|
|
|
static Mappable *Create(const char *name, Zip *zip,
|
2012-02-22 11:12:15 +04:00
|
|
|
Zip::Stream *stream);
|
|
|
|
|
|
|
|
/* Inherited from Mappable */
|
2013-06-27 04:35:49 +04:00
|
|
|
virtual MemoryRange mmap(const void *addr, size_t length, int prot, int flags, off_t offset);
|
2012-02-22 11:12:15 +04:00
|
|
|
virtual void munmap(void *addr, size_t length);
|
|
|
|
virtual void finalize();
|
|
|
|
virtual bool ensure(const void *addr);
|
2012-02-22 11:12:15 +04:00
|
|
|
virtual void stats(const char *when, const char *name) const;
|
2013-04-12 12:23:12 +04:00
|
|
|
virtual size_t GetLength() const;
|
2012-02-22 11:12:15 +04:00
|
|
|
|
2013-04-12 12:23:12 +04:00
|
|
|
virtual Kind GetKind() const { return MAPPABLE_SEEKABLE_ZSTREAM; };
|
2012-02-22 11:12:15 +04:00
|
|
|
private:
|
|
|
|
MappableSeekableZStream(Zip *zip);
|
|
|
|
|
|
|
|
/* Zip reference */
|
|
|
|
mozilla::RefPtr<Zip> zip;
|
|
|
|
|
|
|
|
/* Decompression buffer */
|
2014-07-22 02:14:11 +04:00
|
|
|
mozilla::UniquePtr<_MappableBuffer> buffer;
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
/* Seekable ZStream */
|
|
|
|
SeekableZStream zStream;
|
|
|
|
|
|
|
|
/* Keep track of mappings performed with MappableSeekableZStream::mmap so
|
|
|
|
* that they can be realized by MappableSeekableZStream::ensure.
|
|
|
|
* Values stored in the struct are those passed to mmap */
|
|
|
|
struct LazyMap
|
|
|
|
{
|
|
|
|
const void *addr;
|
|
|
|
size_t length;
|
|
|
|
int prot;
|
|
|
|
off_t offset;
|
|
|
|
|
|
|
|
/* Returns addr + length, as a pointer */
|
|
|
|
const void *end() const {
|
|
|
|
return reinterpret_cast<const void *>
|
|
|
|
(reinterpret_cast<const unsigned char *>(addr) + length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns offset + length */
|
|
|
|
const off_t endOffset() const {
|
|
|
|
return offset + length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the offset corresponding to the given address */
|
|
|
|
const off_t offsetOf(const void *ptr) const {
|
|
|
|
return reinterpret_cast<uintptr_t>(ptr)
|
|
|
|
- reinterpret_cast<uintptr_t>(addr) + offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns whether the given address is in the LazyMap range */
|
|
|
|
const bool Contains(const void *ptr) const {
|
|
|
|
return (ptr >= addr) && (ptr < end());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* List of all mappings */
|
|
|
|
std::vector<LazyMap> lazyMaps;
|
|
|
|
|
|
|
|
/* Array keeping track of which chunks have already been decompressed.
|
|
|
|
* Each value is the number of pages decompressed for the given chunk. */
|
2014-07-22 02:14:11 +04:00
|
|
|
mozilla::UniquePtr<unsigned char[]> chunkAvail;
|
2012-02-22 11:12:15 +04:00
|
|
|
|
2012-02-22 11:12:15 +04:00
|
|
|
/* Number of chunks that have already been decompressed. */
|
2013-07-26 07:57:53 +04:00
|
|
|
mozilla::Atomic<size_t> chunkAvailNum;
|
2012-02-22 11:12:15 +04:00
|
|
|
|
2012-02-22 11:12:15 +04:00
|
|
|
/* Mutex protecting decompression */
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
};
|
|
|
|
|
2012-01-20 12:48:44 +04:00
|
|
|
#endif /* Mappable_h */
|