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

Fixed-size array in shared memory with zero-overhead access. More...

#include <shm_array.h>

+ Inheritance diagram for shm_array< T, TableType >:
+ Collaboration diagram for shm_array< T, TableType >:

Public Types

STL Type Definitions
using value_type = T
 Element type.
 
using size_type = size_t
 Size type.
 
using difference_type = std::ptrdiff_t
 Iterator difference.
 
using reference = T &
 Reference type.
 
using const_reference = const T &
 Const reference.
 
using pointer = T *
 Pointer type.
 
using const_pointer = const T *
 Const pointer.
 
using iterator = T *
 Iterator type (raw pointer)
 
using const_iterator = const T *
 Const iterator.
 
using reverse_iterator = std::reverse_iterator< iterator >
 Reverse iterator.
 
using const_reverse_iterator = std::reverse_iterator< const_iterator >
 Const reverse.
 

Public Member Functions

template<typename ShmType >
 shm_array (ShmType &shared_mem, std::string_view name, size_t count=0)
 Create new array or attach to existing array by name.
 
void initialize (std::string_view name, size_t count=0)
 Internal initialization helper.
 
Tat (size_t pos)
 Bounds-checked element access.
 
const Tat (size_t pos) const
 Const version of at()
 
Tfront ()
 Access first element.
 
const Tfront () const
 Access first element (const)
 
Tback ()
 Access last element.
 
const Tback () const
 Access last element (const)
 
bool empty () const noexcept
 Check if array is empty.
 
size_t size () const noexcept
 Get number of elements.
 
void fill (const T &value)
 Fill array with value.
 
std::span< Tas_span () noexcept
 Convert to std::span for modern C++ usage.
 
std::span< const Tas_span () const noexcept
 Convert to const span.
 
size_type max_size () const noexcept
 Get maximum size (same as size() for fixed arrays)
 
pointer data () noexcept
 Get pointer to underlying data.
 
const_pointer data () const noexcept
 Get const pointer to underlying data.
 
std::string_view name () const noexcept
 Get array name from metadata table.
 
Iterator Support
iterator begin () noexcept
 Get iterator to beginning.
 
iterator end () noexcept
 Get iterator to end.
 
const_iterator begin () const noexcept
 Get const iterator to beginning.
 
const_iterator end () const noexcept
 Get const iterator to end.
 
const_iterator cbegin () const noexcept
 Get const iterator to beginning (explicit)
 
const_iterator cend () const noexcept
 Get const iterator to end (explicit)
 
Reverse Iterator Support
reverse_iterator rbegin () noexcept
 Get reverse iterator to beginning.
 
reverse_iterator rend () noexcept
 Get reverse iterator to end.
 
const_reverse_iterator rbegin () const noexcept
 Get const reverse iterator to beginning.
 
const_reverse_iterator rend () const noexcept
 Get const reverse iterator to end.
 
const_reverse_iterator crbegin () const noexcept
 Get const reverse iterator to beginning (explicit)
 
const_reverse_iterator crend () const noexcept
 Get const reverse iterator to end (explicit)
 
- Public Member Functions inherited from shm_span< T, ShmType >
 shm_span (ShmType &shm, size_t off, size_t count)
 
T * data ()
 
const T * data () const
 
size_t size () const
 
T & operator[] (size_t index)
 
const T & operator[] (size_t index) const
 

Additional Inherited Members

- Protected Attributes inherited from shm_span< T, ShmType >
ShmType & shm
 
size_t offset
 
size_t num_elem
 

Detailed Description

template<typename T, typename TableType = shm_table>
requires std::is_trivially_copyable_v<T>
class shm_array< T, TableType >

Fixed-size array in shared memory with zero-overhead access.

Provides a fully STL-compliant array interface for shared memory. Key features:

  • Zero-overhead reads: Direct pointer access, no synchronization
  • Named discovery: Find arrays by name across processes
  • STL compatibility: Works with all standard algorithms
  • Range support: Full C++20 ranges support
  • Type safety: Compile-time checks via concepts

Memory layout:

[T][T][T][T]... // Contiguous array of T
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator.
Definition shm_array.h:278
Template Parameters
TElement type (must be trivially copyable for shared memory)
TableTypeMetadata table type (default: shm_table)
Note
Read performance identical to native arrays (~2.3ns per access)
No synchronization provided - use external locking if needed
Warning
Cannot resize after creation (fixed-size)
Example:
// Process 1: Create and populate
posix_shm shm("sim", 10*1024*1024);
shm_array<double> temps(shm, "temperatures", 1000);
temps[0] = 25.5;
// Process 2: Discover and read
posix_shm shm("sim");
shm_array<double> temps(shm, "temperatures"); // Auto-discovers size
double t = temps[0]; // Zero-overhead read
ShmType & shm
Definition shm_span.h:13
See also
shm_span Base class providing core functionality
shm_queue For FIFO operations
shm_object_pool For dynamic allocation

Definition at line 62 of file shm_array.h.

Member Typedef Documentation

◆ const_iterator

template<typename T , typename TableType = shm_table>
using shm_array< T, TableType >::const_iterator = const T*

Const iterator.

Definition at line 277 of file shm_array.h.

◆ const_pointer

template<typename T , typename TableType = shm_table>
using shm_array< T, TableType >::const_pointer = const T*

Const pointer.

Definition at line 275 of file shm_array.h.

◆ const_reference

template<typename T , typename TableType = shm_table>
using shm_array< T, TableType >::const_reference = const T&

Const reference.

Definition at line 273 of file shm_array.h.

◆ const_reverse_iterator

template<typename T , typename TableType = shm_table>
using shm_array< T, TableType >::const_reverse_iterator = std::reverse_iterator<const_iterator>

Const reverse.

Definition at line 279 of file shm_array.h.

◆ difference_type

template<typename T , typename TableType = shm_table>
using shm_array< T, TableType >::difference_type = std::ptrdiff_t

Iterator difference.

Definition at line 271 of file shm_array.h.

◆ iterator

template<typename T , typename TableType = shm_table>
using shm_array< T, TableType >::iterator = T*

Iterator type (raw pointer)

Definition at line 276 of file shm_array.h.

◆ pointer

template<typename T , typename TableType = shm_table>
using shm_array< T, TableType >::pointer = T*

Pointer type.

Definition at line 274 of file shm_array.h.

◆ reference

template<typename T , typename TableType = shm_table>
using shm_array< T, TableType >::reference = T&

Reference type.

Definition at line 272 of file shm_array.h.

◆ reverse_iterator

template<typename T , typename TableType = shm_table>
using shm_array< T, TableType >::reverse_iterator = std::reverse_iterator<iterator>

Reverse iterator.

Definition at line 278 of file shm_array.h.

◆ size_type

template<typename T , typename TableType = shm_table>
using shm_array< T, TableType >::size_type = size_t

Size type.

Definition at line 270 of file shm_array.h.

◆ value_type

template<typename T , typename TableType = shm_table>
using shm_array< T, TableType >::value_type = T

Element type.

Definition at line 269 of file shm_array.h.

Constructor & Destructor Documentation

◆ shm_array()

template<typename T , typename TableType = shm_table>
template<typename ShmType >
shm_array< T, TableType >::shm_array ( ShmType shared_mem,
std::string_view  name,
size_t  count = 0 
)
inline

Create new array or attach to existing array by name.

Template Parameters
ShmTypeType of shared memory (must have matching TableType)
Parameters
shared_memShared memory segment to allocate from
nameUnique identifier for the array (max TableType::MAX_NAME_SIZE chars)
countNumber of elements (0 = attach to existing)
Exceptions
std::runtime_errorif creation fails (insufficient space)
std::runtime_errorif name not found and count=0
std::runtime_errorif existing array has different size
Note
Name is truncated if longer than MAX_NAME_SIZE
Uses stack allocation - cannot be deallocated
Thread Safety:
Constructor is thread-safe for different names. Race condition possible for same name.
Example:
// Create new array of 1000 floats
shm_array<float> arr(shm, "sensor_data", 1000);
// Attach to existing array (size auto-discovered)
shm_array<float> arr2(shm, "sensor_data");

Definition at line 97 of file shm_array.h.

References shm_array< T, TableType >::initialize(), and shm_array< T, TableType >::name().

Member Function Documentation

◆ as_span() [1/2]

template<typename T , typename TableType = shm_table>
std::span< const T > shm_array< T, TableType >::as_span ( ) const
inlinenoexcept

Convert to const span.

Definition at line 262 of file shm_array.h.

References shm_array< T, TableType >::data(), and shm_span< T, ShmType >::num_elem.

◆ as_span() [2/2]

template<typename T , typename TableType = shm_table>
std::span< T > shm_array< T, TableType >::as_span ( )
inlinenoexcept

Convert to std::span for modern C++ usage.

Returns
std::span view of the array
Note
Allows use with C++20 ranges and algorithms
Example:
auto span = arr.as_span();
std::ranges::sort(span);

Definition at line 256 of file shm_array.h.

References shm_array< T, TableType >::data(), and shm_span< T, ShmType >::num_elem.

◆ at() [1/2]

template<typename T , typename TableType = shm_table>
T & shm_array< T, TableType >::at ( size_t  pos)
inline

Bounds-checked element access.

Parameters
posIndex to access
Returns
Reference to element
Exceptions
std::out_of_rangeif pos >= size()
Note
Use operator[] for unchecked access (faster)
Complexity: O(1)

Definition at line 179 of file shm_array.h.

References shm_span< T, ShmType >::num_elem.

◆ at() [2/2]

template<typename T , typename TableType = shm_table>
const T & shm_array< T, TableType >::at ( size_t  pos) const
inline

Const version of at()

See also
at(size_t)

Definition at line 190 of file shm_array.h.

References shm_span< T, ShmType >::num_elem.

◆ back() [1/2]

template<typename T , typename TableType = shm_table>
T & shm_array< T, TableType >::back ( )
inline

Access last element.

Returns
Reference to last element
Precondition
!empty()

Definition at line 210 of file shm_array.h.

References shm_array< T, TableType >::data(), and shm_span< T, ShmType >::num_elem.

◆ back() [2/2]

template<typename T , typename TableType = shm_table>
const T & shm_array< T, TableType >::back ( ) const
inline

Access last element (const)

Definition at line 213 of file shm_array.h.

References shm_array< T, TableType >::data(), and shm_span< T, ShmType >::num_elem.

◆ begin() [1/2]

template<typename T , typename TableType = shm_table>
const_iterator shm_array< T, TableType >::begin ( ) const
inlinenoexcept

Get const iterator to beginning.

Definition at line 292 of file shm_array.h.

References shm_array< T, TableType >::data().

◆ begin() [2/2]

template<typename T , typename TableType = shm_table>
iterator shm_array< T, TableType >::begin ( )
inlinenoexcept

◆ cbegin()

template<typename T , typename TableType = shm_table>
const_iterator shm_array< T, TableType >::cbegin ( ) const
inlinenoexcept

Get const iterator to beginning (explicit)

Definition at line 298 of file shm_array.h.

References shm_array< T, TableType >::begin().

◆ cend()

template<typename T , typename TableType = shm_table>
const_iterator shm_array< T, TableType >::cend ( ) const
inlinenoexcept

Get const iterator to end (explicit)

Definition at line 301 of file shm_array.h.

References shm_array< T, TableType >::end().

◆ crbegin()

template<typename T , typename TableType = shm_table>
const_reverse_iterator shm_array< T, TableType >::crbegin ( ) const
inlinenoexcept

Get const reverse iterator to beginning (explicit)

Definition at line 320 of file shm_array.h.

References shm_array< T, TableType >::rbegin().

◆ crend()

template<typename T , typename TableType = shm_table>
const_reverse_iterator shm_array< T, TableType >::crend ( ) const
inlinenoexcept

Get const reverse iterator to end (explicit)

Definition at line 323 of file shm_array.h.

References shm_array< T, TableType >::rend().

◆ data() [1/2]

template<typename T , typename TableType = shm_table>
const_pointer shm_array< T, TableType >::data ( ) const
inlinenoexcept

Get const pointer to underlying data.

Definition at line 336 of file shm_array.h.

References shm_array< T, TableType >::data().

◆ data() [2/2]

◆ empty()

template<typename T , typename TableType = shm_table>
bool shm_array< T, TableType >::empty ( ) const
inlinenoexcept

Check if array is empty.

Returns
true if size() == 0

Definition at line 217 of file shm_array.h.

References shm_span< T, ShmType >::num_elem.

◆ end() [1/2]

template<typename T , typename TableType = shm_table>
const_iterator shm_array< T, TableType >::end ( ) const
inlinenoexcept

Get const iterator to end.

Definition at line 295 of file shm_array.h.

References shm_array< T, TableType >::data(), and shm_span< T, ShmType >::num_elem.

◆ end() [2/2]

◆ fill()

template<typename T , typename TableType = shm_table>
void shm_array< T, TableType >::fill ( const T value)
inline

Fill array with value.

Parameters
valueValue to fill with
Note
No synchronization - use external locking if needed
Complexity: O(n)
Example:
shm_array<int> arr(shm, "data", 1000);
arr.fill(42); // All elements = 42

Definition at line 238 of file shm_array.h.

References shm_array< T, TableType >::data(), and shm_span< T, ShmType >::num_elem.

◆ front() [1/2]

template<typename T , typename TableType = shm_table>
T & shm_array< T, TableType >::front ( )
inline

Access first element.

Returns
Reference to first element
Precondition
!empty()

Definition at line 202 of file shm_array.h.

References shm_array< T, TableType >::data().

◆ front() [2/2]

template<typename T , typename TableType = shm_table>
const T & shm_array< T, TableType >::front ( ) const
inline

Access first element (const)

Definition at line 205 of file shm_array.h.

References shm_array< T, TableType >::data().

◆ initialize()

template<typename T , typename TableType = shm_table>
void shm_array< T, TableType >::initialize ( std::string_view  name,
size_t  count = 0 
)
inline

◆ max_size()

template<typename T , typename TableType = shm_table>
size_type shm_array< T, TableType >::max_size ( ) const
inlinenoexcept

Get maximum size (same as size() for fixed arrays)

Returns
Maximum number of elements (equals size())

Definition at line 328 of file shm_array.h.

References shm_span< T, ShmType >::num_elem.

◆ name()

template<typename T , typename TableType = shm_table>
std::string_view shm_array< T, TableType >::name ( ) const
inlinenoexcept

Get array name from metadata table.

Returns
Name used to create/find this array
Note
Returns empty string_view if table entry lost
Complexity: O(1) - cached lookup
Example:
shm_array<int> arr(shm, "sensor_data", 100);
std::cout << arr.name(); // Prints: sensor_data

Definition at line 353 of file shm_array.h.

Referenced by example_large_simulation(), example_minimal_overhead(), shm_array< T, TableType >::initialize(), and shm_array< T, TableType >::shm_array().

◆ rbegin() [1/2]

template<typename T , typename TableType = shm_table>
const_reverse_iterator shm_array< T, TableType >::rbegin ( ) const
inlinenoexcept

Get const reverse iterator to beginning.

Definition at line 314 of file shm_array.h.

References shm_array< T, TableType >::end().

◆ rbegin() [2/2]

template<typename T , typename TableType = shm_table>
reverse_iterator shm_array< T, TableType >::rbegin ( )
inlinenoexcept

Get reverse iterator to beginning.

Definition at line 308 of file shm_array.h.

References shm_array< T, TableType >::end().

Referenced by shm_array< T, TableType >::crbegin().

◆ rend() [1/2]

template<typename T , typename TableType = shm_table>
const_reverse_iterator shm_array< T, TableType >::rend ( ) const
inlinenoexcept

Get const reverse iterator to end.

Definition at line 317 of file shm_array.h.

References shm_array< T, TableType >::begin().

◆ rend() [2/2]

template<typename T , typename TableType = shm_table>
reverse_iterator shm_array< T, TableType >::rend ( )
inlinenoexcept

Get reverse iterator to end.

Definition at line 311 of file shm_array.h.

References shm_array< T, TableType >::begin().

Referenced by shm_array< T, TableType >::crend().

◆ size()


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