mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
mx::ipc::sharedMemSegment Class Reference

A c++ class to manage a System V shared memory segment with memory mapping.

The associated functions create and/or attach to a shared memory segment. If a segment is created, a block of size sizeof(uintptr_t) is reserved where the address is stored. This address is used when subsequently attaching to the segment so that pointers stored in the block are valid, etc.

Definition at line 52 of file sharedMemSegment.hpp.

#include <ipc/sharedMemSegment.hpp>

Public Member Functions

void initialize ()
 Initialize the class. More...
 
key_t setKey (const char *path, const int id)
 Set the key. More...
 
int create (size_t sz)
 Create and attach to the segment. More...
 
int attach (bool donot_set_addr=false)
 Attach to a segment without creating it. More...
 
int detach ()
 Detach from the segment. More...
 

Public Attributes

char key_path [MX_IPC_KEYLEN]
 The path to use for key creation. More...
 
int key_id
 The id to use for key creation. More...
 
key_t key
 The shared memory key. More...
 
int shmemid
 The shared memory id associated with the key. More...
 
void * addr
 The base address of the segment. More...
 
size_t size
 The size of the segment. More...
 
int attached
 Flag indicating whether or not the segment is attached. More...
 

Member Function Documentation

◆ attach()

int mx::ipc::sharedMemSegment::attach ( bool  donot_set_addr = false)

Attach to a segment without creating it.

If donot_set_addr == false, then the address is mapped to match that stored in the first uintptr_t of the segment.

Parameters
donot_set_addrif > 0 (true) then the address is not mapped to match that stored in the segment

Definition at line 97 of file sharedMemSegment.cpp.

References addr, attached, key, shmemid, and size.

Referenced by create().

◆ create()

int mx::ipc::sharedMemSegment::create ( size_t  sz)

Create and attach to the segment.

A segment of size = sz + sizeof(uintptr_t) is actually created.

Note
key must be set before calling this function.
Parameters
szthe size of the segment to create

Definition at line 69 of file sharedMemSegment.cpp.

References addr, attach(), key, and shmemid.

◆ detach()

int mx::ipc::sharedMemSegment::detach ( )

Detach from the segment.

Definition at line 160 of file sharedMemSegment.cpp.

References addr, and attached.

◆ initialize()

void mx::ipc::sharedMemSegment::initialize ( )

Initialize the class.

Definition at line 35 of file sharedMemSegment.cpp.

References addr, attached, key, key_id, shmemid, and size.

◆ setKey()

key_t mx::ipc::sharedMemSegment::setKey ( const char *  path,
const int  id 
)

Set the key.

If path == 0, then the key is set directly to id, without using ftok.

Parameters
pathis the full path to use in a call to ftok
idis the id number to use in a call to ftok
Returns
the key value, which is also set in the msgq

Definition at line 47 of file sharedMemSegment.cpp.

References key, key_id, and key_path.

Member Data Documentation

◆ addr

void* mx::ipc::sharedMemSegment::addr

The base address of the segment.

Definition at line 68 of file sharedMemSegment.hpp.

Referenced by attach(), create(), detach(), and initialize().

◆ attached

int mx::ipc::sharedMemSegment::attached

Flag indicating whether or not the segment is attached.

Definition at line 74 of file sharedMemSegment.hpp.

Referenced by attach(), detach(), and initialize().

◆ key

key_t mx::ipc::sharedMemSegment::key

The shared memory key.

Definition at line 62 of file sharedMemSegment.hpp.

Referenced by attach(), create(), initialize(), and setKey().

◆ key_id

int mx::ipc::sharedMemSegment::key_id

The id to use for key creation.

Definition at line 59 of file sharedMemSegment.hpp.

Referenced by initialize(), and setKey().

◆ key_path

char mx::ipc::sharedMemSegment::key_path[MX_IPC_KEYLEN]

The path to use for key creation.

Definition at line 56 of file sharedMemSegment.hpp.

Referenced by setKey().

◆ shmemid

int mx::ipc::sharedMemSegment::shmemid

The shared memory id associated with the key.

Definition at line 65 of file sharedMemSegment.hpp.

Referenced by attach(), create(), and initialize().

◆ size

size_t mx::ipc::sharedMemSegment::size

The size of the segment.

Definition at line 71 of file sharedMemSegment.hpp.

Referenced by attach(), and initialize().


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