Image Component Library (ICL)
Public Member Functions | Private Attributes | List of all members
icl::io::SharedMemorySegment Class Reference

Implementation of a cross-process shared memory. More...

#include <SharedMemorySegment.h>

Public Member Functions

 SharedMemorySegment (std::string name="", int minsize=1)
 Creates a SharedMemorySegment instance. More...
 
 ~SharedMemorySegment ()
 The destructor basically calls release. More...
 
void release ()
 Releases the acuired SharedMemorySegment. More...
 
std::string getName ()
 Returns the current SharedMemorySegments name. More...
 
void reset (std::string name, int minsize=0)
 Sets name and minsize to the passed values. More...
 
bool isAttached ()
 Tells whether this instance is currently attached to a SharedMemory. More...
 
bool lock (int minsize=0)
 Locks the underlying SharedMemorySegment, aquireing when needed. More...
 
bool unlock ()
 Unlocks the underlying SharedMemorySegment. More...
 
void forceMinSize (int minsize)
 Forces a resize of the SharedMemory to the passed minsize. More...
 
int getSize ()
 Returns the current size of the SharedMemorySegment in bytes. More...
 
bool isEmpty () const
 Tells whether the Segment is empty. More...
 
void * data ()
 getter for the MemorySegment data. More...
 
const void * data () const
 implicit const getter for the MemorySegment data. More...
 
const void * constData () const
 explicit const getter for the MemorySegment data. More...
 

Private Attributes

utils::Mutex m_Mutex
 instance locking More...
 
Impl * m_Impl
 pointer to the internal implementation More...
 
std::string m_Name
 the current name of the SharedMemorySegment More...
 
int m_Minsize
 local minimal size tf the Segment More...
 

Detailed Description

Implementation of a cross-process shared memory.

The SharedMemorySegment can be used to share information between multiple processes on a single system.

This class uses in iternal singleton implementation and map allowing multiple thereads to read a ShredMemory concurrent. The drawback is that external locking is needed for concurrent reading and writing.

Created SharedMemorySegments are registered via the SharedMemorySegmentRegister.

A signal handler is installed to ensure that all SharedMemorySegments are released.

Constructor & Destructor Documentation

◆ SharedMemorySegment()

icl::io::SharedMemorySegment::SharedMemorySegment ( std::string  name = "",
int  minsize = 1 
)

Creates a SharedMemorySegment instance.

No memory allocation is done here. To connect to the corresponding SharedMemorySegment call lock().

Parameters
nameThe name identifying the segment.
minsizeThe minimal needed size.

◆ ~SharedMemorySegment()

icl::io::SharedMemorySegment::~SharedMemorySegment ( )

The destructor basically calls release.

Member Function Documentation

◆ constData()

const void* icl::io::SharedMemorySegment::constData ( ) const

explicit const getter for the MemorySegment data.

Returns
a pointer to the memory block. Null when not attached or not locked.

◆ data() [1/2]

void* icl::io::SharedMemorySegment::data ( )

getter for the MemorySegment data.

Returns
a pointer to the memory block. Null when not attached or not locked.

◆ data() [2/2]

const void* icl::io::SharedMemorySegment::data ( ) const

implicit const getter for the MemorySegment data.

Returns
a pointer to the memory block. Null when not attached or not locked.

◆ forceMinSize()

void icl::io::SharedMemorySegment::forceMinSize ( int  minsize)

Forces a resize of the SharedMemory to the passed minsize.

This can shrink the SharedMemorySegment when the current space is no longer needed. It will force a recreation of the underlying SharedMemory to the passed size. It will not prevend other processes from re-enlarging the Segment. Caution: Don't use this to ensure that the SharedMemory is large enough. Call lock() with the desired size instead.

◆ getName()

std::string icl::io::SharedMemorySegment::getName ( )

Returns the current SharedMemorySegments name.

◆ getSize()

int icl::io::SharedMemorySegment::getSize ( )

Returns the current size of the SharedMemorySegment in bytes.

Returns
-1 when this instance is not attached

◆ isAttached()

bool icl::io::SharedMemorySegment::isAttached ( )

Tells whether this instance is currently attached to a SharedMemory.

Attachment is performed lock() and detachment release().

◆ isEmpty()

bool icl::io::SharedMemorySegment::isEmpty ( ) const

Tells whether the Segment is empty.

Basically checks whether all data bits are zero. A freshly created SharedMemorySegment is always overwritten with zeros.

◆ lock()

bool icl::io::SharedMemorySegment::lock ( int  minsize = 0)

Locks the underlying SharedMemorySegment, aquireing when needed.

Here happens all the fun. When this instance is not connected, it will be connected. When minsize > getSize or a resize is requested, this will detach and reattach/recreate with the needed size. Every call to lock needs a call to unlock. Multiple threads of the same process cat get the lock simultaneously. That way, concurrent access in one process is possible but extra locking is necessary when this is not desired. Can return true without having the necessary size when already locked by this process.

Returns
whether the segment is locked. may sometimes not be reached, when this process already holds the lock.

◆ release()

void icl::io::SharedMemorySegment::release ( )

Releases the acuired SharedMemorySegment.

When the last instance calls release the underlying singleton implementation unlocks and detatches from the systems SharedMemory.

◆ reset()

void icl::io::SharedMemorySegment::reset ( std::string  name,
int  minsize = 0 
)

Sets name and minsize to the passed values.

This function just sets the variables. Potential releasing and reattaching happens on the following lock().

◆ unlock()

bool icl::io::SharedMemorySegment::unlock ( )

Unlocks the underlying SharedMemorySegment.

Returns
whether the segment is not locked. true when still locked.

Member Data Documentation

◆ m_Impl

Impl* icl::io::SharedMemorySegment::m_Impl
private

pointer to the internal implementation

◆ m_Minsize

int icl::io::SharedMemorySegment::m_Minsize
private

local minimal size tf the Segment

◆ m_Mutex

utils::Mutex icl::io::SharedMemorySegment::m_Mutex
mutableprivate

instance locking

◆ m_Name

std::string icl::io::SharedMemorySegment::m_Name
private

the current name of the SharedMemorySegment


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