![]() |
This class is work in progress ... More...
#include <SharedMemory.h>
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. | |
![]() | |
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 | |
![]() | |
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. | |
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.
HSEP::SharedMemory::SharedMemory | ( | const string & | pNameRef | ) |
Creates a shared instance mapping to a previously created shared memory block.
pNameRef | The 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().
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.
pNameRef | The memory block that will be created and accessed through this instance. |
pSlotSize | The size for a sub-memory block inside the complete defined shared memory block. |
pSlotQty | The 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().
|
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().
int HSEP::SharedMemory::obtainSlot | ( | ) |
Obtains a free sub-memory block or slot from the shared memory area.
Definition at line 266 of file SharedMemory.cpp.
References HSEP::ManagedBuffer::popFree().
ErrorCode HSEP::SharedMemory::read | ( | int | pSlotId, |
char * | pDestinationPtr, | ||
size_t | pBufferSize | ||
) |
Definition at line 315 of file SharedMemory.cpp.
References HSEP::EC::BufferOverflow, HSEP::EC::BufferUnderflow, HSEP::EC::IncompleteProcessing, HSEP::EC::OK, HSEP::ManagedBuffer::slotQty(), and HSEP::ManagedBuffer::slotSize().
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.
pSlotId | the slot-ID that would be returned to the sub-memory block pool. |
Definition at line 274 of file SharedMemory.cpp.
References HSEP::ManagedBuffer::pushFree().
void HSEP::SharedMemory::signal | ( | int | pSlotId | ) |
Sends a slot id to another thread or process that is waiting for it running a wait method.
pSlotId |
Definition at line 354 of file SharedMemory.cpp.
References HSEP::ManagedBuffer::signal().
int HSEP::SharedMemory::wait | ( | ) |
Waits until a slot id is received.
Return such id.
Definition at line 349 of file SharedMemory.cpp.
References HSEP::ManagedBuffer::wait().
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.
pSlotId | The sub-memory block or slot where the data will be written. |
pSourcePtr | The data that will be written. |
pBufferSize | The quantity of bytes from pSource that will be written. |
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().