-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buffer.hpp
92 lines (76 loc) · 2.48 KB
/
Buffer.hpp
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
/**
* @file Buffer.hpp
* @author ottojo
* @date 5/25/21
* Description here TODO
*/
#ifndef INFINIBAND_BUFFER_HPP
#define INFINIBAND_BUFFER_HPP
#include <memory>
#include <cassert>
#include <infiniband/verbs.h>
#include <fmt/format.h>
#include <gsl/gsl>
template<typename T>
class Buffer {
public:
explicit Buffer(std::size_t size, ibv_pd *protectionDomain) :
_size(size),
_data(new T[size]),
memoryRegionMetadata(
ibv_reg_mr(protectionDomain, _data.get(), size * sizeof(T),
IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE)) {
if (memoryRegionMetadata == nullptr) {
throw std::runtime_error{fmt::format("Registering send memory region failed: {}", strerror(errno))};
}
};
~Buffer() {
if (memoryRegionMetadata != nullptr) {
// This case occurs if this object was moved-from
ibv_dereg_mr(memoryRegionMetadata);
}
}
Buffer(const Buffer &) = delete;
Buffer(Buffer &&rhs) noexcept {
_size = rhs._size;
_data = std::move(rhs._data);
memoryRegionMetadata = rhs.memoryRegionMetadata;
rhs.memoryRegionMetadata = nullptr;
};
Buffer &operator=(const Buffer &) = delete;
Buffer &operator=(Buffer &&rhs) noexcept {
if (this != &rhs) {
_size = rhs._size;
rhs._size = 0;
_data = std::move(rhs._data);
if (memoryRegionMetadata != nullptr) {
// This case occurs if this object was moved-from
ibv_dereg_mr(memoryRegionMetadata);
}
memoryRegionMetadata = rhs.memoryRegionMetadata;
rhs.memoryRegionMetadata = nullptr;
}
};
T &at(std::ptrdiff_t i) {
assert(i < _size);
return _data[i];
}
const T &at(std::ptrdiff_t i) const {
assert(i < _size);
return _data[i];
}
T *data() {
return _data.get();
}
ibv_mr *getMR() {
return memoryRegionMetadata;
}
[[nodiscard]] std::size_t size() const {
return _size;
}
private:
std::size_t _size;
std::unique_ptr<T[]> _data;
gsl::owner<ibv_mr *> memoryRegionMetadata;
};
#endif //INFINIBAND_BUFFER_HPP