The qDecoder Project

qShm.c File Reference

Shared Memory Handling API. More…


Functions

int qShmInit (const char *keyfile, int keyid, size_t size, bool ifexistdestroy)
 Initialize shared-memory.
int qShmGetId (const char *keyfile, int keyid)
 Get shared memory identifier by keyfile and keyid which are used for qShmInit().
void * qShmGet (int shmid)
 Get a pointer of shared memory.
bool qShmFree (int shmid)
 De-allocate shared memory.


Detailed Description

Shared Memory Handling API.

Note:
   [your header file]
   struct SharedData {
     (... structrue definitions ...)
   }
   [shared memory creater]
   // create shared memory
   int shmid = qShmInit("/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 *)qShmGet(shmid);
   if(sdata == NULL) {
     printf("ERROR: Can't get shared memory.n");
     return -1;
   }
   [shared memory user]
   // get shared memory id
   int shmid = qShmGetId("/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 *)qShmGet(shmid);
   if(sdata == NULL) {
     printf("ERROR: Can't get shared memory.n");
     return -1;
   }

Function Documentation

int qShmInit ( const char *  keyfile,
int  keyid,
size_t  size,
bool  ifexistdestroy 
)

Initialize shared-memory.

Parameters:
keyfile seed for generating unique IPC key
keyid seed for generating unique IPC key
size size of shared memory
ifexistdestroy set to true for if shared memory already exists, try to destroy it first
Returns:
non-negative shared memory identifier if successful, otherwise returns -1

int qShmGetId ( const char *  keyfile,
int  keyid 
)

Get shared memory identifier by keyfile and keyid which are used for qShmInit().

Parameters:
keyfile seed for generating unique IPC key
keyid seed for generating unique IPC key
Returns:
non-negative shared memory identifier if successful, otherwise returns -1

void* qShmGet ( int  shmid  ) 

Get a pointer of shared memory.

Parameters:
shmid shared memory identifier
Returns:
a pointer of shared memory

bool qShmFree ( int  shmid  ) 

De-allocate shared memory.

Parameters:
shmid shared memory identifier
Returns:
true if successful, otherwise returns false


[Home] [About] [Examples] [Changes] [Download] [SVN Repository] [Install] [Reference]