feisty meow concerns codebase  2.140
application::shared_memory Class Reference

Implements storage for memory that can be shared between threads. More...

#include <shared_memory.h>

Inheritance diagram for application::shared_memory:
Collaboration diagram for application::shared_memory:

Public Member Functions

 shared_memory (int size, const char *identity)
 a shared chunk of the "size" specified will be created. More...
 
virtual ~shared_memory ()
 cleans up the shared bit of memory as far as we're concerned. More...
 
 DEFINE_CLASS_NAME ("shared_memory")
 
bool valid () const
 this must be true for the shared_memory to be usable. More...
 
int size () const
 returns the size of the shared chunk of memory. More...
 
const basis::astringidentity () const
 provides a peek at the name that this chunk was constructed with. More...
 
bool first_usage (basis::abyte *locked_memory, int max_compare)
 returns true if the "locked_memory" was just created. More...
 
basis::abytelock ()
 locks the shared memory and returns a pointer to the storage. More...
 
void unlock (basis::abyte *&to_unlock)
 returns control of the shared memory so others can access it. More...
 

Static Public Member Functions

static basis::astring unique_shared_mem_identifier (int sequencer)
 returns a unique identifier for a shared memory chunk. More...
 

Detailed Description

Implements storage for memory that can be shared between threads.

Provides a means to create shared memory chunks and access them from anywhere in a program or from cooperating programs.

Definition at line 30 of file shared_memory.h.

Constructor & Destructor Documentation

◆ shared_memory()

application::shared_memory::shared_memory ( int  size,
const char *  identity 
)

a shared chunk of the "size" specified will be created.

it is named by the "identity" string. that "identity" uniquely points to one shared chunk on this machine. note that if this object is the first to create the chunk of memory, then all of the contents are initialized to zero. this can be used to determine if the chunk needs higher level, application-specific initialization.

Definition at line 49 of file shared_memory.cpp.

References FUNCDEF, identity(), processes::rendezvous::lock(), basis::negative(), O_CREAT, O_EXCL, O_RDWR, size(), and processes::rendezvous::unlock().

◆ ~shared_memory()

application::shared_memory::~shared_memory ( )
virtual

cleans up the shared bit of memory as far as we're concerned.

if some other instance still has it opened, then it isn't destroyed for real yet.

Definition at line 124 of file shared_memory.cpp.

References close, identity(), NULL_POINTER, and basis::WHACK().

Member Function Documentation

◆ DEFINE_CLASS_NAME()

application::shared_memory::DEFINE_CLASS_NAME ( "shared_memory"  )

◆ first_usage()

bool application::shared_memory::first_usage ( basis::abyte locked_memory,
int  max_compare 
)

returns true if the "locked_memory" was just created.

that is assuming that a user of the shared memory will set the first "max_compare" bytes to something other than all zeros. this is really just a test of whether bytes zero through bytes "max_compare" - 1 are currently zero, causing a return of true. seeing anything besides zero causes a false return.

Definition at line 162 of file shared_memory.cpp.

◆ identity()

const astring & application::shared_memory::identity ( ) const

provides a peek at the name that this chunk was constructed with.

Definition at line 142 of file shared_memory.cpp.

Referenced by shared_memory(), and ~shared_memory().

◆ lock()

abyte * application::shared_memory::lock ( )

locks the shared memory and returns a pointer to the storage.

the synchronization supported is only within this program; this type of shared memory is not intended for access from multiple processes, just for access from multiple threads in the same app.

Definition at line 169 of file shared_memory.cpp.

References processes::rendezvous::lock().

◆ size()

int application::shared_memory::size ( ) const
inline

returns the size of the shared chunk of memory.

Definition at line 51 of file shared_memory.h.

Referenced by shared_memory().

◆ unique_shared_mem_identifier()

astring application::shared_memory::unique_shared_mem_identifier ( int  sequencer)
static

returns a unique identifier for a shared memory chunk.

the id returned is unique on this host for this particular process and application, given a "sequencer" number that is up to the application to keep track of uniquely. the values from -100 through -1 are reserved for hoople library internals.

Definition at line 152 of file shared_memory.cpp.

◆ unlock()

void application::shared_memory::unlock ( basis::abyte *&  to_unlock)

returns control of the shared memory so others can access it.

calls to lock() must be paired up with calls to unlock().

Definition at line 175 of file shared_memory.cpp.

References processes::rendezvous::unlock().

◆ valid()

bool application::shared_memory::valid ( ) const
inline

this must be true for the shared_memory to be usable.

if it's false, then the memory chunk was never created.

Definition at line 48 of file shared_memory.h.


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