Fawkes API  Fawkes Development Version
fawkes::SharedMemory Class Reference

Shared memory segment. More...

#include <>>

Inheritance diagram for fawkes::SharedMemory:

Classes

struct  SharedMemory_header_t
 General header. More...
 
class  SharedMemoryIterator
 Shared Memory iterator. More...
 

Public Member Functions

 SharedMemory (const char *magic_token, SharedMemoryHeader *header, bool is_read_only, bool create, bool destroy_on_delete, const char *registry_name=0)
 Create a new shared memory segment. More...
 
 SharedMemory (const SharedMemory &s)
 Copy constructor. More...
 
virtual ~SharedMemory ()
 Destructor. More...
 
bool is_read_only () const
 Check for read-only mode. More...
 
bool is_destroyed () const
 Check if segment has been destroyed This can be used if the segment has been destroyed. More...
 
bool is_swapable () const
 Check if memory can be swapped out. More...
 
bool is_valid () const
 Check validity of shared memory segment. More...
 
bool is_creator () const
 Determine if the shared memory segment has been created by this instance. More...
 
bool is_protected () const
 Check if memory segment is protected. More...
 
void * memptr () const
 Get a pointer to the shared memory This method returns a pointer to the data-segment of the shared memory segment. More...
 
size_t data_size () const
 Get the size of the data-segment. More...
 
int shmem_id () const
 Get shared memory ID. More...
 
unsigned int num_attached () const
 Get number of attached processes. More...
 
void set (void *memptr)
 Copies data from the memptr to shared memory. More...
 
void set_destroy_on_delete (bool destroy)
 Set deletion behaviour. More...
 
void add_semaphore ()
 Add semaphore to shared memory segment. More...
 
void set_swapable (bool swapable)
 Set shared memory swapable. More...
 
void lock_for_read ()
 Lock shared memory segment for reading. More...
 
bool try_lock_for_read ()
 Try to aquire lock on shared memory segment for reading. More...
 
void lock_for_write ()
 Lock shared memory segment for writing. More...
 
bool try_lock_for_write ()
 Try to aquire lock on shared memory segment for writing. More...
 
void unlock ()
 Unlock memory. More...
 
void * ptr (void *addr) const
 Get the real pointer to the data based on an address. More...
 
void * addr (void *ptr) const
 Get an address from a real pointer. More...
 

Static Public Member Functions

static void list (const char *magic_token, SharedMemoryHeader *header, SharedMemoryLister *lister, const char *registry_name=0)
 List shared memory segments of a given type. More...
 
static void erase (const char *magic_token, SharedMemoryHeader *header, SharedMemoryLister *lister=0, const char *registry_name=0)
 Erase shared memory segments of a given type. More...
 
static void erase_orphaned (const char *magic_token, SharedMemoryHeader *header, SharedMemoryLister *lister=0, const char *registry_name=0)
 Erase orphaned (attach count = 0) shared memory segments of a given type. More...
 
static bool exists (const char *magic_token, SharedMemoryHeader *header, const char *registry_name=0)
 Check if a specific shared memory segment exists. More...
 
static bool is_destroyed (int shm_id)
 Check if a segment has been destroyed. More...
 
static bool is_swapable (int shm_id)
 Check if memory can be swapped out. More...
 
static unsigned int num_attached (int shm_id)
 Get number of attached processes. More...
 
static SharedMemoryIterator find (const char *magic_token, SharedMemoryHeader *header, const char *registry_name=0)
 Find SharedMemory segments. More...
 
static SharedMemoryIterator end ()
 Get invalid iterator. More...
 

Static Public Attributes

static const unsigned int MagicTokenSize = MAGIC_TOKEN_SIZE
 The magic token size. More...
 
static const short MaxNumConcurrentReaders = 8
 Maximum number of concurrent readers. More...
 

Protected Member Functions

 SharedMemory (const char *magic_token, bool is_read_only, bool create, bool destroy_on_delete, const char *registry_name=0)
 Constructor for derivates. More...
 
void attach ()
 Attach to the shared memory segment. More...
 
void free ()
 Detach from and maybe destroy the shared memory segment. More...
 

Protected Attributes

void * _memptr
 Pointer to the data segment. More...
 
size_t _mem_size
 Total size of the segment, including headers. More...
 
size_t _data_size
 Size of the data segment only. More...
 
SharedMemoryHeader_header
 Data-specific header. More...
 
bool _is_read_only
 Read-only. More...
 
bool _destroy_on_delete
 destroy on delete. More...
 
bool _should_create
 Create shared memory segment. More...
 
char * _magic_token
 Magic token. More...
 
char * _shm_magic_token
 Magic token as stored in the shared memory segment. More...
 
SharedMemory_header_t_shm_header
 general header as stored in the shared memory segment More...
 
void * _shm_upper_bound
 Upper bound of memory. More...
 
long unsigned int _shm_offset
 Offset to the master's base addr. More...
 

Detailed Description

Shared memory segment.

This class gives access to shared memory segment to store arbitrary data. With shared memory data can be shared between several applications. Special means like semaphores have to be used to control access to the storage to prevent data corruption.

The shared memory segment is divided into three parts.

  1. General shared memory header
  2. Data-specific header
  3. Data

The general header consists of a magic token of MagicTokenSize that is used to find the basically compatible shared memory segments out of all existing shared memory segments. This is done for convenience. Although in general shared memory is accessed via keys or IDs it is easier from the maintenance side to just scan the segments to find the correct one, especially if there may be more than just one segment for the same application. The header also includes a semaphore ID which is unused at the moment.

The data-specific header is generated from a given SharedMemoryHeader implementation. It can be used to store any information that is needed to identify a specific shared memory segment and to store management data for the data segment. It should always contain enough information to derive the data segment size or if needed an explicit information about the memory size.

The data segment can be filled with any data you like.

Shared memory segments are protected with a read-write lock implemented with two IPC semaphores. The writer takes preference in locking. Only a limited number of concurrent readers can be allowed. The constant MaxNumberConcurrentReaders defines how many these are. If a shared memory segment already has a semaphore assigned at the time it is opened this semaphore is automatically opened. In any case add_semaphore() can be used to create (or open if it already exists) a semaphore for the shared memory segment. Information about the semaphore is stored in the shared memory general header.

This class provides utilities to list, erase and check existence of given shared memory segments. For this often a SharedMemoryLister is used that takes care of formatting the output of the specific information about the shared memory segment.

See also
SharedMemoryHeader
SharedMemorySegment
qa_shmem.cpp
Author
Tim Niemueller

Definition at line 49 of file shm.h.

Constructor & Destructor Documentation

◆ SharedMemory() [1/3]

fawkes::SharedMemory::SharedMemory ( const char *  magic_token,
SharedMemoryHeader header,
bool  is_read_only,
bool  create,
bool  destroy_on_delete,
const char *  registry_name = 0 
)

Create a new shared memory segment.

This will open a shared memory segment that exactly fits the given SharedMemoryHeader. It the segment does not exist and create is assured the segment is created from the given data, otherwise the SharedMemory instance remains in an invalid state and an exception is thrown. The segment can be destroyed automatically if the instance is destroyed. Shared memory segments can be opened read-only.

Parameters
magic_tokenThis is the magic token discussed above that is used to identify the shared memory segment. The magic_token can be of arbitrary size but at most MagicTokenSize bytes are used.
headerThe data-sepcific header used for this shared memory segment
is_read_onlyif true the shared memory segment is opened in read-only mode
createif true the shared memory segment is created if no one matching the headers was found
destroy_on_deleteif true the shared memory segment is destroyed when this SharedMemory instance is deleted.
registry_namename of the SharedMemoryRegistry to use
Exceptions
ShmNoHeaderExceptionNo header has been set
ShmInconsistentSegmentSizeExceptionThe memory size is not the expected memory size
ShmCouldNotAttachExceptionCould not attach to shared memory segment

Definition at line 352 of file shm.cpp.

References _data_size, _destroy_on_delete, _header, _is_read_only, _magic_token, _memptr, _shm_header, _shm_magic_token, _should_create, fawkes::Exception::append(), attach(), is_read_only(), and MagicTokenSize.

◆ SharedMemory() [2/3]

fawkes::SharedMemory::SharedMemory ( const SharedMemory s)

Copy constructor.

If the given SharedMemory was attached this instance will also attach.

Parameters
sSharedMemory instance to copy.

Definition at line 282 of file shm.cpp.

References _data_size, _destroy_on_delete, _header, _is_read_only, _magic_token, _memptr, _shm_header, _shm_magic_token, _should_create, fawkes::Exception::append(), attach(), fawkes::SharedMemoryHeader::clone(), and MagicTokenSize.

◆ ~SharedMemory()

fawkes::SharedMemory::~SharedMemory ( )
virtual

◆ SharedMemory() [3/3]

fawkes::SharedMemory::SharedMemory ( const char *  magic_token,
bool  is_read_only,
bool  create,
bool  destroy_on_delete,
const char *  registry_name = 0 
)
protected

Constructor for derivates.

This constructor may only be used by derivatives. It can be used to delay the call to attach() to do other preparations like creating a SharedMemoryHeader object.

Parameters
magic_tokenmagic token of the shared memory segment
is_read_onlyif true the shared memory segment is opened in read-only mode
createif true the shared memory segment is created if no one matching the headers was found
destroy_on_deleteif true the shared memory segment is destroyed when this SharedMemory instance is deleted.
registry_namename of the SharedMemoryRegistry to use

Definition at line 241 of file shm.cpp.

References _data_size, _destroy_on_delete, _header, _is_read_only, _magic_token, _memptr, _shm_header, _shm_magic_token, _should_create, is_read_only(), and MagicTokenSize.

Member Function Documentation

◆ add_semaphore()

void fawkes::SharedMemory::add_semaphore ( )

Add semaphore to shared memory segment.

This adds a semaphore to the system and puts its key in the shared memory segment header. The semaphore can then be protected via the semaphore by appropriate locking. If a semaphore has been assigned to the shared memory segment already but after the segment was opened the semaphore is opened and no new semaphore is created.

Definition at line 810 of file shm.cpp.

References _is_read_only, _memptr, _shm_header, fawkes::SemaphoreSet::key(), MaxNumConcurrentReaders, fawkes::SharedMemory::SharedMemory_header_t::semaphore, fawkes::SemaphoreSet::set_value(), and fawkes::SemaphoreSet::unlock().

Referenced by attach(), fawkes::BlackBoardMemoryManager::BlackBoardMemoryManager(), and firevision::SharedMemoryImageBuffer::SharedMemoryImageBuffer().

◆ addr()

void * fawkes::SharedMemory::addr ( void *  ptr) const

Get an address from a real pointer.

If there is address-dependent data in the shared memory segment (like pointers to the next element in a linked list) these are only valid for the process that created the shared memory segment, they are not necessarily valid for other processes.

This method takes a pointer that points to data in the shared memory segment that is valid in the local process and transform it to a pointer that is valid inside the shared memory segment with respect to the base address used by the creating process.

Parameters
ptrpointer to data inside the shared memory segment
Returns
memory address valid for the creator of the shared memory segment
Exceptions
ShmPtrOutOfBoundsExceptionThis exception is thrown if ptr is not NULL, smaller than the local base ptr and greater or equal to the local base ptr plus the memory size.
See also
ptr()

Definition at line 637 of file shm.cpp.

References _shm_offset, and ptr().

Referenced by fawkes::BlackBoardMemoryManager::BlackBoardMemoryManager(), ptr(), fawkes::BlackBoardSharedMemoryHeader::set_alloc_list_head(), fawkes::BlackBoardSharedMemoryHeader::set_free_list_head(), and fawkes::BlackBoardMemoryManager::~BlackBoardMemoryManager().

◆ attach()

◆ data_size()

size_t fawkes::SharedMemory::data_size ( ) const

Get the size of the data-segment.

Use this method to get the size of the data segment. Calls dataSize() of the data-specific header internally.

Returns
size of the data-segment in bytes

Definition at line 694 of file shm.cpp.

References _data_size.

◆ end()

SharedMemory::SharedMemoryIterator fawkes::SharedMemory::end ( )
static

Get invalid iterator.

Returns an iterator to a non-existing element.

Returns
Non-existing element

Definition at line 1254 of file shm.cpp.

Referenced by erase(), erase_orphaned(), exists(), find(), and list().

◆ erase()

void fawkes::SharedMemory::erase ( const char *  magic_token,
SharedMemoryHeader header,
SharedMemoryLister lister = 0,
const char *  registry_name = 0 
)
static

Erase shared memory segments of a given type.

This method erases (destroys) all shared memory segments that match the given magic token (first MagicTokenSize bytes, filled with zero) and the given header. The lister is called to format the output. If a semaphore has been assigned to this shared memory segment it is destroyed as well.

Parameters
magic_tokenToken to look for
headerheader to identify interesting segments with matching magic_token
listerLister used to format output, maybe NULL (default)
registry_namename of the SharedMemoryRegistry to use

Definition at line 1115 of file shm.cpp.

References fawkes::SharedMemory::SharedMemoryIterator::databuf(), fawkes::SemaphoreSet::destroy(), end(), find(), fawkes::SharedMemoryLister::print_footer(), fawkes::SharedMemoryLister::print_header(), fawkes::SharedMemoryLister::print_info(), fawkes::SharedMemoryLister::print_no_segments(), fawkes::SharedMemory::SharedMemoryIterator::segmsize(), fawkes::SharedMemory::SharedMemoryIterator::semaphore(), and fawkes::SharedMemory::SharedMemoryIterator::shmid().

◆ erase_orphaned()

void fawkes::SharedMemory::erase_orphaned ( const char *  magic_token,
SharedMemoryHeader header,
SharedMemoryLister lister = 0,
const char *  registry_name = 0 
)
static

Erase orphaned (attach count = 0) shared memory segments of a given type.

This method erases (destroys) all shared memory segments that match the given magic token (first MagicTokenSize bytes, filled with zero) and the given header and where no process is attached to. If a semaphore has been assigned to this shared memory segment it is destroyed as well. The lister is called to format the output.

Parameters
magic_tokenToken to look for
headerheader to identify interesting segments with matching magic_token
listerLister used to format output, maybe NULL (default)
registry_namename of the SharedMemoryRegistry to use

Definition at line 1163 of file shm.cpp.

References fawkes::SharedMemory::SharedMemoryIterator::databuf(), fawkes::SemaphoreSet::destroy(), end(), find(), fawkes::SharedMemoryLister::print_footer(), fawkes::SharedMemoryLister::print_header(), fawkes::SharedMemoryLister::print_info(), fawkes::SharedMemoryLister::print_no_orphaned_segments(), fawkes::SharedMemoryLister::print_no_segments(), fawkes::SharedMemory::SharedMemoryIterator::segmnattch(), fawkes::SharedMemory::SharedMemoryIterator::segmsize(), fawkes::SharedMemory::SharedMemoryIterator::semaphore(), and fawkes::SharedMemory::SharedMemoryIterator::shmid().

Referenced by fawkes::LocalBlackBoard::cleanup().

◆ exists()

bool fawkes::SharedMemory::exists ( const char *  magic_token,
SharedMemoryHeader header,
const char *  registry_name = 0 
)
static

Check if a specific shared memory segment exists.

This method will search for a memory chunk that matches the given magic token and header.

Parameters
magic_tokenToken to look for
headerheader to identify interesting segments with matching magic_token
registry_namename of the SharedMemoryRegistry to use
Returns
true, if a matching shared memory segment was found, else otherwise

Definition at line 1221 of file shm.cpp.

References end(), and find().

◆ find()

SharedMemory::SharedMemoryIterator fawkes::SharedMemory::find ( const char *  magic_token,
SharedMemoryHeader header,
const char *  registry_name = 0 
)
static

Find SharedMemory segments.

Find SharedMemory segments identified by the supplied magic_token and header.

Parameters
magic_tokenmagic token
headershared memory header
registry_namename of the SharedMemoryRegistry to use
Returns
iterator pointing to the first found element (or end() if none found)

Definition at line 1237 of file shm.cpp.

References end(), and fawkes::SharedMemoryRegistry::find_segments().

Referenced by erase(), erase_orphaned(), exists(), and list().

◆ free()

◆ is_creator()

bool fawkes::SharedMemory::is_creator ( ) const

Determine if the shared memory segment has been created by this instance.

In some situations you want to know if the current instance has created the shared memory segment or if it attached to an existing shared memory segment. This is handy for example in master-slave constellations where one process is the master over a given shared memory segment and other slaves may read but need special means to alter the data. This is a somewhat softer variant of exclusive access.

Returns
true, if this instance of SharedMemory created the segment, false otherwise

Definition at line 670 of file shm.cpp.

Referenced by fawkes::BlackBoardMemoryManager::BlackBoardMemoryManager().

◆ is_destroyed() [1/2]

bool fawkes::SharedMemory::is_destroyed ( ) const

Check if segment has been destroyed This can be used if the segment has been destroyed.

This means that no other process can connect to the shared memory segment. As long as some process is attached to the shared memory segment the segment will still show up in the list

Returns
true, if this shared memory segment has been destroyed, false otherwise

Definition at line 741 of file shm.cpp.

◆ is_destroyed() [2/2]

bool fawkes::SharedMemory::is_destroyed ( int  shm_id)
static

Check if a segment has been destroyed.

Check for a shared memory segment of the given ID.

Parameters
shm_idID of the shared memory segment.
Returns
true, if the shared memory segment is marked as destroyed or does not exist at all, false otherwise.

Definition at line 1011 of file shm.cpp.

◆ is_protected()

bool fawkes::SharedMemory::is_protected ( ) const

Check if memory segment is protected.

This method can be used to determine if a semaphore has been associated to this shared memory segment. Locking is not guaranteed, it depends on the application. Use lock(), tryLock() and unlock() appropriately. You can do this always, also if you start with unprotected memory. The operations are just noops in that case. Protection can be enabled by calling add_semaphore(). If a memory segment was protected when it was opened it is automatically opened in protected mode.

Returns
true, if semaphore is associated to memory, false otherwise

Definition at line 783 of file shm.cpp.

◆ is_read_only()

bool fawkes::SharedMemory::is_read_only ( ) const

Check for read-only mode.

Returns
true, if the segment is opened in read-only mode, false otherwise

Definition at line 653 of file shm.cpp.

References _is_read_only.

Referenced by SharedMemory(), firevision::SharedMemoryImageBuffer::SharedMemoryImageBuffer(), and firevision::SharedMemoryLookupTable::SharedMemoryLookupTable().

◆ is_swapable() [1/2]

bool fawkes::SharedMemory::is_swapable ( ) const

Check if memory can be swapped out.

This method can be used to check if the memory can be swapped.

Returns
true, if the memory can be swapped, false otherwise

Definition at line 752 of file shm.cpp.

◆ is_swapable() [2/2]

bool fawkes::SharedMemory::is_swapable ( int  shm_id)
static

Check if memory can be swapped out.

This method can be used to check if the memory can be swapped.

Parameters
shm_idID of the shared memory segment.
Returns
true, if the memory can be swapped, false otherwise

Definition at line 1034 of file shm.cpp.

◆ is_valid()

bool fawkes::SharedMemory::is_valid ( ) const

Check validity of shared memory segment.

Use this to check if the shared memory segmentis valid. That means that this instance is attached to the shared memory and data can be read from or written to the memptr.

Returns
true, if the shared memory segment is valid and can be utilized, false otherwise

Definition at line 766 of file shm.cpp.

References _memptr.

Referenced by fawkes::BlackBoardMemoryManager::BlackBoardMemoryManager(), and FvRetrieverThread::init().

◆ list()

void fawkes::SharedMemory::list ( const char *  magic_token,
SharedMemoryHeader header,
SharedMemoryLister lister,
const char *  registry_name = 0 
)
static

List shared memory segments of a given type.

This method lists all shared memory segments that match the given magic token (first MagicTokenSize bytes, filled with zero) and the given header. The lister is called to format the output.

Parameters
magic_tokenToken to look for
headerheader to identify interesting segments with matching magic_token
listerLister used to format output
registry_namename of the SharedMemoryRegistry to use

Definition at line 1079 of file shm.cpp.

References fawkes::SharedMemory::SharedMemoryIterator::databuf(), end(), find(), fawkes::SharedMemoryLister::print_footer(), fawkes::SharedMemoryLister::print_header(), fawkes::SharedMemoryLister::print_info(), fawkes::SharedMemoryLister::print_no_segments(), fawkes::SharedMemory::SharedMemoryIterator::segmsize(), fawkes::SharedMemory::SharedMemoryIterator::semaphore(), and fawkes::SharedMemory::SharedMemoryIterator::shmid().

◆ lock_for_read()

void fawkes::SharedMemory::lock_for_read ( )

Lock shared memory segment for reading.

If the shared memory segment is protected by an associated semaphore it can be locked with this semaphore by calling this method.

See also
isProtected()
unlock()
try_lock_for_read()

Definition at line 867 of file shm.cpp.

References fawkes::SemaphoreSet::lock().

Referenced by firevision::FuseImageContent::FuseImageContent(), firevision::SharedMemoryCamera::lock_for_read(), and OpenNiPclOnlyThread::loop().

◆ lock_for_write()

void fawkes::SharedMemory::lock_for_write ( )

Lock shared memory segment for writing.

If the shared memory segment is protected by an associated semaphore it can be locked with this semaphore by calling this method.

See also
is_protected()
unlock()
try_lock_for_read()

Definition at line 918 of file shm.cpp.

References fawkes::SemaphoreSet::lock(), MaxNumConcurrentReaders, and fawkes::SemaphoreSet::unlock().

Referenced by fawkes::BlackBoardMemoryManager::alloc(), OpenNiPointCloudThread::finalize(), fawkes::BlackBoardMemoryManager::free(), fawkes::BlackBoardMemoryManager::lock(), FvRetrieverThread::loop(), OpenNiImageThread::loop(), and Bumblebee2Thread::loop().

◆ memptr()

void * fawkes::SharedMemory::memptr ( ) const

Get a pointer to the shared memory This method returns a pointer to the data-segment of the shared memory segment.

It has the size stated as dataSize() from the header.

Returns
pointer to the data-segment
See also
getDataSize()

Definition at line 682 of file shm.cpp.

References _memptr.

Referenced by fawkes::BlackBoardMemoryManager::BlackBoardMemoryManager().

◆ num_attached() [1/2]

unsigned int fawkes::SharedMemory::num_attached ( ) const

Get number of attached processes.

Returns
number of attached processes

Definition at line 714 of file shm.cpp.

Referenced by OpenNiDepthThread::loop(), OpenNiPclOnlyThread::loop(), OpenNiImageThread::loop(), OpenNiUserTrackerThread::loop(), Bumblebee2Thread::loop(), and OpenNiPointCloudThread::loop().

◆ num_attached() [2/2]

unsigned int fawkes::SharedMemory::num_attached ( int  shm_id)
static

Get number of attached processes.

Parameters
shm_idID of the shared memory segment.
Returns
number of attached processes

Definition at line 1056 of file shm.cpp.

◆ ptr()

void * fawkes::SharedMemory::ptr ( void *  addr) const

Get the real pointer to the data based on an address.

If there is address-dependent data in the shared memory segment (like pointers to the next element in a linked list) these are only valid for the process that created the shared memory segment, they are not necessarily valid for other processes.

The function takes an address that has been stored in the shared memory segment and transforms it into a valid local pointer. Not that this does only work with pointers inside the shared memory segment. You can only tranform addresses that point to somewhere inside the shared memory segment!

We could also have added local offsets, starting with 0 at the beginning of the shared memory segment. We decided against this since our major our main concern is that this works fast for the master, because this will be the Fawkes main application, and for attached processes it may work slower and we don't care.

Parameters
addrmemory address read from the shared memory segment
Returns
pointer inside the shared memory segment
Exceptions
ShmAddrOutOfBoundsExceptionThis exception is thrown if addr is not NULL, smaller than the base addr and greater or equal to the base addr plus the memory size.
See also
addr()

Definition at line 606 of file shm.cpp.

References _shm_offset, _shm_upper_bound, and addr().

Referenced by addr(), fawkes::BlackBoardSharedMemoryHeader::alloc_list_head(), fawkes::BlackBoardSharedMemoryHeader::free_list_head(), fawkes::BlackBoardMemoryManager::ChunkIterator::operator*(), and fawkes::BlackBoardMemoryManager::~BlackBoardMemoryManager().

◆ set()

void fawkes::SharedMemory::set ( void *  memptr)

Copies data from the memptr to shared memory.

Use this method to copy data from the given external memptr to the data segment of the shared memory.

Parameters
memptrthe memptr to copy from

Definition at line 726 of file shm.cpp.

References _data_size, and _memptr.

◆ set_destroy_on_delete()

void fawkes::SharedMemory::set_destroy_on_delete ( bool  destroy)

Set deletion behaviour.

This has the same effect as the destroy_on_delete parameter given to the constructor.

Parameters
destroyset to true to destroy the shared memory segment on deletion

Definition at line 796 of file shm.cpp.

References _destroy_on_delete.

Referenced by fawkes::BlackBoardMemoryManager::BlackBoardMemoryManager().

◆ set_swapable()

void fawkes::SharedMemory::set_swapable ( bool  swapable)

Set shared memory swapable.

Setting memory unswapable (in terms of Linux memory management: lock all pages related to this memory segment) will only succeed for very small portions of memory. A resource limit is implied (see getrlimit(2)). In most cases the maximum amout of locked memory is about 32 KB.

Parameters
swapableset to true, if memory should be allowed to be swaped out.

Definition at line 847 of file shm.cpp.

Referenced by fawkes::BlackBoardMemoryManager::BlackBoardMemoryManager().

◆ shmem_id()

int fawkes::SharedMemory::shmem_id ( ) const

Get shared memory ID.

Returns
shared memory ID

Definition at line 704 of file shm.cpp.

◆ try_lock_for_read()

bool fawkes::SharedMemory::try_lock_for_read ( )

Try to aquire lock on shared memory segment for reading.

If the shared memory segment is protected by an associated semaphore it can be locked. With tryLock() you can try to aquire the lock, but the method will not block if it cannot get the lock but simply return false. This can be used to detect if memory is locked:

if (mem->tryLock()) {
// was not locked
mem->unlock();
} else {
// is locked
}
Returns
true if the lock was acquired for reading, false if lock was not acquired.
See also
isProtected()
unlock()
lock()

Definition at line 897 of file shm.cpp.

References fawkes::SemaphoreSet::try_lock().

◆ try_lock_for_write()

bool fawkes::SharedMemory::try_lock_for_write ( )

Try to aquire lock on shared memory segment for writing.

If the shared memory segment is protected by an associated semaphore it can be locked. With tryLock() you can try to aquire the lock, but the method will not block if it cannot get the lock but simply return false. This can be used to detect if memory is locked:

if (mem->tryLock()) {
// was not locked
mem->unlock();
} else {
// is locked
}
Returns
true if the lock was acquired for writing, false if lock was not acquired.
See also
isProtected()
unlock()
lock()

Definition at line 953 of file shm.cpp.

References MaxNumConcurrentReaders, fawkes::SemaphoreSet::try_lock(), and fawkes::SemaphoreSet::unlock().

Referenced by fawkes::BlackBoardMemoryManager::try_lock().

◆ unlock()

void fawkes::SharedMemory::unlock ( )

Unlock memory.

If the shared memory segment is protected by an associated semaphore it can be locked. With unlock() you lift the lock on the memory. Be aware that unlocking a not-locked piece of memory will result in havoc and insanity! Have only exactly guaranteed pairs of lock/successful tryLock() and unlock()!

Definition at line 985 of file shm.cpp.

References MaxNumConcurrentReaders, and fawkes::SemaphoreSet::unlock().

Referenced by fawkes::BlackBoardMemoryManager::alloc(), OpenNiPointCloudThread::finalize(), fawkes::BlackBoardMemoryManager::free(), firevision::FuseImageContent::FuseImageContent(), FvRetrieverThread::loop(), OpenNiPclOnlyThread::loop(), OpenNiImageThread::loop(), Bumblebee2Thread::loop(), and fawkes::BlackBoardMemoryManager::unlock().

Member Data Documentation

◆ _data_size

fawkes::SharedMemory::_data_size
protected

Size of the data segment only.

Definition at line 179 of file shm.h.

Referenced by attach(), data_size(), set(), and SharedMemory().

◆ _destroy_on_delete

fawkes::SharedMemory::_destroy_on_delete
protected

destroy on delete.

If true before free() segment is destroyed.

Definition at line 182 of file shm.h.

Referenced by free(), set_destroy_on_delete(), SharedMemory(), and ~SharedMemory().

◆ _header

◆ _is_read_only

◆ _magic_token

fawkes::SharedMemory::_magic_token
protected

Magic token.

Definition at line 184 of file shm.h.

Referenced by attach(), SharedMemory(), and ~SharedMemory().

◆ _mem_size

fawkes::SharedMemory::_mem_size
protected

Total size of the segment, including headers.

Definition at line 178 of file shm.h.

Referenced by attach().

◆ _memptr

◆ _shm_header

fawkes::SharedMemory::_shm_header
protected

general header as stored in the shared memory segment

Definition at line 186 of file shm.h.

Referenced by add_semaphore(), attach(), free(), SharedMemory(), and ~SharedMemory().

◆ _shm_magic_token

fawkes::SharedMemory::_shm_magic_token
protected

Magic token as stored in the shared memory segment.

Definition at line 185 of file shm.h.

Referenced by attach(), free(), and SharedMemory().

◆ _shm_offset

fawkes::SharedMemory::_shm_offset
protected

Offset to the master's base addr.

Definition at line 188 of file shm.h.

Referenced by addr(), attach(), and ptr().

◆ _shm_upper_bound

fawkes::SharedMemory::_shm_upper_bound
protected

Upper bound of memory.

Used by ptr to determine if the given address is valid.

Definition at line 187 of file shm.h.

Referenced by attach(), and ptr().

◆ _should_create

fawkes::SharedMemory::_should_create
protected

Create shared memory segment.

If true before attach shared memory segment is created if it does not exist.

Definition at line 183 of file shm.h.

Referenced by attach(), and SharedMemory().

◆ MagicTokenSize

const unsigned int fawkes::SharedMemory::MagicTokenSize = MAGIC_TOKEN_SIZE
static

The magic token size.

Your magic token identifier may have an arbitrary size. It is truncated at MagicTokenSize bytes or filled with zeros up to a length of MagicTokenSize bytes.

Definition at line 54 of file shm.h.

Referenced by attach(), fawkes::SharedMemory::SharedMemoryIterator::operator++(), and SharedMemory().

◆ MaxNumConcurrentReaders

const short fawkes::SharedMemory::MaxNumConcurrentReaders = 8
static

Maximum number of concurrent readers.

This constant defines how many readers may concurrently read from shared memory segments.

Definition at line 55 of file shm.h.

Referenced by add_semaphore(), lock_for_write(), try_lock_for_write(), and unlock().


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