| 9 | #include <cstddef> |
| 10 | |
| 11 | class FileMemMap { |
| 12 | private: |
| 13 | void *mAddress = nullptr; |
| 14 | size_t mMapLength = 0; |
| 15 | size_t mLength = 0; |
| 16 | |
| 17 | public: |
| 18 | FileMemMap() = default; |
| 19 | |
| 20 | ~FileMemMap() noexcept; |
| 21 | |
| 22 | FileMemMap(const FileMemMap &) = delete; |
| 23 | |
| 24 | FileMemMap &operator=(const FileMemMap &other) = delete; |
| 25 | |
| 26 | /** |
| 27 | * Map a file into memory. |
| 28 | * @param path the absolute path to the file to map. |
| 29 | * @param readOnly whether the file should be mapped read-only. |
| 30 | * @param length the length of the file to map, may be 0 to map the entire file. |
| 31 | * @return 0 on success, errno on error. |
| 32 | */ |
| 33 | [[nodiscard]] int mapFilePath(const char* path, bool readOnly = true, size_t length = 0); |
| 34 | |
| 35 | /** |
| 36 | * Map a file into memory. |
| 37 | * Note that the fd will not be closed when the FileMemMap is destroyed. |
| 38 | * @param fd the file descriptor of the file to map. |
| 39 | * @param readOnly whether the file should be mapped read-only. |
| 40 | * @param length the length of the file to map, may be 0 to map the entire file. |
| 41 | * @param shared whether the file should be mapped with MAP_SHARED or MAP_PRIVATE. |
| 42 | * @return 0 on success, errno on error. |
| 43 | */ |
| 44 | [[nodiscard]] int mapFileDescriptor(int fd, bool readOnly = true, size_t length = 0, bool shared = false); |
| 45 | |
| 46 | /** |
| 47 | * Get the address of the mapped file. |
| 48 | * @return address of the mapped file, or nullptr if the file is not mapped. |
| 49 | */ |
| 50 | [[nodiscard]] inline void *getAddress() const noexcept { |
| 51 | return mAddress; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get the length of the mapped file. |
| 56 | * @return length of the mapped file, or 0 if the file is not mapped. |
| 57 | */ |
| 58 | [[nodiscard]] inline size_t getLength() const noexcept { |
| 59 | return mLength; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get is the file is mapped. |
| 64 | * @return true if the file is mapped, false otherwise. |
| 65 | */ |
| 66 | [[nodiscard]] inline bool isValid() const noexcept { |
| 67 | return mAddress != nullptr && mLength != 0; |
| 68 | } |
nothing calls this directly
no outgoing calls
no test coverage detected