VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
vlink::SysSharemem Class Referencefinal

Named cross-process shared memory backed by the OS IPC layer. More...

#include <sys_sharemem.h>

Collaboration diagram for vlink::SysSharemem:

Public Types

enum  Mode : uint8_t { kReadOnly = 0 , kReadWrite = 1 }
 Access mode for the shared memory mapping. More...

Public Member Functions

 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.

Detailed Description

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.

Member Enumeration Documentation

◆ Mode

enum vlink::SysSharemem::Mode : uint8_t

Access mode for the shared memory mapping.

Enumerator
kReadOnly 

Read-only mapping (PROT_READ).

kReadWrite 

Read-write mapping (PROT_READ | PROT_WRITE).

Constructor & Destructor Documentation

◆ SysSharemem()

vlink::SysSharemem::SysSharemem ( )

Default constructor. The object is not attached until create() or attach() is called.

Here is the caller graph for this function:

◆ ~SysSharemem()

vlink::SysSharemem::~SysSharemem ( )

Destructor. Calls detach(true) if still attached.

Member Function Documentation

◆ 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
nameOS name of the shared memory object.
modeAccess 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
nameOS name for the shared memory object (POSIX: must start with '/').
sizeSize of the region in bytes.
modeAccess mode (default: kReadWrite).
Returns
true on success, false on failure.
Here is the call graph for this function:

◆ 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
forceIf 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.
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: