53 strncpy(
key_path, path, MX_IPC_KEYLEN);
71 if( (
shmemid = shmget(
key, sz + 1*
sizeof(uintptr_t), IPC_CREAT | 0666))<0)
75 if(shmctl(
shmemid, IPC_RMID, 0) < 0)
77 fprintf(stderr,
"Could not remove shared memory with key %i\n",
key);
82 if((
shmemid = shmget(
key, sz + 1*
sizeof(uintptr_t), IPC_CREAT | 0666))<0)
84 fprintf(stderr,
"Could not create shared memory with key %i\n",
key);
92 *((uintptr_t *)
addr) = (uintptr_t)
addr;
99 struct shmid_ds shmstats;
106 fprintf(stderr,
"Could not remove shared memory with key %i\n",
key);
111 if ((new_addr = shmat(
shmemid, 0, 0)) == (
char *) -1)
113 fprintf(stderr,
"Could not attach to shared memory with key %i\n",
key);
119 if (shmctl(
shmemid, IPC_STAT, &shmstats) < 0)
121 fprintf(stderr,
"Could not get shared memory stats with key %i\n",
key);
125 size = shmstats.shm_segsz;
132 addr = (
void *) *((uintptr_t *) new_addr);
135 if(shmdt(new_addr) != 0)
137 fprintf(stderr,
"Unable to detach from shared memory\n");
144 if ((new_addr = shmat(
shmemid,
addr, 0)) == (
char *) -1)
146 fprintf(stderr,
"Could not re-attach shared memory with key %i\n",
key);
165 if(
addr == 0)
return 0;
170 fprintf(stderr,
"Unable to detach from shared memory\n");
void initialize()
Initialize the class.
int shmemid
The shared memory id associated with the key.
int create(size_t sz)
Create and attach to the segment.
int detach()
Detach from the segment.
int key_id
The id to use for key creation.
size_t size
The size of the segment.
int attach(bool donot_set_addr=false)
Attach to a segment without creating it.
void * addr
The base address of the segment.
key_t key
The shared memory key.
int attached
Flag indicating whether or not the segment is attached.
char key_path[MX_IPC_KEYLEN]
The path to use for key creation.
key_t setKey(const char *path, const int id)
Set the key.
Declarations for the mxlib shared memory facility.