All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HSEP::SharedMemory Class Reference

This class is work in progress ... More...

#include <SharedMemory.h>

+ Inheritance diagram for HSEP::SharedMemory:
+ Collaboration diagram for HSEP::SharedMemory:

Public Member Functions

 SharedMemory (const string &pNameRef)
 Creates a shared instance mapping to a previously created shared memory block.
 
 SharedMemory (const string &pNameRef, size_t pSlotSize, int pSlotQty)
 Creates a shared memory block with the given name and sizes, and permits to access it to the creator.
 
virtual ~SharedMemory ()
 When the shared memory area has been created, releases and destroys it, returning the memory to the Operating System.
 
int obtainSlot ()
 Obtains a free sub-memory block or slot from the shared memory area.
 
void releaseSlot (int pSlotId)
 Returns "releases" the sub-memory block or slot that was previously requested with obtainSlot().
 
ErrorCode write (int pSlotId, char *pSourcePtr, size_t pBufferSize)
 Writes some data to the specified slot, taking care to avoid any type of overflows.
 
ErrorCode read (int pSlotId, char *pDestinationPtr, size_t pBufferSize)
 
int wait ()
 Waits until a slot id is received.
 
void signal (int pSlotId)
 Sends a slot id to another thread or process that is waiting for it running a wait method.
 
- Public Member Functions inherited from HSEP::HSEPObject
string & lastErrorRef ()
 Returns a reference to the last stored error message.
 
ErrorCode lastCode ()
 Returns the last stored code.
 
bool valid ()
 Permits to know if the object has a valid internal state.
 
string & typeRef ()
 Permits to know the type of this particular object.
 

Additional Inherited Members

- Protected Member Functions inherited from HSEP::HSEPObject
 HSEPObject (const char *pType)
 Constructor, providing the base for the state controlling instance.
 
virtual ~HSEPObject ()
 
void setLastError (string &pErrorMsgRef)
 Assigns an error message text to the last executed operation.
 
void setLastError (const string &pErrorMsgRef)
 Assigns an error message text to the last executed operation.
 
void setLastError (const char *pErrorMsg)
 Assigns an error message text to the last executed operation.
 
void setValid ()
 Defines that the object has a valid state.
 
void setInvalid ()
 Defines that the object has an invalid state.
 
void setLastCode (const ErrorCode pErrorCode)
 Defines the internal result code for the object.
 

Detailed Description

This class is work in progress ...

... There are still some "local memory" issues that must be resolved to have a full multi-process sharing memory area ... Provides the fastest method for Interprocess Communication using shared memory blocks.

Each SharedMemory instance creates two independent shared memory areas with the names:

/givenname-header /givenname-buffer

being "givenname" a valid file name given to the instance through the constructor.

The /givenname-header file contains all the important stuff that will help to control how the /givenname-buffer memory area will work. This is necessary because we don't like that the control data and the actual stored data be combined, so a possible misleading in how the buffer is controlled could lead to unknown problems.

Must be taken into consideration that the instance that creates the shared memory area will destroy it when going out, but only if the program that defined such instance is able to have a clear exit that permit all the constructor to be correctly executed.

A "security" issue: The shared memory operations permit to clean the unused memory if they are used in order, request, return. If the program just stop without returning the unused memory to the class, the used memory slots will stay with the written data until the operating system decides to recover that memory; in that case, as this is implementation supported, could be possible that memory zone stay with sensitive data when another process ask for memory.

Also, interprocess communication primitives wait and signal are included, to send a slot id between two processes. As the SharedMemory works with memory areas, the slot ids is enough information to use one of the memory slots as an interchange mechanism.

Definition at line 74 of file SharedMemory.h.

Constructor & Destructor Documentation

HSEP::SharedMemory::SharedMemory ( const string &  pNameRef)

Creates a shared instance mapping to a previously created shared memory block.

Parameters
pNameRefThe memory block that will be accessed through this instance.

Definition at line 45 of file SharedMemory.cpp.

References HSEP::EC::AccessError, HSEP::EC::AlreadyCreated, HSEP::ManagedBuffer::dataSize(), HSEP::EC::InvalidArgument, HSEP::EC::InvalidObject, HSEP::EC::ManyOpenFiles, HSEP::EC::PathNameVeryLong, HSEP::HSEPObject::setLastCode(), and HSEP::HSEPObject::setLastError().

+ Here is the call graph for this function:

HSEP::SharedMemory::SharedMemory ( const string &  pNameRef,
size_t  pSlotSize,
int  pSlotQty 
)

Creates a shared memory block with the given name and sizes, and permits to access it to the creator.

Parameters
pNameRefThe memory block that will be created and accessed through this instance.
pSlotSizeThe size for a sub-memory block inside the complete defined shared memory block.
pSlotQtyThe quantity of sub-memory blocks or slots that will be managed through this interface.

Definition at line 138 of file SharedMemory.cpp.

References HSEP::EC::AccessError, HSEP::EC::AlreadyCreated, HSEP::ManagedBuffer::dataSize(), HSEP::EC::InvalidArgument, HSEP::EC::InvalidObject, HSEP::EC::ManyOpenFiles, HSEP::EC::PathNameVeryLong, HSEP::HSEPObject::setLastCode(), and HSEP::HSEPObject::setLastError().

+ Here is the call graph for this function:

HSEP::SharedMemory::~SharedMemory ( )
virtual

When the shared memory area has been created, releases and destroys it, returning the memory to the Operating System.

When the shared memory area has been only accessed, releases the handler managed by this instance.

Definition at line 240 of file SharedMemory.cpp.

References HSEP::ManagedBuffer::dataSize().

+ Here is the call graph for this function:

Member Function Documentation

int HSEP::SharedMemory::obtainSlot ( )

Obtains a free sub-memory block or slot from the shared memory area.

Returns
the slot-ID that has been requested.

Definition at line 266 of file SharedMemory.cpp.

References HSEP::ManagedBuffer::popFree().

+ Here is the call graph for this function:

ErrorCode HSEP::SharedMemory::read ( int  pSlotId,
char *  pDestinationPtr,
size_t  pBufferSize 
)
void HSEP::SharedMemory::releaseSlot ( int  pSlotId)

Returns "releases" the sub-memory block or slot that was previously requested with obtainSlot().

Before returning the memory to the shared area, the contents for this sub-area or slot are destroyed.

Parameters
pSlotIdthe slot-ID that would be returned to the sub-memory block pool.

Definition at line 274 of file SharedMemory.cpp.

References HSEP::ManagedBuffer::pushFree().

+ Here is the call graph for this function:

void HSEP::SharedMemory::signal ( int  pSlotId)

Sends a slot id to another thread or process that is waiting for it running a wait method.

Parameters
pSlotId

Definition at line 354 of file SharedMemory.cpp.

References HSEP::ManagedBuffer::signal().

+ Here is the call graph for this function:

int HSEP::SharedMemory::wait ( )

Waits until a slot id is received.

Return such id.

Returns

Definition at line 349 of file SharedMemory.cpp.

References HSEP::ManagedBuffer::wait().

+ Here is the call graph for this function:

ErrorCode HSEP::SharedMemory::write ( int  pSlotId,
char *  pSourcePtr,
size_t  pBufferSize 
)

Writes some data to the specified slot, taking care to avoid any type of overflows.

Won't success if the pSlotId was not obtained previously by any of the processes that shares the shared memory area.

Parameters
pSlotIdThe sub-memory block or slot where the data will be written.
pSourcePtrThe data that will be written.
pBufferSizeThe quantity of bytes from pSource that will be written.
Returns
EC::IncompleteProcessing if the operation didn't have a clean exit.
EC::BufferUnderflow if pSlotId is negative.
EC::BufferOverflow if pSlotId go beyond the defined shared area.
EC::OK if the operation was successful.

Definition at line 281 of file SharedMemory.cpp.

References HSEP::EC::BufferOverflow, HSEP::EC::BufferUnderflow, HSEP::EC::IncompleteProcessing, HSEP::EC::OK, HSEP::ManagedBuffer::slotQty(), and HSEP::ManagedBuffer::slotSize().

+ Here is the call graph for this function:


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