Skip to content

Commit

Permalink
fvec: add additional constructors + rule-of-five
Browse files Browse the repository at this point in the history
  • Loading branch information
sebsura committed Feb 12, 2024
1 parent 6d88abc commit afd7a87
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion core/src/stored/backends/dedup/fvec.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ extern "C" {
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
}
#include <system_error>
#include <limits>

namespace dedup {
struct access {
Expand Down Expand Up @@ -58,6 +61,8 @@ template <typename T> class fvec : access {
static constexpr auto element_align = alignof(T);


fvec() = default;

fvec(rdonly_t, int fd, size_type initial_size = 0)
: fvec(fd, initial_size, PROT_READ)
{
Expand All @@ -68,6 +73,28 @@ template <typename T> class fvec : access {
{
}

fvec(bool readonly, int fd, size_type initial_size = 0)
: fvec(fd,
initial_size,
readonly ? (PROT_READ) : (PROT_READ | PROT_WRITE))
{
}

fvec(const fvec&) = delete;
fvec& operator=(const fvec&) = delete;

fvec(fvec&& other) : fvec{} { *this = std::move(other); }

fvec& operator=(fvec&& other)
{
std::swap(buffer, other.buffer);
std::swap(cap, other.cap);
std::swap(count, other.count);
std::swap(fd, other.fd);

return *this;
}

reference operator[](size_type idx) { return buffer[idx]; }

const_reference operator[](size_type idx) const { return buffer[idx]; }
Expand Down Expand Up @@ -134,7 +161,10 @@ template <typename T> class fvec : access {
if (ftruncate(fd, cap * element_size) != 0) { throw error("ftruncate"); }
}

~fvec() { munmap(buffer, cap * element_size); }
~fvec()
{
if (buffer) { munmap(buffer, cap * element_size); }
}

private:
T* buffer{nullptr};
Expand Down

0 comments on commit afd7a87

Please sign in to comment.