Mappable.h (1407B)
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/. */ 4 5 #ifndef Mappable_h 6 #define Mappable_h 7 8 #include <optional> 9 #include "mozilla/RefCounted.h" 10 #include "Utils.h" 11 12 /** 13 * Helper class to mmap plain files. 14 */ 15 class Mappable : public mozilla::RefCounted<Mappable> { 16 public: 17 MOZ_DECLARE_REFCOUNTED_TYPENAME(Mappable) 18 ~Mappable() {} 19 20 MemoryRange mmap(const void* addr, size_t length, int prot, int flags, 21 off_t offset); 22 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; 29 30 public: 31 /** 32 * Indicate to a Mappable instance that no further mmap is going to happen. 33 */ 34 void finalize(); 35 36 /** 37 * Returns the maximum length that can be mapped from this Mappable for 38 * offset = 0. 39 */ 40 size_t GetLength() const; 41 42 /** 43 * Create a Mappable instance for the given file path. 44 */ 45 static Mappable* Create(const char* path); 46 47 protected: 48 explicit Mappable(int fd) : fd(fd) {} 49 50 private: 51 /* File descriptor */ 52 std::optional<AutoCloseFD> fd; 53 }; 54 55 #endif /* Mappable_h */