1 /*-------------------------------------------------------------------------
4 * System V Shared Memory Emulation
6 * Copyright (c) 1999, repas AEG Automation GmbH
10 * $Header: /cvsroot/pgsql/src/backend/port/qnx4/Attic/shm.c,v 1.4 2001/03/22 03:59:43 momjian Exp $
12 *-------------------------------------------------------------------------
36 static struct shm_info *ShmInfo;
39 static int shm_putinfo(struct shm_info * info);
40 static int shm_updinfo(int i, struct shm_info * info);
41 static int shm_getinfo(int shmid, struct shm_info * info);
42 static int shm_getinfobyaddr(const void *addr, struct shm_info * info);
46 shm_putinfo(struct shm_info * info)
52 ShmInfo = calloc(SHMMAX, sizeof(struct shm_info));
55 /* initialize ShmInfo */
56 for (i = 0; i < SHMMAX; i++)
57 ShmInfo[i].shmid = -1;
60 /* search first free element */
62 while (i < SHMMAX && ShmInfo[i].shmid != -1)
70 memcpy(&ShmInfo[i], info, sizeof(struct shm_info));
76 shm_updinfo(int i, struct shm_info * info)
83 memcpy(&ShmInfo[i], info, sizeof(struct shm_info));
89 shm_getinfo(int shmid, struct shm_info * info)
98 while (i < SHMMAX && ShmInfo[i].shmid != shmid)
103 memcpy(info, &ShmInfo[i], sizeof(struct shm_info));
109 shm_getinfobyaddr(const void *addr, struct shm_info * info)
113 if (ShmInfo == (struct shm_info *) - 1)
118 while (i < SHMMAX && ShmInfo[i].addr != addr)
123 memcpy(info, &ShmInfo[i], sizeof(struct shm_info));
130 shmat(int shmid, const void *shmaddr, int shmflg)
132 struct shm_info info;
135 i = shm_getinfo(shmid, &info);
142 info.addr = mmap((void *) shmaddr, info.size,
143 PROT_READ | PROT_WRITE, MAP_SHARED, shmid, 0);
144 if (info.addr == MAP_FAILED)
147 if (shm_updinfo(i, &info) == -1)
157 shmdt(const void *addr)
159 struct shm_info info;
161 if (shm_getinfobyaddr(addr, &info) == -1)
167 return munmap((void *) addr, info.size);
171 shmctl(int shmid, int cmd, struct shmid_ds * buf)
173 struct shm_info info;
174 char name[NAME_MAX + 1];
178 if (shm_getinfo(shmid, &info) == -1)
183 return shm_unlink(itoa(info.key, name, 16));
189 * Can we support IPC_STAT? We only need shm_nattch ... For now,
190 * punt and assume the shm seg does not exist.
200 shmget(key_t key, size_t size, int flags)
202 char name[NAME_MAX + 1];
204 struct shm_info info;
206 if (flags & IPC_CREAT)
208 if (flags & IPC_EXCL)
217 info.shmid = shm_open(itoa(key, name, 16), oflag, MODE);
219 /* store shared memory information */
220 if (info.shmid != -1)
225 if (shm_putinfo(&info) == -1)
229 /* The size may only be set once. Ignore errors. */
230 ltrunc(info.shmid, size, SEEK_SET);