VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
sys_sharemem.h
浏览该文件的文档.
1/*
2 * Copyright (C) 2026 by Thun Lu. All rights reserved.
3 * Author: Thun Lu <thun.lu@zohomail.cn>
4 * Repo: https://github.com/thun-res/vlink
5 * _ __ __ _ __
6 * | | / / / / (_) ____ / /__
7 * | | / / / / / / / __ \ / //_/
8 * | |/ / / /___ / / / / / / / ,<
9 * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24/**
25 * @file sys_sharemem.h
26 * @brief Named cross-process shared memory region.
27 *
28 * @details
29 * @c SysSharemem wraps the POSIX shared-memory API (@c shm_open + @c mmap) on
30 * Linux/macOS and the Win32 @c CreateFileMapping / @c MapViewOfFile API on
31 * Windows to provide a named shared memory region that multiple processes can
32 * map into their address space simultaneously.
33 *
34 * Lifecycle:
35 * -# Construct a @c SysSharemem object.
36 * -# Creator process: call @c create(name, size) to allocate and map the region.
37 * -# Peer processes: call @c attach(name) to map the existing region.
38 * -# Access the raw memory via @c data().
39 * -# Call @c detach(force) to unmap. If @p force is @c true, POSIX backends
40 * also unlink the backing object from the OS namespace.
41 * -# The destructor calls @c detach() automatically with @p force == @c false.
42 *
43 * Access modes:
44 *
45 * | Mode | Description |
46 * | ----------- | ------------------------------------------------- |
47 * | kReadOnly | Maps with PROT_READ (no write access) |
48 * | kReadWrite | Maps with PROT_READ | PROT_WRITE (default) |
49 *
50 * @note
51 * - POSIX shared-memory names must start with '/' (e.g., @c "/vlink_cam0").
52 * On Windows any non-empty name is valid.
53 * - Shared memory is @b not initialised to zero by @c create(). The caller
54 * must initialise the region before use.
55 * - Concurrent access from multiple processes requires external synchronisation
56 * (e.g., @c SysSemaphore or a mutex in the shared region itself).
57 * - @c size() returns 0 when not attached.
58 *
59 * @par Example
60 * @code
61 * // Process A (creator):
62 * vlink::SysSharemem shm;
63 * shm.create("/vlink_frame", 1024 * 1024); // 1 MB
64 * auto* frame = static_cast<FrameHeader*>(shm.data());
65 * frame->seq = 0;
66 * sem.release(); // signal Process B
67 *
68 * // Process B (consumer):
69 * vlink::SysSharemem shm;
70 * shm.attach("/vlink_frame");
71 * sem.acquire();
72 * const auto* frame = static_cast<const FrameHeader*>(shm.data());
73 * process_frame(*frame);
74 * @endcode
75 */
76
77#pragma once
78
79#include <cstdint>
80#include <memory>
81#include <string>
82
83#include "./macros.h"
84
85namespace vlink {
86
87/**
88 * @class SysSharemem
89 * @brief Named cross-process shared memory backed by the OS IPC layer.
90 *
91 * @details
92 * Provides a typed @c data() pointer into a shared memory region that is
93 * accessible by any process that attaches the same name.
94 */
96 public:
97 /**
98 * @brief Access mode for the shared memory mapping.
99 */
100 enum Mode : uint8_t {
101 kReadOnly = 0, ///< Read-only mapping (PROT_READ)
102 kReadWrite = 1 ///< Read-write mapping (PROT_READ | PROT_WRITE)
103 };
104
105 /**
106 * @brief Default constructor. The object is not attached until @c create()
107 * or @c attach() is called.
108 */
110
111 /**
112 * @brief Destructor. Calls @c detach(true) if still attached.
113 */
115
116 /**
117 * @brief Creates a new named shared memory region of the given size and maps it.
118 *
119 * @details
120 * Fails if a region with the same name already exists.
121 * The region is @b not zero-initialised.
122 *
123 * @param name OS name for the shared memory object (POSIX: must start with '/').
124 * @param size Size of the region in bytes.
125 * @param mode Access mode (default: @c kReadWrite).
126 * @return @c true on success, @c false on failure.
127 */
128 bool create(const std::string& name, size_t size, Mode mode = kReadWrite);
129
130 /**
131 * @brief Attaches to an existing named shared memory region.
132 *
133 * @details
134 * The region must have been created by another process (or an earlier call
135 * to @c create()) before @c attach() can succeed.
136 *
137 * @param name OS name of the shared memory object.
138 * @param mode Access mode (default: @c kReadWrite).
139 * @return @c true on success, @c false if the region does not exist or
140 * access is denied.
141 */
142 bool attach(const std::string& name, Mode mode = kReadWrite);
143
144 /**
145 * @brief Unmaps the shared memory region and optionally unlinks the OS object.
146 *
147 * @details
148 * After @c detach(), @c is_attached() returns @c false and @c data() returns
149 * @c nullptr.
150 *
151 * @param force If @c true, POSIX backends unlink the backing OS object.
152 * Set @c false to merely unmap without destroying the object
153 * (other processes keep access). On Windows this only closes
154 * the local mapping handle. Default: @c true.
155 * @return @c true on success, @c false if not attached or unmap failed.
156 */
157 bool detach(bool force = true);
158
159 /**
160 * @brief Returns @c true if the region is currently mapped.
161 *
162 * @return @c true if attached (mapped), @c false otherwise.
163 */
164 [[nodiscard]] bool is_attached() const;
165
166 /**
167 * @brief Returns a writable pointer to the beginning of the shared memory region.
168 *
169 * @return Pointer to the mapped region, or @c nullptr if not attached or in
170 * read-only mode (use the @c const overload for read-only access).
171 */
172 [[nodiscard]] void* data();
173
174 /**
175 * @brief Returns a read-only pointer to the beginning of the shared memory region.
176 *
177 * @return Const pointer to the mapped region, or @c nullptr if not attached.
178 */
179 [[nodiscard]] const void* data() const;
180
181 /**
182 * @brief Returns the size of the mapped region in bytes.
183 *
184 * @return Region size in bytes, or 0 if not attached.
185 */
186 [[nodiscard]] size_t size() const;
187
188 private:
189 std::unique_ptr<struct SysSharememImpl> impl_;
190
192};
193
194} // namespace vlink
Platform-independent macro definitions for the VLink library.
#define VLINK_EXPORT
定义 macros.h:85
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
定义 macros.h:184