Contur 2
Educational OS kernel simulator
Loading...
Searching...
No Matches
shared_memory.h
Go to the documentation of this file.
1
3
4#pragma once
5
6#include <memory>
7#include <string>
8
9#include "contur/core/types.h"
10
12
13namespace contur {
14
19 class SharedMemory final : public IIpcChannel
20 {
21 public:
25 explicit SharedMemory(std::string name, std::size_t bytes);
26
28 ~SharedMemory() override;
29
31 SharedMemory(const SharedMemory &) = delete;
32
37
39 SharedMemory &operator=(SharedMemory &&) noexcept;
40
43 [[nodiscard]] Result<std::size_t> write(std::span<const std::byte> data) override;
44
47 [[nodiscard]] Result<std::size_t> read(std::span<std::byte> buffer) override;
48
50 void close() override;
51
53 [[nodiscard]] bool isOpen() const noexcept override;
54
56 [[nodiscard]] std::string_view name() const noexcept override;
57
61 [[nodiscard]] Result<void> attach(ProcessId pid);
62
66 [[nodiscard]] Result<void> detach(ProcessId pid);
67
69 [[nodiscard]] bool isAttached(ProcessId pid) const noexcept;
70
72 [[nodiscard]] std::size_t attachedCount() const noexcept;
73
75 [[nodiscard]] std::size_t size() const noexcept;
76
77 private:
78 struct Impl;
79 std::unique_ptr<Impl> impl_;
80 };
81
82} // namespace contur
Common interface for IPC channels.
A result type that holds either a success value of type T or an ErrorCode.
Definition error.h:104
bool isAttached(ProcessId pid) const noexcept
Returns true if process is attached.
void close() override
Closes the region, clears attachments, and resets data.
SharedMemory(const SharedMemory &)=delete
Copy construction is disabled.
Result< std::size_t > write(std::span< const std::byte > data) override
Writes bytes into the beginning of the shared region.
std::size_t size() const noexcept
Region size in bytes.
Result< std::size_t > read(std::span< std::byte > buffer) override
Reads bytes from the beginning of the shared region.
SharedMemory(SharedMemory &&) noexcept
Move-constructs shared-memory state.
Result< void > detach(ProcessId pid)
Detaches a process from this region.
bool isOpen() const noexcept override
Returns whether the region is open.
std::size_t attachedCount() const noexcept
Number of attached processes.
std::string_view name() const noexcept override
Region name.
~SharedMemory() override
Destroys shared-memory channel.
std::unique_ptr< Impl > impl_
Result< void > attach(ProcessId pid)
Attaches a process to this region.
SharedMemory & operator=(const SharedMemory &)=delete
Copy assignment is disabled.
SharedMemory(std::string name, std::size_t bytes)
Creates a named shared-memory region.
IIpcChannel interface for inter-process communication channels.
Definition block.h:15
std::uint32_t ProcessId
Unique identifier for a process.
Definition types.h:12
Common type aliases, sentinel constants, and forward declarations used throughout the Contur 2 kernel...