MapAllocator.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #pragma once
  2. #include <c10/core/Allocator.h>
  3. #include <c10/util/string_view.h>
  4. namespace at {
  5. enum MappedAllocatorModes {
  6. ALLOCATOR_MAPPED_SHARED = 1,
  7. ALLOCATOR_MAPPED_SHAREDMEM = 2,
  8. ALLOCATOR_MAPPED_EXCLUSIVE = 4,
  9. ALLOCATOR_MAPPED_NOCREATE = 8,
  10. ALLOCATOR_MAPPED_KEEPFD = 16,
  11. ALLOCATOR_MAPPED_FROMFD = 32,
  12. ALLOCATOR_MAPPED_UNLINK = 64
  13. };
  14. // Sentinel value/type to help distinguish the file descriptor constructor from
  15. // the non-file descriptor constructor
  16. enum WithFd { WITH_FD };
  17. TORCH_API std::string NewProcessWideShmHandle();
  18. class TORCH_API MapAllocator {
  19. public:
  20. MapAllocator(c10::string_view filename, int flags, size_t size);
  21. MapAllocator(
  22. WithFd,
  23. c10::string_view filename,
  24. int fd,
  25. int flags,
  26. size_t size);
  27. MapAllocator(const MapAllocator&) = delete;
  28. MapAllocator& operator=(const MapAllocator&) = delete;
  29. MapAllocator(MapAllocator&&) = delete;
  30. MapAllocator& operator=(MapAllocator&&) = delete;
  31. const char* filename() const {
  32. return filename_.c_str();
  33. }
  34. int fd() const {
  35. #ifdef _WIN32
  36. TORCH_CHECK(false, "MapAllocator::fd() is unsupported on Windows");
  37. #else
  38. return fd_;
  39. #endif
  40. }
  41. ptrdiff_t size() const {
  42. return size_;
  43. }
  44. // Return a pointer to the actual data for this allocator
  45. // (in the case of the refcounted allocator, this is offset
  46. // from the base pointer.)
  47. virtual void* data() const {
  48. return base_ptr_;
  49. }
  50. static MapAllocator* fromDataPtr(const at::DataPtr&);
  51. static at::DataPtr makeDataPtr(
  52. c10::string_view filename,
  53. int flags,
  54. size_t size,
  55. size_t* actual_size_out);
  56. static at::DataPtr makeDataPtr(
  57. WithFd,
  58. const char* filename,
  59. int fd,
  60. int flags,
  61. size_t size,
  62. size_t* actual_size_out);
  63. // Closes the data. Helps us avoid destructor shenanigans
  64. virtual void close();
  65. // This is very dangerous. You have to redefine this destructor for each
  66. // subclass
  67. virtual ~MapAllocator();
  68. protected:
  69. bool closed_ = false;
  70. std::string filename_;
  71. int flags_ = 0;
  72. ptrdiff_t size_; /* mapped size */
  73. #ifdef _WIN32
  74. void* handle_;
  75. void* event_;
  76. std::string eventname_;
  77. #else
  78. int fd_ = -1;
  79. #endif
  80. void* base_ptr_ = nullptr;
  81. };
  82. // Base-from-member idiom
  83. struct TORCH_API RefcountedMapAllocatorArgCheck {
  84. RefcountedMapAllocatorArgCheck(int flags);
  85. };
  86. class TORCH_API RefcountedMapAllocator : private RefcountedMapAllocatorArgCheck,
  87. public MapAllocator {
  88. public:
  89. RefcountedMapAllocator(const char* filename, int flags, size_t size);
  90. RefcountedMapAllocator(
  91. WithFd,
  92. const char* filename,
  93. int fd,
  94. int flags,
  95. size_t size);
  96. static RefcountedMapAllocator* fromDataPtr(const at::DataPtr&);
  97. static at::DataPtr makeDataPtr(
  98. const char* filename,
  99. int flags,
  100. size_t size,
  101. size_t* actual_size_out);
  102. static at::DataPtr makeDataPtr(
  103. WithFd,
  104. const char* filename,
  105. int fd,
  106. int flags,
  107. size_t size,
  108. size_t* actual_size_out);
  109. void* data() const override;
  110. void incref();
  111. int decref();
  112. void close() override;
  113. ~RefcountedMapAllocator() override {
  114. RefcountedMapAllocator::close();
  115. }
  116. protected:
  117. void checkFlags();
  118. void initializeAlloc();
  119. };
  120. } // namespace at