forb
shared_memory.hpp
1 //
2 // Created by gabriele on 04/11/18.
3 //
4 
5 #ifndef LIBFORB_SHARED_MEMORY_HPP
6 #define LIBFORB_SHARED_MEMORY_HPP
7 
8 #include <sys/types.h>
9 
10 #include <forb/stream/stream.hpp>
11 
12 namespace forb {
13  namespace streams {
14 
21  class shared_memory : public stream {
22  /* *********************************** ALIASES AND STATIC ATTRIBUTES ************************************ */
23  public:
26 
28  using key_t = ::key_t; // Defined in sys/types.h
29 
31  using id_t = int;
32 
33  private:
35  static constexpr int PRIVILEGES = 0666;
36 
40  struct shmem_data;
41 
42  /* ********************************************* ATTRIBUTES ********************************************* */
43  private:
45  key_t _key = 0;
46 
48  id_t _shmem_id = 0;
49 
51  shmem_data *_pointer = nullptr;
52 
54  bool _is_owner = false;
55 
56  /* ******************************************** CONSTRUCTORS ******************************************** */
57  public:
58 
61  shared_memory() = default;
62 
63  /**********************************************************************************************************/
64 
66  ~shared_memory() override {
67  close();
68  };
69 
71  shared_memory(const shared_memory &other) = delete;
72 
74  shared_memory &operator=(const shared_memory &other) = delete;
75 
77  shared_memory(shared_memory &&other) noexcept {
78  _move_attributes(other);
79  };
80 
82  shared_memory &operator=(shared_memory &&other) noexcept {
83  if (this != &other) // Probably useless pointer-assignment check
84  {
85  // If the same memory space is reused, the old shared
86  // memory must be released
87  close();
88 
89  _move_attributes(other);
90  }
91 
92  return *this;
93  };
94 
95  /**********************************************************************************************************/
96 
97  // Following static methods are implicitly inline
98 
100  static shared_memory get(key_t key) {
101  shared_memory shmem{};
102  shmem._get(key);
103  return shmem;
104  };
105 
107  static shared_memory create(size_t size) {
108  shared_memory shmem{};
109  shmem._init(size);
110  return shmem;
111  };
112 
113  /**********************************************************************************************************/
114  private:
116  void _move_attributes(shared_memory &other) noexcept;
117 
122  void _get(key_t key, size_t size = 0, int flags = 0);
123 
128  void _init(size_t size);
129 
130  public:
132  key_t get_key() noexcept {
133  return _key;
134  };
135 
137  void send(const void *buffer, size_t size) override;
138 
140  void recv(void *buffer, size_t size) override;
141 
143  void close() noexcept override;
144 
146  type get_type() const noexcept override {
147  return type::SHMEM;
148  };
149 
153  bool require_marshal() const override {
154  return false;
155  };
156 
157  };
158 
159  } // namespace streams
160 
161 } // namespace forb
162 
163 
164 #endif //LIBFORB_SHARED_MEMORY_HPP
shared_memory & operator=(shared_memory &&other) noexcept
This class supports move assignment.
Definition: shared_memory.hpp:82
shared_memory(shared_memory &&other) noexcept
This class supports move construction.
Definition: shared_memory.hpp:77
bool require_marshal() const override
Returns true if the execution platform requires marshalling before sending data on the socket...
Definition: shared_memory.hpp:153
Virtual class that represents a stream.
Definition: stream.hpp:69
This function implements a memcpy abstract that is valid also for volatile data.
Definition: base_skeleton.hpp:15
Definition of the private structure that will be allocated within the shared memory area...
Definition: shared_memory.cpp:37
Implementation of the forbcc::strams::stream API that uses a Linux Shared Memory to communicate...
Definition: shared_memory.hpp:21
::key_t key_t
The type of the key associated with the shared memory area.
Definition: shared_memory.hpp:28
type
The type of the stream.
Definition: stream.hpp:78
type get_type() const noexcept override
Returns the type of the stream.
Definition: shared_memory.hpp:146
void send(const void *buffer, size_t size) override
Blocking call that fills the shared memory area with size data from the given buffer.
Definition: shared_memory.cpp:230
key_t get_key() noexcept
Returns the key of the shared memory.
Definition: shared_memory.hpp:132
std::size_t size_t
An unsigned size type.
Definition: stream.hpp:72
void close() noexcept override
Closes the socket, called by the virtual destructor.
Definition: shared_memory.cpp:354
void recv(void *buffer, size_t size) override
Blocking call that fills buffer with size data received through the shared_memory area...
Definition: shared_memory.cpp:292
shared_memory & operator=(const shared_memory &other)=delete
This class does not support copy assignment.
shared_memory()=default
Default constructor, creates an empty shared memory object, with no associated shared memory area...
stream::size_t index_t
The type of the indexes used within the shared memory area.
Definition: shared_memory.hpp:25
~shared_memory() override
Virtual destructor that closes shared memory (if open).
Definition: shared_memory.hpp:66
static shared_memory create(size_t size)
Creates a new shared memory area with the given size.
Definition: shared_memory.hpp:107
int id_t
The type of the ID of the shared memory area.
Definition: shared_memory.hpp:31