POSIX Shared Memory Data Structures 1.0
High-performance lock-free data structures for inter-process communication
Loading...
Searching...
No Matches
posix_shm_impl< TableType > Class Template Reference

POSIX shared memory wrapper with RAII and reference counting. More...

#include <posix_shm.h>

Public Types

using table_type = TableType
 Type alias for the table type parameter.
 

Public Member Functions

 posix_shm_impl (const std::string &name, size_t size=0)
 Create or attach to a POSIX shared memory segment.
 
 ~posix_shm_impl ()
 Destructor with automatic cleanup.
 
void * get_base_addr () const
 Get base address for user data (after header)
 
size_t get_total_size () const
 Get usable size for data (excluding header)
 
TableType * get_table ()
 Get mutable pointer to metadata table.
 
const TableType * get_table () const
 Get const pointer to metadata table.
 
int get_ref_count () const
 Get current reference count.
 
bool unlink ()
 Explicitly unlink the shared memory segment.
 
 posix_shm_impl (const posix_shm_impl &)=delete
 Deleted copy constructor (non-copyable)
 
posix_shm_imploperator= (const posix_shm_impl &)=delete
 Deleted copy assignment (non-copyable)
 

Detailed Description

template<typename TableType = shm_table>
class posix_shm_impl< TableType >

POSIX shared memory wrapper with RAII and reference counting.

This class provides:

  • Automatic creation/attachment to shared memory segments
  • Multi-process safe reference counting
  • Embedded metadata table for structure discovery
  • RAII-style automatic cleanup
  • Zero-overhead access to shared data

The shared memory layout:

[Header: ref_count + table] [User data...]
Template Parameters
TableTypeThe metadata table implementation (default: shm_table)
Note
Thread-safe reference counting via std::atomic
Last process to detach automatically unlinks the segment
Warning
Uses stack allocation - no individual deallocation supported
Example:
// Process 1: Create and populate
posix_shm shm("simulation", 10*1024*1024);
shm_array<float> data(shm, "sensor_data", 1000);
// Process 2: Attach and read
posix_shm shm("simulation", 0); // size=0 means attach existing
auto* data = shm_array<float>::open(shm, "sensor_data");
Fixed-size array in shared memory with zero-overhead access.
Definition shm_array.h:63
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator.
Definition shm_array.h:278

Definition at line 61 of file posix_shm.h.

Member Typedef Documentation

◆ table_type

template<typename TableType = shm_table>
using posix_shm_impl< TableType >::table_type = TableType

Type alias for the table type parameter.

Definition at line 330 of file posix_shm.h.

Constructor & Destructor Documentation

◆ posix_shm_impl() [1/2]

template<typename TableType = shm_table>
posix_shm_impl< TableType >::posix_shm_impl ( const std::string &  name,
size_t  size = 0 
)
inline

Create or attach to a POSIX shared memory segment.

Parameters
nameUnique identifier for the shared memory segment
sizeSize in bytes (0 = attach to existing segment)
Exceptions
std::runtime_errorif shm_open fails
std::runtime_errorif mmap fails
std::runtime_errorif ftruncate fails (when creating)
Note
If size=0, attempts to attach to existing segment
If size>0, creates new segment or attaches to existing
Name should not include /dev/shm/ prefix
Thread Safety:
Constructor is thread-safe when multiple processes/threads attempt to create/attach simultaneously.
Example:
// Create new 10MB segment
posix_shm shm("my_sim", 10*1024*1024);
// Attach to existing segment
posix_shm shm2("my_sim", 0);

Definition at line 121 of file posix_shm.h.

◆ ~posix_shm_impl()

template<typename TableType = shm_table>
posix_shm_impl< TableType >::~posix_shm_impl ( )
inline

Destructor with automatic cleanup.

Performs the following:

  1. Decrements reference count atomically
  2. Unmaps memory region
  3. Closes file descriptor
Note
Exception-safe: never throws
Shared memory persists until explicitly unlinked

Definition at line 204 of file posix_shm.h.

◆ posix_shm_impl() [2/2]

template<typename TableType = shm_table>
posix_shm_impl< TableType >::posix_shm_impl ( const posix_shm_impl< TableType > &  )
delete

Deleted copy constructor (non-copyable)

Member Function Documentation

◆ get_base_addr()

template<typename TableType = shm_table>
void * posix_shm_impl< TableType >::get_base_addr ( ) const
inline

Get base address for user data (after header)

Returns
Pointer to beginning of user data area
Note
Points to the table, user allocations come after
Address is stable for lifetime of mapping
Performance:
O(1) - Simple pointer arithmetic

Definition at line 222 of file posix_shm.h.

◆ get_ref_count()

template<typename TableType = shm_table>
int posix_shm_impl< TableType >::get_ref_count ( ) const
inline

Get current reference count.

Returns
Number of processes attached to this segment
Note
Thread-safe via atomic load
Useful for debugging and monitoring
Example:
if (shm.get_ref_count() == 1) {
// We're the only process attached
}

Definition at line 292 of file posix_shm.h.

◆ get_table() [1/2]

template<typename TableType = shm_table>
TableType * posix_shm_impl< TableType >::get_table ( )
inline

Get mutable pointer to metadata table.

Returns
Pointer to embedded table for structure registration
Note
Use this to register/discover data structures
Thread-safety depends on TableType implementation
Example:
auto* table = shm.get_table();
table->add("my_array", offset, size);

Definition at line 260 of file posix_shm.h.

◆ get_table() [2/2]

template<typename TableType = shm_table>
const TableType * posix_shm_impl< TableType >::get_table ( ) const
inline

Get const pointer to metadata table.

Returns
Const pointer for read-only table access
See also
get_table() for mutable access

Definition at line 272 of file posix_shm.h.

◆ get_total_size()

template<typename TableType = shm_table>
size_t posix_shm_impl< TableType >::get_total_size ( ) const
inline

Get usable size for data (excluding header)

Returns
Size in bytes available for user allocations
Note
This is less than the size passed to constructor
Header overhead is sizeof(atomic<int>) + sizeof(TableType)
Example:
posix_shm shm("test", 1024*1024); // Request 1MB
size_t usable = shm.get_total_size(); // ~1MB - header

Definition at line 241 of file posix_shm.h.

◆ operator=()

template<typename TableType = shm_table>
posix_shm_impl & posix_shm_impl< TableType >::operator= ( const posix_shm_impl< TableType > &  )
delete

Deleted copy assignment (non-copyable)

◆ unlink()

template<typename TableType = shm_table>
bool posix_shm_impl< TableType >::unlink ( )
inline

Explicitly unlink the shared memory segment.

Returns
true if successfully unlinked, false otherwise
Note
This removes the shared memory from the system
Other processes with the segment mapped can continue using it
Never throws (safe for cleanup code)
Warning
This will make the shared memory inaccessible to new processes
Example:
posix_shm shm("test", 1024);
// ... use shared memory ...
shm.unlink(); // Explicitly remove from system

Definition at line 314 of file posix_shm.h.


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