Debian Codegraph @

modules/lsp-runtime-lib/include/lsp-plug.in/ipc/SharedMem.h

raw
1 /*
2 * Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2024 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 21 апр. 2024 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #ifndef LSP_PLUG_IN_IPC_SHAREDMEM_H_
23 #define LSP_PLUG_IN_IPC_SHAREDMEM_H_
24
25 #include <lsp-plug.in/runtime/version.h>
26
27 #include <lsp-plug.in/common/status.h>
28 #include <lsp-plug.in/runtime/LSPString.h>
29
30 namespace lsp
31 {
32 namespace ipc
33 {
34 struct shared_context_t;
35
36 /**
37 * Named global shared memory segment for inter-process communication.
38 */
39 class SharedMem
40 {
41 public:
42 enum mode_t
43 {
44 SHM_READ = 1 << 0, // Read access
45 SHM_WRITE = 1 << 1, // Write access
46 SHM_EXEC = 1 << 2, // Execute access
47 SHM_CREATE = 1 << 3, // Create segment if it does not exist
48 SHM_PERSIST = 1 << 4, // Persistent flag: do not remove associated file
49
50 SHM_RW = SHM_READ | SHM_WRITE,
51 SHM_RWX = SHM_READ | SHM_WRITE | SHM_EXEC
52 };
53
54 private:
55 mutable shared_context_t *pContext;
56
57 protected:
58 void release_context();
59 bool allocate_context();
60
61 protected:
62 static status_t open_context(shared_context_t *ctx, size_t mode, size_t size);
63 static status_t unlink_file(shared_context_t *ctx);
64 static status_t close_context(shared_context_t *ctx);
65 static status_t unmap_context(shared_context_t *ctx);
66 static status_t map_context(shared_context_t *ctx, size_t offset, size_t size);
67 static status_t close_file(shared_context_t *ctx);
68
69 public:
70 SharedMem();
71 SharedMem(const SharedMem & src);
72 SharedMem(SharedMem && src);
73 ~SharedMem();
74 SharedMem & operator = (const SharedMem & src);
75 SharedMem & operator = (SharedMem && src);
76
77 public:
78 /**
79 * Create shared segment with unique name
80 * @param name pointer to store generated shared memory segment name
81 * @param postfix postfix added to the name, can be NULL
82 * @param mode open mode, the SHM_CREATE flag is interpreted as being always set
83 * @param size size of the shared memory segment
84 * @return status of operation
85 */
86 status_t create(LSPString *name, const LSPString *postfix, size_t mode, size_t size);
87
88 /**
89 * Create shared segment with unique name
90 * @param name pointer to store shared memory segment name
91 * @param postfix postfix added to the name, can be NULL
92 * @param mode open mode, the SHM_CREATE flag is interpreted as being always set
93 * @param size size of the shared memory segment
94 * @return status of operation
95 */
96 status_t create(LSPString *name, const char *postfix, size_t mode, size_t size);
97
98 /**
99 * Create named shared memory segment and map to memory
100 * @param name shared memory segment identifier (UTF-8)
101 * @param mode memory access mode
102 * @param size the size of shared memory segment if it should be created
103 * @return status of operation
104 */
105 status_t open(const char *name, size_t mode, size_t size);
106
107 /**
108 * Create named shared memory segment and map to memory
109 * @param name shared memory segment identifier
110 * @param mode memory access mode
111 * @param size the size of shared memory segment if it should be created
112 * @return status of operation
113 */
114 status_t open(const LSPString *name, size_t mode, size_t size);
115
116 /**
117 * Close named shared memory segment
118 * @return status of operation
119 */
120 status_t close();
121
122 /**
123 * Map shared segment to the specified offset. If the segment already was mapped,
124 * the previous mapping is released on success.
125 * @param offset the offset from the beginning of the segment
126 * @param size the number of bytes to map
127 * @return status of operation
128 */
129 status_t map(size_t offset, size_t size);
130
131 /**
132 * Unmap current memory mapping
133 * @return status of operation
134 */
135 status_t unmap();
136
137 public:
138 /**
139 * Check if shared memory segment is opened
140 * @return true if shared memory segment is opened
141 */
142 bool opened() const;
143
144 /**
145 * Check if shared memory segment is opened
146 * @return true if shared memory segment is opened
147 */
148 bool mapped() const;
149
150 /**
151 * Return the pointer to the mapped memory
152 * @return pointer to the mapped memory or NULL if shared memory segment is not mapped
153 */
154 void *data();
155
156 /**
157 * Return the pointer to the mapped memory
158 * @return pointer to the mapped memory or NULL if shared memory segment is not mapped
159 */
160 const void *data() const;
161
162 /**
163 * Return mapping offset
164 * @return mapping offset or negative error code
165 */
166 ssize_t map_offset() const;
167
168 /**
169 * Return mapping size
170 * @return mapping size or negative error code
171 */
172 ssize_t map_size() const;
173
174 /**
175 * Return size of the whole shared memory segment
176 * @return the size of the whole shared memory segment
177 */
178 wssize_t size() const;
179 };
180
181 } /* namespace ipc */
182 } /* namespace lsp */
183
184
185 #endif /* LSP_PLUG_IN_IPC_SHAREDMEM_H_ */