about summary refs log tree commit diff
path: root/fakefbdev/SharedBuffer.cpp
blob: fbc312122b054275953fdd6dad18c259e76d1f60 (plain)
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
#include "SharedBuffer.h"

#include <cerrno>
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

SharedFB::SharedFB(const char* path) : fd(shm_open(path, O_RDWR, 0755)) {
  if (fd == -1) {
    fd = shm_open(path, O_RDWR | O_CREAT, 0755);
  }

  if (fd < 0) {
    perror("Can't open shm");
    return;
  }

  ftruncate(fd, fb_size);
  mem = (uint16_t*)mmap(nullptr, fb_size, PROT_WRITE, MAP_SHARED, fd, 0);
}

SharedFB::~SharedFB() {
  if (mem != nullptr) {
    munmap(mem, fb_size);
  }
}

SharedFB::SharedFB(SharedFB&& other) noexcept : fd(other.fd), mem(other.mem) {
  other.fd = -1;
  other.mem = nullptr;
}

SharedFB&
SharedFB::operator=(SharedFB&& other) noexcept {
  // TODO: release
  this->fd = other.fd;
  this->mem = other.mem;

  other.fd = -1;
  other.mem = nullptr;

  return *this;
}