qLibc
qshm.c File Reference

Shared-memory APIs. More...

Go to the source code of this file.

Functions

int qshm_init (const char *keyfile, int keyid, size_t size, bool recreate)
 Initialize shared-memory.
 
int qshm_getid (const char *keyfile, int keyid)
 Get shared memory identifier by keyfile and keyid for existing shared memory.
 
void * qshm_get (int shmid)
 Get a pointer of shared memory.
 
bool qshm_free (int shmid)
 De-allocate shared memory.
 

Detailed Description

Shared-memory APIs.

Note
[your header file]
struct SharedData {
(... structrue definitions ...)
}
[shared memory creater]
// create shared memory
int shmid = qshm_init("/some/file/for/generating/unique/key", 's', sizeof(struct SharedData), true);
if(shmid < 0) {
printf("ERROR: Can't initialize shared memory.\n");
return -1;
}
// get shared memory pointer
struct SharedData *sdata = (SharedData *)qshm_get(shmid);
if(sdata == NULL) {
printf("ERROR: Can't get shared memory.\n");
return -1;
}
[shared memory user]
// get shared memory id
int shmid = qshm_getid("/some/file/for/generating/unique/key", 's');
if(shmid < 0) {
printf("ERROR: Can't get shared memory id.\n");
return -1;
}
// get shared memory pointer
struct SharedData *sdata = (SharedData *)qshm_get(shmid);
if(sdata == NULL) {
printf("ERROR: Can't get shared memory.\n");
return -1;
}
void * qshm_get(int shmid)
Get a pointer of shared memory.
Definition qshm.c:149
int qshm_init(const char *keyfile, int keyid, size_t size, bool recreate)
Initialize shared-memory.
Definition qshm.c:91
int qshm_getid(const char *keyfile, int keyid)
Get shared memory identifier by keyfile and keyid for existing shared memory.
Definition qshm.c:127

Definition in file qshm.c.

Function Documentation

◆ qshm_init()

int qshm_init ( const char *  keyfile,
int  keyid,
size_t  size,
bool  recreate 
)

Initialize shared-memory.

Parameters
keyfileseed for generating unique IPC key
keyidseed for generating unique IPC key
sizesize of shared memory
recreateset to true to re-create shared-memory if already exists
Returns
non-negative shared memory identifier if successful, otherwise returns -1

Definition at line 91 of file qshm.c.

◆ qshm_getid()

int qshm_getid ( const char *  keyfile,
int  keyid 
)

Get shared memory identifier by keyfile and keyid for existing shared memory.

Parameters
keyfileseed for generating unique IPC key
keyidseed for generating unique IPC key
Returns
non-negative shared memory identifier if successful, otherwise returns -1

Definition at line 127 of file qshm.c.

◆ qshm_get()

void * qshm_get ( int  shmid)

Get a pointer of shared memory.

Parameters
shmidshared memory identifier
Returns
a pointer of shared memory

Definition at line 149 of file qshm.c.

◆ qshm_free()

bool qshm_free ( int  shmid)

De-allocate shared memory.

Parameters
shmidshared memory identifier
Returns
true if successful, otherwise returns false

Definition at line 167 of file qshm.c.