Named cross-process shared memory backed by the OS IPC layer.
More...
#include <sys_sharemem.h>
|
| | SysSharemem () |
| | Default constructor. The object is not attached until create() or attach() is called.
|
| | ~SysSharemem () |
| | Destructor. Calls detach(true) if still attached.
|
| bool | create (const std::string &name, size_t size, Mode mode=kReadWrite) |
| | Creates a new named shared memory region of the given size and maps it.
|
| bool | attach (const std::string &name, Mode mode=kReadWrite) |
| | Attaches to an existing named shared memory region.
|
| bool | detach (bool force=true) |
| | Unmaps the shared memory region and optionally unlinks the OS object.
|
| bool | is_attached () const |
| | Returns true if the region is currently mapped.
|
| void * | data () |
| | Returns a writable pointer to the beginning of the shared memory region.
|
| const void * | data () const |
| | Returns a read-only pointer to the beginning of the shared memory region.
|
| size_t | size () const |
| | Returns the size of the mapped region in bytes.
|
Named cross-process shared memory backed by the OS IPC layer.
Provides a typed data() pointer into a shared memory region that is accessible by any process that attaches the same name.
◆ Mode
Access mode for the shared memory mapping.
| Enumerator |
|---|
| kReadOnly | Read-only mapping (PROT_READ).
|
| kReadWrite | Read-write mapping (PROT_READ | PROT_WRITE).
|
◆ SysSharemem()
| vlink::SysSharemem::SysSharemem |
( |
| ) |
|
Default constructor. The object is not attached until create() or attach() is called.
◆ ~SysSharemem()
| vlink::SysSharemem::~SysSharemem |
( |
| ) |
|
Destructor. Calls detach(true) if still attached.
◆ attach()
| bool vlink::SysSharemem::attach |
( |
const std::string & | name, |
|
|
Mode | mode = kReadWrite ) |
Attaches to an existing named shared memory region.
The region must have been created by another process (or an earlier call to create()) before attach() can succeed.
- Parameters
-
| name | OS name of the shared memory object. |
| mode | Access mode (default: kReadWrite). |
- Returns
true on success, false if the region does not exist or access is denied.
◆ create()
| bool vlink::SysSharemem::create |
( |
const std::string & | name, |
|
|
size_t | size, |
|
|
Mode | mode = kReadWrite ) |
Creates a new named shared memory region of the given size and maps it.
Fails if a region with the same name already exists. The region is not zero-initialised.
- Parameters
-
| name | OS name for the shared memory object (POSIX: must start with '/'). |
| size | Size of the region in bytes. |
| mode | Access mode (default: kReadWrite). |
- Returns
true on success, false on failure.
◆ data() [1/2]
| void * vlink::SysSharemem::data |
( |
| ) |
|
|
nodiscard |
Returns a writable pointer to the beginning of the shared memory region.
- Returns
- Pointer to the mapped region, or
nullptr if not attached or in read-only mode (use the const overload for read-only access).
◆ data() [2/2]
| const void * vlink::SysSharemem::data |
( |
| ) |
const |
|
nodiscard |
Returns a read-only pointer to the beginning of the shared memory region.
- Returns
- Const pointer to the mapped region, or
nullptr if not attached.
◆ detach()
| bool vlink::SysSharemem::detach |
( |
bool | force = true | ) |
|
Unmaps the shared memory region and optionally unlinks the OS object.
After detach(), is_attached() returns false and data() returns nullptr.
- Parameters
-
| force | If true, POSIX backends unlink the backing OS object. Set false to merely unmap without destroying the object (other processes keep access). On Windows this only closes the local mapping handle. Default: true. |
- Returns
true on success, false if not attached or unmap failed.
◆ is_attached()
| bool vlink::SysSharemem::is_attached |
( |
| ) |
const |
|
nodiscard |
Returns true if the region is currently mapped.
- Returns
true if attached (mapped), false otherwise.
◆ size()
| size_t vlink::SysSharemem::size |
( |
| ) |
const |
|
nodiscard |
Returns the size of the mapped region in bytes.
- Returns
- Region size in bytes, or 0 if not attached.
The documentation for this class was generated from the following file: