Image Component Library (ICL)
|
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... | |
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.
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().
name | The name identifying the segment. |
minsize | The minimal needed size. |
icl::io::SharedMemorySegment::~SharedMemorySegment | ( | ) |
The destructor basically calls release.
const void* icl::io::SharedMemorySegment::constData | ( | ) | const |
explicit const getter for the MemorySegment data.
void* icl::io::SharedMemorySegment::data | ( | ) |
getter for the MemorySegment data.
const void* icl::io::SharedMemorySegment::data | ( | ) | const |
implicit const getter for the MemorySegment data.
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.
std::string icl::io::SharedMemorySegment::getName | ( | ) |
Returns the current SharedMemorySegments name.
int icl::io::SharedMemorySegment::getSize | ( | ) |
Returns the current size of the SharedMemorySegment in bytes.
bool icl::io::SharedMemorySegment::isAttached | ( | ) |
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.
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.
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.
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().
bool icl::io::SharedMemorySegment::unlock | ( | ) |
Unlocks the underlying SharedMemorySegment.
|
private |
pointer to the internal implementation
|
private |
local minimal size tf the Segment
|
mutableprivate |
instance locking
|
private |
the current name of the SharedMemorySegment