VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
G::SharedMemory Class Reference

A POSIX shared memory class. More...

#include <gsharedmemory.h>

Classes

struct  Control
 Overload discriminator for G::SharedMemory. More...
 

Public Member Functions

 SharedMemory (size_t size)
 Constructor for a new anonymous segment, typically used across a fork(). More...
 
 SharedMemory (int fd)
 Constructor for a segment inherited via a file descriptor, typically across fork()/exec(). More...
 
 SharedMemory (const std::string &name)
 Constructor for an existing named segment. More...
 
 SharedMemory (const std::string &name, size_t size)
 Constructor for a new named segment. More...
 
 SharedMemory (size_t size, Control)
 Constructor overload for control segments that might contain semaphores. More...
 
 SharedMemory (int fd, Control)
 Constructor overload for control segments that might contain semaphores. More...
 
 SharedMemory (const std::string &name, Control)
 Constructor overload for control segments that might contain semaphores. More...
 
 SharedMemory (const std::string &name, size_t size, Control)
 Constructor overload for control segments that might contain semaphores. More...
 
 ~SharedMemory ()
 Destructor. More...
 
size_t size () const
 Returns the segment size. This is affected by remap().
 
void unlink ()
 Unlinks the segment from the filesystem. More...
 
int fd () const
 Returns the file descriptor. Returns -1 for anonymous segments.
 
void inherit ()
 Marks fd() as inherited across exec() ie. no-close-on-exec.
 
void * ptr () const
 Returns the mapped address.
 
bool remap (size_t, bool may_move, bool no_throw=false)
 Resizes and remaps the shared memory. More...
 

Static Public Member Functions

static std::string delete_ (const std::string &name, bool control_segment)
 Unlinks the segment from the filesystem. More...
 
static void cleanup (SignalSafe, const char *os_name, void(*fn)(SignalSafe, void *))
 An exit handler that unlinks the named shared memory segment and calls the user's function for the memory contents. More...
 
static void trap (SignalSafe, const char *os_name, void(*fn)(SignalSafe, void *))
 An exit handler that calls the user's function on the shared memory contents. More...
 
static std::string osName (const std::string &name)
 Converts the segment name to an internal form that can be passed to cleanup() or trap(). More...
 
static void help (const std::string &)
 Sets some error-message help text for the case when named shared memory cannot be created because it already exists. More...
 
static void prefix (const std::string &)
 Sets a prefix for shared memory names, used by osName(). More...
 
static std::string prefix ()
 Returns the prefix, as set by by a call to the other overload.
 
static void filetext (const std::string &)
 Sets some help text that gets written into the placeholder files.
 
static std::vector< std::string > list (bool os_names=false)
 Returns a list of possible control segment names.
 

Detailed Description

A POSIX shared memory class.

Anonymous shared memory can be inherited across a fork(); shared memory associated with an open file descriptor can be inherited across a fork() and exec(); and shared memory associated with a filesystem path can be shared freely by name.

Definition at line 41 of file gsharedmemory.h.

Constructor & Destructor Documentation

G::SharedMemory::SharedMemory ( size_t  size)
explicit

Constructor for a new anonymous segment, typically used across a fork().

Definition at line 104 of file gsharedmemory.cpp.

G::SharedMemory::SharedMemory ( int  fd)
explicit

Constructor for a segment inherited via a file descriptor, typically across fork()/exec().

The file is closed by the destructor. See also: inherit(), fd()

Definition at line 110 of file gsharedmemory.cpp.

G::SharedMemory::SharedMemory ( const std::string &  name)
explicit

Constructor for an existing named segment.

Throws if it does not exist.

Definition at line 116 of file gsharedmemory.cpp.

G::SharedMemory::SharedMemory ( const std::string &  name,
size_t  size 
)

Constructor for a new named segment.

Throws if it already exists. The destructor will call unlink().

Definition at line 122 of file gsharedmemory.cpp.

G::SharedMemory::SharedMemory ( size_t  size,
Control   
)

Constructor overload for control segments that might contain semaphores.

Definition at line 130 of file gsharedmemory.cpp.

G::SharedMemory::SharedMemory ( int  fd,
Control   
)

Constructor overload for control segments that might contain semaphores.

Definition at line 136 of file gsharedmemory.cpp.

G::SharedMemory::SharedMemory ( const std::string &  name,
Control   
)

Constructor overload for control segments that might contain semaphores.

Definition at line 142 of file gsharedmemory.cpp.

G::SharedMemory::SharedMemory ( const std::string &  name,
size_t  size,
Control   
)

Constructor overload for control segments that might contain semaphores.

Definition at line 148 of file gsharedmemory.cpp.

G::SharedMemory::~SharedMemory ( )

Destructor.

The memory is unmapped and, where relevant, the newly-created named segment is unlink()ed.

Definition at line 156 of file gsharedmemory.cpp.

Member Function Documentation

void G::SharedMemory::cleanup ( SignalSafe  signal_safe,
const char *  os_name,
void(*)(SignalSafe, void *)  fn 
)
static

An exit handler that unlinks the named shared memory segment and calls the user's function for the memory contents.

Use osName() for the segment name.

Definition at line 268 of file gsharedmemory.cpp.

std::string G::SharedMemory::delete_ ( const std::string &  name,
bool  control_segment 
)
static

Unlinks the segment from the filesystem.

Typically used administratively, especially on BSD. Returns a failure reason, or the empty string on success.

Definition at line 396 of file gsharedmemory.cpp.

void G::SharedMemory::help ( const std::string &  s)
static

Sets some error-message help text for the case when named shared memory cannot be created because it already exists.

Definition at line 416 of file gsharedmemory.cpp.

std::string G::SharedMemory::osName ( const std::string &  name)
static

Converts the segment name to an internal form that can be passed to cleanup() or trap().

Definition at line 258 of file gsharedmemory.cpp.

void G::SharedMemory::prefix ( const std::string &  s)
static

Sets a prefix for shared memory names, used by osName().

This method has to be called early and consistently (if called at all) in all cooperating programs. Does nothing if the prefix starts with a double-underscore.

Definition at line 421 of file gsharedmemory.cpp.

bool G::SharedMemory::remap ( size_t  new_size,
bool  may_move,
bool  no_throw = false 
)

Resizes and remaps the shared memory.

Addresses may change if 'may_move' is true, but when false resizing will be less likely to succeed. Throws or returns false on error depending on 'no_throw'. Do not use this if there are semaphores inside the shared memory.

Definition at line 239 of file gsharedmemory.cpp.

void G::SharedMemory::trap ( SignalSafe  signal_safe,
const char *  os_name,
void(*)(SignalSafe, void *)  fn 
)
static

An exit handler that calls the user's function on the shared memory contents.

Use osName() for the segment name.

Definition at line 288 of file gsharedmemory.cpp.

void G::SharedMemory::unlink ( )

Unlinks the segment from the filesystem.

Typically used for early cleanup when doing fork()/exec(). Does not throw.

Definition at line 326 of file gsharedmemory.cpp.


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