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