Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

CascadeSharedList Class Reference

a process safe shared memory linked list with PID tracking More...

#include <cascade/interprocess/CascadeSharedList.h>

Inheritance diagram for CascadeSharedList:

CascadeObject List of all members.

Public Types

typedef void Entry
typedef bool() EnumProc (Entry *pEnry, void *pClientData)
enum  { kMaxHeaderSize = 0xF000, kMaxEntrySize = 0xF000, kMaxEntries = 0xF000 }

Public Member Functions

 CascadeSharedList ()
virtual ~CascadeSharedList ()
bool Open (const char *pName, u16 nHeaderSize, u16 nEntrySize, u16 nMaxEntries, bool bCreateIfNecessary)
bool Close ()
bool MapLock ()
bool Unlock ()
bool InsertAtHead (void *pData)
bool AddToTail (void *pData)
bool Insert (void *pData, Entry *pEntryToInsertBefore)
u16 GetNumEntries ()
u16 GetMaxEntries ()
u16 GetNumEntriesWatermark ()
EntryGetFirstEntry ()
EntryGetLastEntry ()
EntryGetNextEntry (Entry *pEntry)
EntryGetPrevEntry (Entry *pEntry)
int GetEntryPID (Entry *pEntry)
void ResetEntryPID (Entry *pEntry, int nPID)
bool IsStalePIDEntry (Entry *pEntry)
bool MoveEntry (Entry *pEntry, Entry *pEntryToMoveBefore)
bool MoveEntryToHead (Entry *pEntry)
bool MoveEntryToTail (Entry *pEntry)
bool RemoveEntry (Entry *pEntry)
void PurgeDeadPIDEntries ()
void * GetListHeader ()
bool Enumerate (EnumProc *pEnumProc, void *pClientData)
bool PIDPurgeEnumerate (EnumProc *pEnumProc, void *pClientData)

Detailed Description

a process safe shared memory linked list with PID tracking

Skip the description

CascadeSharedList provides a named doubly-linked list in shared memory with an optional list-header and optional automatic purging of list entries when the process that added the entry dies.

CascadeSharedList is based on CascadeSharedMemZone.

Note:
While CascadeSharedList provides process safe access to shared memory, CascadeSharedList instances themselves are not thread safe. Either allow each of your threads to have it's own CascadeSharedList instance referring to the same named shared list (preferred, no penalty), or protect simultaneous access to a CascadeSharedList instance with a CascadeMutex.

class CascadeSharedList


Member Typedef Documentation

typedef void CascadeSharedList::Entry
 

the type of a list entry

CascadeSharedList returns Entry *'s to you the client. These Entry *'s are pointers to copies of data that you've added to the list. These copies are owned and managed by CascadeSharedList and are only valid while the list is MapLocked. You may cast these pointers to your client data type, but be advised if you want to use the data beyond the lifetime of when you have the list MapLocked you must copy the data.

See also:
GetFirstEntry(), GetLastEntry(), GetPrevEntry(), GetNextEntry()

typedef bool() CascadeSharedList::EnumProc(Entry *pEnry, void *pClientData)
 

Callback function for enumerating entries in the list

Clients provide their own EnumProc to the Enumerate() and PIDPurgeEnumerate() functions. Your custom EnumProc will get called back by these functions, one for each qualified entry in the list.

Parameters:
pEnry the list entry
pClientData the void * pClientData passed thru from the enumeration functions
Returns:
You should return true to continue enumeration, false to abort it
See also:
Enumerate(), PIDPurgeEnumerate()


Member Enumeration Documentation

anonymous enum
 

Enumeration values:
kMaxHeaderSize  the maximum size in bytes of a SharedList header
kMaxEntrySize  the maximum size in bytes of a SharedList entry
kMaxEntries  the maximum number of SharedList entries


Constructor & Destructor Documentation

CascadeSharedList::CascadeSharedList  ) 
 

the default constructor - lightweight

This default constructor is lightweight.

virtual CascadeSharedList::~CascadeSharedList  )  [virtual]
 

destructor

This destructor will unlock and close the SharedMemZone if necessary.


Member Function Documentation

bool CascadeSharedList::AddToTail void *  pData  ) 
 

adds data to the tail of the list

AddToTail() makes a copy of *pData (in the amount of bytes that the list was opened with), and makes an Entry out of it and adds it to the end of the list returning true if successful, false otherwise. Note that if you modify *pData after adding it to the list, it doesn't modify the copy in the list.

Parameters:
pData the data to add
Returns:
whether or not the addition was successful

bool CascadeSharedList::Close  ) 
 

closes the SharedList

Call Close() to close a previously opened shared list. When the last handle to a shared list is closed, the operating system shared memory object is destroyed.

This function is provided for convenience - the destructor ~CascadeSharedList() will automatically Close() the shared list if it is still open when the destructor is called. Close() will automatically unlock the shared list if it is map-locked, but good programming practice dictates that you should unlock the shared memory zone yourself as soon as you are done using it.

Returns:
whether or not the close was successful
See also:
Open()

Unlock(), ~CascadeSharedList()

bool CascadeSharedList::Enumerate EnumProc pEnumProc,
void *  pClientData
 

enumerates the entries in the list

Enumerate() will enumerate through the entries in the list calling *pEnumProc for each entry encountered. Clients should return true from their enum procs to continue enumeration, false to abort enumeration. Enumerate() returns true if the enumeration was carried to completion, false if it was aborted.

Parameters:
pEnumProc your EnumProc to be called back for each list entry
pClientData a void * that will be passed straight thru to your EnumProc
Returns:
true if enumeration carried through to completion, false if enumeration was aborted
See also:
EnumProc

PIDPurgeEnumerate()

int CascadeSharedList::GetEntryPID Entry pEntry  ) 
 

gets the PID of the process that added the entry to the list

GetEntryPID() returns the PID of the process that added the entry to the list.

Parameters:
pEntry the entry for which to retrieve the PID
Returns:
the PID of the process that added the entry to the list

Entry* CascadeSharedList::GetFirstEntry  ) 
 

gets the first entry in the list

GetFirstEntry() returns the Entry * of the first entry in the list or NULL if the list is empty.

Returns:
the first entry in the list or NULL if the list is empty

Entry* CascadeSharedList::GetLastEntry  ) 
 

gets the last entry in the list

GetLastEntry() returns the Entry * of the last entry in the list or NULL if the list is entry.

Returns:
the last entry in the list or NULL if the list is empty

void* CascadeSharedList::GetListHeader  ) 
 

gets a pointer to the header of the list

GetListHeader() returns a pointer to the memory area in the shared memory zone for you, the client's list header. This pointer is only valid while you have the list map-locked and points to the number of bytes passed in as the value of the paramter nHeaderSize to the Open() function. This list header is initialized to all zeros when the list is first created. GetListHeader() returns NULL if the list is not map-locked when GetListHeader() is called, NULL also if the client specified a size of zero bytes for his list header.

Returns:
a pointer to the list header or NULL if there is no list header

u16 CascadeSharedList::GetMaxEntries  ) 
 

gets the maximum number of entries the list can hold

GetMaxEntries() returns the maximum number of entries that the list can hold.

Returns:
the maximum number of entries the list can hold

Entry* CascadeSharedList::GetNextEntry Entry pEntry  ) 
 

gets the next entry in the list

GetNextEntry() returns the Entry * of the next entry in the list after pEntry or NULL if pEntry is the last entry in the list.

Parameters:
pEntry the entry for which to retrieve the next entry
Returns:
the next entry in the list or NULL if the list end has been reached

u16 CascadeSharedList::GetNumEntries  ) 
 

gets the number of entries in the list

GetNumEntries() returns the number of entries in the list.

Note:
This may not be the same number of entries encountered during list enumeration with PIDPurgeEnumerate since stale pid entries are removed during enumeration by that function.

To iterate through a SharedList do this:

            CascadeSharedList Entry * pEntry = list.GetFirstEntry();
            while (NULL != pEntry) { pEntry = list.GetNextEntry(pEntry); } 
Returns:
the number of entries in the list

u16 CascadeSharedList::GetNumEntriesWatermark  ) 
 

gets the maximum number of entries the list has actually held

GetNumEntriesWatermark() returns the highest number of entries (the watermark) that the list has ever held.

Returns:
the maximum number of entries the list has ever held

Entry* CascadeSharedList::GetPrevEntry Entry pEntry  ) 
 

gets the previous entry in the list

GetPrevEntry() returns the Entry * of the previous entry in the list before pEntry or NULL if pEntry is the first entry in the list.

Parameters:
pEntry the entry for which to retrieve the previous entry
Returns:
the previous entry in the list or NULL if the list beginning has been reached

bool CascadeSharedList::Insert void *  pData,
Entry pEntryToInsertBefore
 

inserts data at a specific point in the list

Insert() makes a copy of *pData (in the amount of bytes that the list was opened with), and makes an Entry out of it and inserts it in the list immediately before the entry identified by pEntryToInsertBefore, returning true if successful, false otherwise. See the functions below that return Entry *'s to get a valid Entry * you can use as the argument for pEntryToInsertToBefore. Note that if you modify *pData after inserting it into the list, it doesn't modify the copy in the list.

Parameters:
pData the data to add
pEntryToInsertBefore the entry in the list to insert the data before
Returns:
whether or not the insertion was successful

bool CascadeSharedList::InsertAtHead void *  pData  ) 
 

inserts data at the head of the list

InsertAtHead() makes a copy of *pData (in the amount of bytes that the list was opened with), and makes an Entry out of it and inserts it at the head of the list returning true if successful, false otherwise. Note that if you modify *pData after inserting it into the list, it doesn't modify the copy in the list.

Parameters:
pData the data to insert
Returns:
whether or not the insertion was successful

bool CascadeSharedList::IsStalePIDEntry Entry pEntry  ) 
 

determines whether or not the process that added the entry to the list is still running

IsStalePIDEntry() returns true if the entry referred to by pEntry has a stale PID, false if it does not have a stale pid. (An entry has a stale pid if the process that added the entry to the list no longer exists).

Parameters:
pEntry the entry for consideration
Returns:
whether or not the process that added the list entry no longer exists (is stale)

bool CascadeSharedList::MapLock  ) 
 

locks and maps the shared list into your processes' address space

Call MapLock() to lock the shared list into your process' memory space. None of the list accessor functions will work unless it is maplocked successfully.

Returns:
whether or not the map-lock was successful
See also:
Unlock()

bool CascadeSharedList::MoveEntry Entry pEntry,
Entry pEntryToMoveBefore
 

moves an entry to a specific point in th elist

MoveEntry() moves pEntry to the position in the list before pEntryToMoveBefore returning true if successful, false otherwise. (false is only returned if the parameters passed in are bad).

Parameters:
pEntry the entry to move
pEntryToMoveBefore the entry in the list to move pEntry before
Returns:
whether or not the move was successful

bool CascadeSharedList::MoveEntryToHead Entry pEntry  ) 
 

moves an entry to the head of the list

MoveEntryToHead() moves the entry pEntry to the head of the list returning true if successful, false otherwise.

Parameters:
pEntry the entry to move
Returns:
whether or not the move was successful

bool CascadeSharedList::MoveEntryToTail Entry pEntry  ) 
 

moves an entry to the tail of the list

MoveEntryToTail() moves the entry pEntry to the tail of the list returning true if successful, false otherwise.

Parameters:
pEntry the entry to move
Returns:
whether or not the move was successful

bool CascadeSharedList::Open const char *  pName,
u16  nHeaderSize,
u16  nEntrySize,
u16  nMaxEntries,
bool  bCreateIfNecessary
 

opens a SharedList

Open() opens the a handle to the shared list named pName.

nHeaderSize specifies an optional size for a list header - memory stored in the shared memory zone along with the list, but not considered an element of the list. The client (you) can access this memory by calling GetListHeader() below. Valid values for nHeaderSize are [0..kMaxHeaderSize].

nEntrySize specifies the size of the entries in the doubly-linked list. This size is the size of the data to be stored in the list. It is exclusive of list node data which is handled internally by CascadeSharedList and need not concern clients. Valid values for nEntrySize are [1..kMaxEntrySize].

nMaxEntries specifies the maximum number of entries that may be stored in the list. Initially the list starts out empty, and may only grow up to nMaxEntries number of elements. Valid values for nMaxEntries are [1..kMaxEntries].

bCreateIfNecessary - if bCreateIfNecessary is true, the shared list will be created if it doesn't already exist (in shared memory). If bCreateIfNecessary is false, the call to Open() will fail unless the list already exists in shared memory.

Open() returns true if successful, false otherwise. Open() will return false if the named list already exists but different parameters were specified than that of the original call to Open() that caused the list to be created.

Parameters:
pName the name of the shared list
nHeaderSize the header size for the shared list
nEntrySize the size of entries in the shared list
nMaxEntries the maximum number of entries the shared list should be prepared to hold
bCreateIfNecessary whether or not to create the SharedList if it doesn't already exist
Returns:
whether or not the open was successful
See also:
Close(), ~CascadeSharedList()

GetListHeader(), GetFirstEntry(), GetLastEntry()

bool CascadeSharedList::PIDPurgeEnumerate EnumProc pEnumProc,
void *  pClientData
 

enumerates the entries in the list skipping over and removing stale entries

PIDPurgeEnumerate() will enumerate through the entries in the list calling *pEnumProc for each entry encountered that does not have a stale PID. Entries that do have stale pids (their adding processes have since died) will be purged from the list during enumeration. Clients should return true from their enum procs to continue enumeration, false to abort enumeration. PIDPurgeEnumerate() returns true if the enumeration was carried to completion, false if it was aborted.

Parameters:
pEnumProc your EnumProc to be called back for each list entry
pClientData a void * that will be passed straight thru to your EnumProc
Returns:
true if enumeration carried through to completion, false if enumeration was aborted
See also:
EnumProc

Enumerate()

void CascadeSharedList::PurgeDeadPIDEntries  ) 
 

removes all entries that were added by processes that no longer exist

PurgeDeadPIDEntries() will iterate through the list, manually removing all entries that were added by processes that no longer exist.

bool CascadeSharedList::RemoveEntry Entry pEntry  ) 
 

removes an entry from the list

RemoveEntry() removes the entry pEntry from the list. pEntry must not be accessed by you the client after RemoveEntry returns. RemoveEntry() returns true if successful, false otherwise. false is only returned if a bad pEntry parameter is passed in.

Parameters:
pEntry the entry to remove
Returns:
whether or not the removal was successful

void CascadeSharedList::ResetEntryPID Entry pEntry,
int  nPID
 

resets the PID the entry is marked with

ResetEntryPID() resets (reassigns) the PID of the process that added the entry to the list.

Parameters:
pEntry the entry for which to reset the PID
nPID the new PID to associate the entry with

bool CascadeSharedList::Unlock  ) 
 

unlocks the shared list

Call Unlock() to unmap and release the shared list. Calls to MapLock() may nest so be sure there is a corresponding call to Unlock() for every call to MapLock().

Returns:
whether or not the unlocking was successful
See also:
MapLock()

Close(), ~CascadeSharedList()


The documentation for this class was generated from the following file:
Generated on Sun Jul 24 14:27:20 2005 for Cascade Library by  doxygen 1.4.1