SharedStorage/core/SharedMemoryManager.h
2025-04-20 22:29:57 +03:00

51 lines
1.1 KiB
C++

//
// Created by Kirill Zhukov on 20.04.2025.
//
#ifndef SHAREDMEMORYMANAGER_H
#define SHAREDMEMORYMANAGER_H
#include <string>
#include <stdexcept>
#include <fcntl.h> // O_CREAT, O_RDWR
#include <unistd.h> // ftruncate, close
#include <sys/mman.h> // mmap, munmap, shm_open, shm_unlink
#include <cstring>
#include <cstdint>
namespace usub::core
{
class SharedMemoryManager
{
public:
SharedMemoryManager(const std::string& name, size_t size, bool create_new = true);
~SharedMemoryManager();
[[nodiscard]] void* base_ptr() const noexcept;
[[nodiscard]] size_t size() const noexcept;
[[nodiscard]] const std::string& name() const noexcept;
void destroy();
static bool exists(const std::string& name);
[[nodiscard]] bool needs_init() const noexcept;
private:
void create();
void open();
private:
std::string shm_name;
size_t shm_size;
int shm_fd;
void* shm_ptr;
};
} // namespace usub::utils
#endif //SHAREDMEMORYMANAGER_H