VLink  2.0.0
A high-performance communication middleware
vlink::SysSharemem Class Referencefinal

Named cross-process shared-memory region with read-only or read-write mappings. More...

#include <sys_sharemem.h>

Collaboration diagram for vlink::SysSharemem:

Public Types

enum  Mode : uint8_t { kReadOnly = 0 , kReadWrite = 1 }
 Access mode applied to the mapping. More...
 

Public Member Functions

 SysSharemem ()
 Constructs a wrapper in the detached state. More...
 
 ~SysSharemem ()
 Destructor. Invokes detach(false) when the wrapper is still attached. More...
 
bool create (const std::string &name, size_t size, Mode mode=kReadWrite)
 Creates and maps a new shared-memory region of size bytes under name. More...
 
bool attach (const std::string &name, Mode mode=kReadWrite)
 Attaches to an existing shared-memory region previously created with the same name. More...
 
bool detach (bool force=true)
 Unmaps the region and optionally unlinks the kernel name. More...
 
bool is_attached () const
 Reports whether the wrapper currently owns a mapping. More...
 
void * data ()
 Returns a writable pointer to the start of the mapping. More...
 
const void * data () const
 Returns a read-only pointer to the start of the mapping. More...
 
size_t size () const
 Returns the size of the mapping in bytes. More...
 

Detailed Description

Named cross-process shared-memory region with read-only or read-write mappings.

Manages one mapping at a time; call detach() before re-attaching to a different region.

Member Enumeration Documentation

◆ Mode

enum vlink::SysSharemem::Mode : uint8_t

Access mode applied to the mapping.

Enumerator names mirror the POSIX O_RDONLY / O_RDWR open flags. On Windows the implementation translates these to the equivalent CreateFileMapping / MapViewOfFile page-protection constants; the observable behaviour is identical across platforms.

Enumerator
kReadOnly 

Read-only mapping; writes through data() are undefined.

kReadWrite 

Read-write mapping (default).

Constructor & Destructor Documentation

◆ SysSharemem()

vlink::SysSharemem::SysSharemem ( )

Constructs a wrapper in the detached state.

◆ ~SysSharemem()

vlink::SysSharemem::~SysSharemem ( )

Destructor. Invokes detach(false) when the wrapper is still attached.

Member Function Documentation

◆ attach()

bool vlink::SysSharemem::attach ( const std::string &  name,
Mode  mode = kReadWrite 
)

Attaches to an existing shared-memory region previously created with the same name.

Parameters
nameKernel-name of the region.
modeAccess mode for the mapping. Default: kReadWrite.
Returns
true on success.

◆ create()

bool vlink::SysSharemem::create ( const std::string &  name,
size_t  size,
Mode  mode = kReadWrite 
)

Creates and maps a new shared-memory region of size bytes under name.

Fails when a region with name already exists in the kernel namespace. The supported POSIX/Windows backends provide zero-initialised storage.

Parameters
nameKernel-name for the region. POSIX must begin with '/'.
sizeRegion size in bytes.
modeAccess mode for the mapping. Default: kReadWrite.
Returns
true on success.

◆ data() [1/2]

void* vlink::SysSharemem::data ( )

Returns a writable pointer to the start of the mapping.

Returns
Mapping base pointer; nullptr when detached or in read-only mode.

◆ data() [2/2]

const void* vlink::SysSharemem::data ( ) const

Returns a read-only pointer to the start of the mapping.

Returns
Const mapping base pointer; nullptr when detached.

◆ detach()

bool vlink::SysSharemem::detach ( bool  force = true)

Unmaps the region and optionally unlinks the kernel name.

Parameters
forceWhen true, POSIX backends call shm_unlink so the region is removed from the kernel namespace. Other processes that still hold mappings keep them. Windows merely closes the local mapping handle. Default: true.
Returns
true on success.

◆ is_attached()

bool vlink::SysSharemem::is_attached ( ) const

Reports whether the wrapper currently owns a mapping.

Returns
true after a successful create or attach.

◆ size()

size_t vlink::SysSharemem::size ( ) const

Returns the size of the mapping in bytes.

Returns
Region size; 0 when detached.

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