libcommonc++  0.7
SharedMemoryBlock Class Reference

An encapsulation of a block of memory that can be mapped by one or more (not necessarily related) processes; when one process changes the data in the block, the changes are immediately visible to the other processes. More...

#include <SharedMemoryBlock.h++>

Inheritance diagram for SharedMemoryBlock:
Collaboration diagram for SharedMemoryBlock:

Public Member Functions

 SharedMemoryBlock (const String &name, size_t size, const Permissions &perm=Permissions::USER_READ_WRITE)
 Construct a new SharedMemoryBlock. More...
 
 ~SharedMemoryBlock ()
 Destructor. More...
 
void attach (bool readOnly=false)
 Attach to the memory block, creating it if it does not yet exist. More...
 
void detach ()
 Detach from the memory block, and destroy it if no other process currently has it mapped. More...
 
virtual void setBase (byte_t *base)
 Overridden to have no effect. More...
 
virtual void setSize (size_t size)
 Overridden to have no effect. More...
 
String getName () const
 Get the name of the memory block. More...
 
void zero ()
 Zero the memory block. More...
 
void fill (byte_t value)
 Fill the memory block with the given byte value. More...
 
byte_tgetBase ()
 Get a pointer to the beginning of the memory block. More...
 
const byte_tgetBase () const
 Get a pointer to the beginning of the memory block. More...
 
size_t getSize () const
 Get the size of the memory block. More...
 
bool operator== (const MemoryBlock &other) const
 Equality operator. More...
 
bool operator!= (const MemoryBlock &other) const
 Inequality operator. More...
 
byte_t operator[] (int index) const
 Bounds-checked access. More...
 
byte_toperator[] (int index)
 Bounds-checked access. More...
 
bool copyFrom (const MemoryBlock &other)
 Copy data from another memory block to this one. More...
 
void moveFrom (MemoryBlock &other)
 Copy data from another (possibly overlapping) memory block to this one. More...
 
int indexOf (byte_t val, uint_t startIndex=0) const
 Find the first occurrence of a given byte value in the memory block, starting at a given index. More...
 
bool operator! () const
 Test if the base of the block is NULL. More...
 

Protected Attributes

byte_t_base
 A pointer to the beginning of the memory block. More...
 
size_t _size
 The size of the memory block, in bytes. More...
 

Detailed Description

An encapsulation of a block of memory that can be mapped by one or more (not necessarily related) processes; when one process changes the data in the block, the changes are immediately visible to the other processes.

Author
Mark Lindner

Constructor & Destructor Documentation

◆ SharedMemoryBlock()

SharedMemoryBlock ( const String name,
size_t  size,
const Permissions perm = Permissions::USER_READ_WRITE 
)

Construct a new SharedMemoryBlock.

The actual memory segment is not created and mapped until a call to attach() is made.

Parameters
nameThe name of the memory block. For maximal portability, the name should consist of at most 14 alphanumeric characters and should not contain slashes.
sizeThe size of the memory block, in bytes. This size will be rounded up to the nearest multiple of the system page size.
permThe permissions with which to create the memory block, if it does not exist at the time of the attach().

◆ ~SharedMemoryBlock()

Destructor.

Detaches from the memory block.

Member Function Documentation

◆ attach()

void attach ( bool  readOnly = false)

Attach to the memory block, creating it if it does not yet exist.

Parameters
readOnlyA flag indicating whether the memory block should be mapped for read-only access or read-write access.
Exceptions
SystemExceptionIf the operation fails.

◆ copyFrom()

bool copyFrom ( const MemoryBlock other)
inherited

Copy data from another memory block to this one.

The number of bytes copied is the minimum of the sizes of the two blocks. The blocks may not overlap.

Parameters
otherThe block to copy from.
Returns
true if the copy succeeded, false if the blocks overlap.

◆ detach()

void detach ( )

Detach from the memory block, and destroy it if no other process currently has it mapped.

◆ fill()

void fill ( byte_t  value)
inherited

Fill the memory block with the given byte value.

◆ getBase() [1/2]

byte_t* getBase ( )
inlineinherited

Get a pointer to the beginning of the memory block.

◆ getBase() [2/2]

const byte_t* getBase ( ) const
inlineinherited

Get a pointer to the beginning of the memory block.

◆ getName()

String getName ( ) const
inline

Get the name of the memory block.

◆ getSize()

size_t getSize ( ) const
inlineinherited

Get the size of the memory block.

◆ indexOf()

int indexOf ( byte_t  val,
uint_t  startIndex = 0 
) const
inherited

Find the first occurrence of a given byte value in the memory block, starting at a given index.

Parameters
valThe value to search for.
startIndexThe index to begin searching at.
Returns
The index of the first matching value, or -1 if not found.

◆ moveFrom()

void moveFrom ( MemoryBlock other)
inherited

Copy data from another (possibly overlapping) memory block to this one.

The number of bytes copied is the minimum of the sizes of the two blocks.

Parameters
otherThe block to copy from. Note that the copy may result in both blocks being modified, if the source area overlaps the target area.

◆ operator!()

bool operator! ( ) const
inlineinherited

Test if the base of the block is NULL.

◆ operator!=()

bool operator!= ( const MemoryBlock other) const
inlineinherited

Inequality operator.

◆ operator==()

bool operator== ( const MemoryBlock other) const
inherited

Equality operator.

◆ operator[]() [1/2]

byte_t operator[] ( int  index) const
inherited

Bounds-checked access.

Exceptions
OutOfBoundsExceptionIf the index is out of bounds.

◆ operator[]() [2/2]

byte_t & operator[] ( int  index)
inherited

Bounds-checked access.

Exceptions
OutOfBoundsExceptionIf the index is out of bounds.

◆ setBase()

void setBase ( byte_t base)
virtual

Overridden to have no effect.

Reimplemented from MemoryBlock.

◆ setSize()

void setSize ( size_t  size)
virtual

Overridden to have no effect.

◆ zero()

void zero ( )
inlineinherited

Zero the memory block.

Member Data Documentation

◆ _base

byte_t* _base
protectedinherited

A pointer to the beginning of the memory block.

◆ _size

size_t _size
protectedinherited

The size of the memory block, in bytes.


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