forked from dsroche/nbdcpp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ramdisk.cpp
86 lines (67 loc) · 2.59 KB
/
ramdisk.cpp
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
#include "nbdserv.h"
#include <vector>
#include <array>
#include <algorithm>
#include <sstream>
// modeled on DevExample from nbdserv.h
// stores the blocks as a vector of block-sized arrays
template <unsigned BS=4096>
class RamDisk {
private:
std::vector<std::array<nbdcpp::byte, BS>> _data;
public:
RamDisk(size_t nblocks) :_data(nblocks) { }
// should return false if some unrecoverable error has occurred
bool good() const { return true; }
// number of bytes per block
static constexpr size_t blocksize() { return BS; }
// number of blocks in device
size_t numblocks() const { return _data.size(); }
// read a single block from the device
// index is the index of the block
// data is a pointer to an array of size at last blocksize()
void read(size_t index, nbdcpp::byte* data) const {
std::copy(_data.at(index).begin(), _data.at(index).end(), data);
}
// write a single block to the device
// index is the index of the block
// data is a pointer to an array of size at last blocksize()
void write(size_t index, const nbdcpp::byte* data) {
std::copy(data, data + BS, _data.at(index).begin());
}
// read multiple blocks at once
void multiread(size_t index, size_t count, nbdcpp::byte* data) const {
nbdcpp::multiread_default(*this, index, count, data);
}
// write multiple blocks at once
void multiwrite(size_t index, size_t count, const nbdcpp::byte* data) {
nbdcpp::multiwrite_default(*this, index, count, data);
}
// returns true iff the flush operation is supported
constexpr bool flushes() const { return false; }
// Syncs all pending read/write ops to any underlying device
void flush() const { }
// returns true iff the trim operation is supported
constexpr bool trims() const { return false; }
// Performs a DISCARD/TRIM operation (optional)
void trim(size_t, size_t) { }
};
using namespace std;
using namespace nbdcpp;
int main(int argc, char** argv) {
auto usage = [argv]() {
errout() << "usage: " << argv[0] << " size" << nbd_usage_line() << "\n";
errout() << " Provides a ramdisk over an NBD server.\n";
errout() << " size is the size of the ramdisk in KB\n";
nbd_usage_doc(errout());
};
// size must be the first command line argument
size_t size;
if (argc < 2 || !(istringstream(argv[1]) >> size) || size <= 0) {
usage();
return 1;
}
size_t numblocks = 1 + (size*1024 - 1) / RamDisk<>::blocksize();
// everything else is taken care of by nbdcpp
return nbdcpp_main<RamDisk<>>(argc, argv, 2, usage, numblocks);
}