mmap.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright (c) Meta Platforms, Inc. and affiliates.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. #ifdef OS_WIN
  7. #include "port/win/port_win.h"
  8. // ^^^ For proper/safe inclusion of windows.h. Must come first.
  9. #include <memoryapi.h>
  10. #else
  11. #include <sys/mman.h>
  12. #endif // OS_WIN
  13. #include <cstdint>
  14. #include <utility>
  15. #include "rocksdb/rocksdb_namespace.h"
  16. namespace ROCKSDB_NAMESPACE {
  17. // An RAII wrapper for mmaped memory
  18. class MemMapping {
  19. public:
  20. static constexpr bool kHugePageSupported =
  21. #if defined(MAP_HUGETLB) || defined(FILE_MAP_LARGE_PAGES)
  22. true;
  23. #else
  24. false;
  25. #endif
  26. // Allocate memory requesting to be backed by huge pages
  27. static MemMapping AllocateHuge(size_t length);
  28. // Allocate memory that is only lazily mapped to resident memory and
  29. // guaranteed to be zero-initialized. Note that some platforms like
  30. // Linux allow memory over-commit, where only the used portion of memory
  31. // matters, while other platforms require enough swap space (page file) to
  32. // back the full mapping.
  33. static MemMapping AllocateLazyZeroed(size_t length);
  34. // No copies
  35. MemMapping(const MemMapping&) = delete;
  36. MemMapping& operator=(const MemMapping&) = delete;
  37. // Move
  38. MemMapping(MemMapping&&) noexcept;
  39. MemMapping& operator=(MemMapping&&) noexcept;
  40. // Releases the mapping
  41. ~MemMapping();
  42. inline void* Get() const { return addr_; }
  43. inline size_t Length() const { return length_; }
  44. private:
  45. MemMapping() {}
  46. // The mapped memory, or nullptr on failure / not supported
  47. void* addr_ = nullptr;
  48. // The known usable number of bytes starting at that address
  49. size_t length_ = 0;
  50. #ifdef OS_WIN
  51. HANDLE page_file_handle_ = NULL;
  52. #endif // OS_WIN
  53. static MemMapping AllocateAnonymous(size_t length, bool huge);
  54. };
  55. // Simple MemMapping wrapper that presents the memory as an array of T.
  56. // For example,
  57. // TypedMemMapping<uint64_t> arr = MemMapping::AllocateLazyZeroed(num_bytes);
  58. template <typename T>
  59. class TypedMemMapping : public MemMapping {
  60. public:
  61. /*implicit*/ TypedMemMapping(MemMapping&& v) noexcept
  62. : MemMapping(std::move(v)) {}
  63. TypedMemMapping& operator=(MemMapping&& v) noexcept {
  64. MemMapping& base = *this;
  65. base = std::move(v);
  66. }
  67. inline T* Get() const { return static_cast<T*>(MemMapping::Get()); }
  68. inline size_t Count() const { return MemMapping::Length() / sizeof(T); }
  69. inline T& operator[](size_t index) const { return Get()[index]; }
  70. };
  71. } // namespace ROCKSDB_NAMESPACE