diff options
Diffstat (limited to 'klm/util/mmap.hh')
-rw-r--r-- | klm/util/mmap.hh | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/klm/util/mmap.hh b/klm/util/mmap.hh index c9068ec9..0a504d89 100644 --- a/klm/util/mmap.hh +++ b/klm/util/mmap.hh @@ -6,6 +6,7 @@ #include <cstddef> +#include <inttypes.h> #include <sys/types.h> namespace util { @@ -19,8 +20,8 @@ class scoped_mmap { void *get() const { return data_; } - const char *begin() const { return reinterpret_cast<char*>(data_); } - const char *end() const { return reinterpret_cast<char*>(data_) + size_; } + const uint8_t *begin() const { return reinterpret_cast<uint8_t*>(data_); } + const uint8_t *end() const { return reinterpret_cast<uint8_t*>(data_) + size_; } std::size_t size() const { return size_; } void reset(void *data, std::size_t size) { @@ -79,23 +80,27 @@ class scoped_memory { scoped_memory &operator=(const scoped_memory &); }; -struct scoped_mapped_file { - scoped_fd fd; - scoped_mmap mem; -}; +typedef enum { + // mmap with no prepopulate + LAZY, + // On linux, pass MAP_POPULATE to mmap. + POPULATE_OR_LAZY, + // Populate on Linux. malloc and read on non-Linux. + POPULATE_OR_READ, + // malloc and read. + READ +} LoadMethod; + // Wrapper around mmap to check it worked and hide some platform macros. void *MapOrThrow(std::size_t size, bool for_write, int flags, bool prefault, int fd, off_t offset = 0); -void *MapForRead(std::size_t size, bool prefault, int fd, off_t offset = 0); +void MapRead(LoadMethod method, int fd, off_t offset, std::size_t size, scoped_memory &out); void *MapAnonymous(std::size_t size); // Open file name with mmap of size bytes, all of which are initially zero. -void MapZeroedWrite(const char *name, std::size_t size, scoped_fd &file, scoped_mmap &mem); -inline void MapZeroedWrite(const char *name, std::size_t size, scoped_mapped_file &out) { - MapZeroedWrite(name, size, out.fd, out.mem); -} - +void *MapZeroedWrite(const char *name, std::size_t size, scoped_fd &file); + } // namespace util -#endif // UTIL_SCOPED__ +#endif // UTIL_MMAP__ |