VLink  2.0.0
A high-performance communication middleware
sys_sharemem.h File Reference

Named cross-process shared-memory region. More...

#include <cstdint>
#include <memory>
#include <string>
#include "./macros.h"
Include dependency graph for sys_sharemem.h:

Go to the source code of this file.

Classes

class  vlink::SysSharemem
 Named cross-process shared-memory region with read-only or read-write mappings. More...
 

Namespaces

 

Detailed Description

Named cross-process shared-memory region.

vlink::SysSharemem wraps the platform's shared-memory API: shm_open + mmap on POSIX targets, CreateFileMapping + MapViewOfFile on Windows. The region appears in the kernel namespace under a caller-chosen name and any process that knows the name can map it into its own address space.

Memory layout exposed through data() and size():

*   +---------------------------------------------------+
*   | byte 0                                            |
*   |   user payload (size = total bytes returned by    |
*   |   size())                                         |
*   | byte size-1                                       |
*   +---------------------------------------------------+
* 

No internal header is reserved; the entire mapping is user payload. Place any synchronisation primitive (mutex, atomic counters, ring buffer indices) in the user region or in a companion SysSemaphore.

Lifecycle states observed by the wrapper:

State Entered by is_attached() data()
Detached Construction, detach false nullptr
Created create() true Pointer to mapping
Attached attach() true Pointer to mapping

Access modes accepted by create() and attach():

Mode POSIX flag Effect
kReadOnly O_RDONLY Maps with PROT_READ
kReadWrite O_RDWR Maps with PROT_READ + PROT_WRITE
Note
  • POSIX names must begin with '/'; Windows accepts arbitrary non-empty strings.
  • The kernel zero-fills newly created regions on the supported POSIX/Windows backends.
  • Cross-process consistency requires external synchronisation.
Example
// Process A (creator)
shm.create("/vlink_frame", 1024 * 1024);
auto* frame = static_cast<FrameHeader*>(shm.data());
frame->seq = 0;
// Process B (consumer)
shm.attach("/vlink_frame");
const auto* frame = static_cast<const FrameHeader*>(shm.data());
process_frame(*frame);
shm.detach();