diff options
Diffstat (limited to 'util/mmap.hh')
-rw-r--r-- | util/mmap.hh | 66 |
1 files changed, 48 insertions, 18 deletions
diff --git a/util/mmap.hh b/util/mmap.hh index 9ac604975..b474dc75b 100644 --- a/util/mmap.hh +++ b/util/mmap.hh @@ -12,7 +12,7 @@ namespace util { class scoped_fd; -long SizePage(); +std::size_t SizePage(); // (void*)-1 is MAP_FAILED; this is done to avoid including the mmap header here. class scoped_mmap { @@ -37,6 +37,13 @@ class scoped_mmap { reset((void*)-1, 0); } + void *steal() { + void *ret = data_; + data_ = (void*)-1; + size_ = 0; + return ret; + } + private: void *data_; std::size_t size_; @@ -51,13 +58,21 @@ class scoped_mmap { */ class scoped_memory { public: - typedef enum {MMAP_ALLOCATED, ARRAY_ALLOCATED, MALLOC_ALLOCATED, NONE_ALLOCATED} Alloc; + typedef enum { + MMAP_ROUND_UP_ALLOCATED, // The size was rounded up to a multiple of page size. Do the same before munmap. + MMAP_ALLOCATED, // munmap + MALLOC_ALLOCATED, // free + NONE_ALLOCATED // nothing here! + } Alloc; scoped_memory(void *data, std::size_t size, Alloc source) : data_(data), size_(size), source_(source) {} scoped_memory() : data_(NULL), size_(0), source_(NONE_ALLOCATED) {} + // Calls HugeMalloc + scoped_memory(std::size_t to, bool zero_new); + ~scoped_memory() { reset(); } void *get() const { return data_; } @@ -71,9 +86,13 @@ class scoped_memory { void reset(void *data, std::size_t size, Alloc from); - // realloc allows the current data to escape hence the need for this call - // If realloc fails, destroys the original too and get() returns NULL. - void call_realloc(std::size_t to); + void *steal() { + void *ret = data_; + data_ = NULL; + size_ = 0; + source_ = NONE_ALLOCATED; + return ret; + } private: void *data_; @@ -85,6 +104,30 @@ class scoped_memory { scoped_memory &operator=(const scoped_memory &); }; +extern const int kFileFlags; + +// Cross-platform, error-checking wrapper for mmap(). +void *MapOrThrow(std::size_t size, bool for_write, int flags, bool prefault, int fd, uint64_t offset = 0); + +// msync wrapper +void SyncOrThrow(void *start, size_t length); + +// Cross-platform, error-checking wrapper for munmap(). +void UnmapOrThrow(void *start, size_t length); + +// Allocate memory, promising that all/vast majority of it will be used. Tries +// hard to use huge pages on Linux. +// If you want zeroed memory, pass zeroed = true. +void HugeMalloc(std::size_t size, bool zeroed, scoped_memory &to); + +// Reallocates memory ala realloc but with option to zero the new memory. +// On Linux, the memory can come from anonymous mmap or malloc/calloc. +// On non-Linux, only malloc/calloc is supported. +// +// To summarize, any memory from HugeMalloc or HugeRealloc can be resized with +// this. +void HugeRealloc(std::size_t size, bool new_zeroed, scoped_memory &mem); + typedef enum { // mmap with no prepopulate LAZY, @@ -98,25 +141,12 @@ typedef enum { PARALLEL_READ, } LoadMethod; -extern const int kFileFlags; - -// Cross-platform, error-checking wrapper for mmap(). -void *MapOrThrow(std::size_t size, bool for_write, int flags, bool prefault, int fd, uint64_t offset = 0); - -// Cross-platform, error-checking wrapper for munmap(). -void UnmapOrThrow(void *start, size_t length); - void MapRead(LoadMethod method, int fd, uint64_t offset, std::size_t size, scoped_memory &out); -void MapAnonymous(std::size_t size, scoped_memory &to); - // Open file name with mmap of size bytes, all of which are initially zero. void *MapZeroedWrite(int fd, std::size_t size); void *MapZeroedWrite(const char *name, std::size_t size, scoped_fd &file); -// msync wrapper -void SyncOrThrow(void *start, size_t length); - // Forward rolling memory map with no overlap. class Rolling { public: |