Merge mozilla-central to autoland. CLOSED TREE
[gecko.git] / mozglue / linker / Mappable.h
blob3e25fc88c99bdc66123aecbf7a65fa248c520dde
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 <optional>
9 #include "mozilla/RefCounted.h"
10 #include "Utils.h"
12 /**
13 * Helper class to mmap plain files.
15 class Mappable : public mozilla::RefCounted<Mappable> {
16 public:
17 MOZ_DECLARE_REFCOUNTED_TYPENAME(Mappable)
18 ~Mappable() {}
20 MemoryRange mmap(const void* addr, size_t length, int prot, int flags,
21 off_t offset);
23 private:
24 void munmap(void* addr, size_t length) { ::munmap(addr, length); }
25 /* Limit use of Mappable::munmap to classes that keep track of the address
26 * and size of the mapping. This allows to ignore ::munmap return value. */
27 friend class Mappable1stPagePtr;
28 friend class LibHandle;
30 public:
31 /**
32 * Indicate to a Mappable instance that no further mmap is going to happen.
34 void finalize();
36 /**
37 * Returns the maximum length that can be mapped from this Mappable for
38 * offset = 0.
40 size_t GetLength() const;
42 /**
43 * Create a Mappable instance for the given file path.
45 static Mappable* Create(const char* path);
47 protected:
48 explicit Mappable(int fd) : fd(fd) {}
50 private:
51 /* File descriptor */
52 std::optional<AutoCloseFD> fd;
55 #endif /* Mappable_h */