Rudiments
Public Member Functions | Static Public Member Functions | List of all members
sharedmemory Class Reference

Inherits object.

Public Member Functions

 sharedmemory ()
 
 ~sharedmemory ()
 
bool create (key_t key, size_t size, mode_t permissions)
 
bool attach (key_t key, size_t size)
 
bool createOrAttach (key_t key, size_t size, mode_t permissions)
 
void setRemove (bool remove)
 
bool getRemove ()
 
int32_t getId ()
 
void * getPointer ()
 
bool setUserName (const char *username)
 
bool setGroupName (const char *groupname)
 
bool setUserId (uid_t uid)
 
bool setGroupId (gid_t gid)
 
bool setPermissions (mode_t permissions)
 
const char * getUserName ()
 
const char * getGroupName ()
 
uid_t getUserId ()
 
gid_t getGroupId ()
 
mode_t getPermissions ()
 
- Public Member Functions inherited from object
virtual ~object ()
 

Static Public Member Functions

static bool isSupported ()
 

Detailed Description

Shared memory segments allow multiple processes to access a common address space.

Shared memory can be used for interprocess communication and is generally faster than using fifo's or unix sockets.

To synchronize access to shared memory, use the semaphoreset class.

A shared memory segment is owned by a user and group and has access permissions just like a file.

Constructor & Destructor Documentation

◆ sharedmemory()

sharedmemory::sharedmemory ( )

Creates an instance of the sharedmemory class.

◆ ~sharedmemory()

sharedmemory::~sharedmemory ( )

Deletes this instance of the sharedmemory class. Removes the shared memory segment if it was created by this class. If it was just attached to, it is not removed.

Member Function Documentation

◆ attach()

bool sharedmemory::attach ( key_t  key,
size_t  size 
)

Attaches to a shared memory segment of "size" bytes, identified by "key".

◆ create()

bool sharedmemory::create ( key_t  key,
size_t  size,
mode_t  permissions 
)

Creates a shared memory segment of "size" bytes, identified by "key". "key" should be generated using the ftok function. "permissions" sets the access permissions for the segment.

◆ createOrAttach()

bool sharedmemory::createOrAttach ( key_t  key,
size_t  size,
mode_t  permissions 
)

Attempts to create a shared memory segment of "size" bytes, identified by "key". If this fails, it attempts to attach to a shared memory segment identified by "key".

◆ getGroupId()

gid_t sharedmemory::getGroupId ( )

Returns the group id of the group that owns this shared memory segment.

◆ getGroupName()

const char* sharedmemory::getGroupName ( )

Returns the name of the group that owns this shared memory segment

Note that getGroupName() uses the groupentry class. If you are using this method in a multithreaded application, you may need to supply the groupentry class a mutex. See groupentry.h for more detail.

◆ getId()

int32_t sharedmemory::getId ( )

Returns the internal id for the shared memory segment.

◆ getPermissions()

mode_t sharedmemory::getPermissions ( )

Returns the access permissions for this shared memory segment.

◆ getPointer()

void* sharedmemory::getPointer ( )

Returns a pointer to the shared memory segment. Data may be read from or written to the segment using this pointer.

◆ getRemove()

bool sharedmemory::getRemove ( )

Returns true if the shared memory segment will be removed by the destructor and false otherwise.

◆ getUserId()

uid_t sharedmemory::getUserId ( )

Returns the user id of the user that owns this shared memory segment.

◆ getUserName()

const char* sharedmemory::getUserName ( )

Returns the name of the user that owns this shared memory segment

Note that getUserName() uses the userentry class. If you are using this method in a multithreaded application, you may need to supply the userentry class a mutex. See userentry.h for more detail.

◆ isSupported()

static bool sharedmemory::isSupported ( )
static

Returns true if the platform supports shared memory and false otherwise.

◆ setGroupId()

bool sharedmemory::setGroupId ( gid_t  gid)

Makes this shared memory segment owned by the group identified by gid.

◆ setGroupName()

bool sharedmemory::setGroupName ( const char *  groupname)

Makes this shared memory segment owned by the group "groupname".

Note that setGroupName() uses the groupentry class. If you are using this method in a multithreaded application, you may need to supply the groupentry class a mutex. See groupentry.h for more detail.

◆ setPermissions()

bool sharedmemory::setPermissions ( mode_t  permissions)

Sets the access permissions for this shared memory segment to "permissions".

◆ setRemove()

void sharedmemory::setRemove ( bool  remove)

By default, the destructor will remove a sharemd memory segment if it was created during a call to create() or createOrAttach().

This method allows you to manually override that behavior.

If "remove" is true then the destructor will remove the shared memory segment whehter it was created or not. If "remove" is set false then the destructor will not remove the shared memory segment, even if it was created.

Manually setting "remove" false is useful, for example, if an application creates a shared memory segment then forks and wants to delete the shred memory segment in the forked process but does not want the shared memory segment removed from the system.

Manually setting "remove" true is useful, for example, if you want to create a utility for removing arbitray shared memory segments.

◆ setUserId()

bool sharedmemory::setUserId ( uid_t  uid)

Makes this shared memory segment owned by the user identified by uid.

◆ setUserName()

bool sharedmemory::setUserName ( const char *  username)

Makes this shared memory segment owned by the user "username".

Note that setUserName() uses the userentry class. If you are using this method in a multithreaded application, you may need to supply the userentry class a mutex. See userentry.h for more detail.