OSDN Git Service

pgindent run. Make it all clean.
[pg-rex/syncrep.git] / src / backend / port / qnx4 / shm.c
1 /*-------------------------------------------------------------------------
2  *
3  * shm.c
4  *        System V Shared Memory Emulation
5  *
6  * Copyright (c) 1999, repas AEG Automation GmbH
7  *
8  *
9  * IDENTIFICATION
10  *        $Header: /cvsroot/pgsql/src/backend/port/qnx4/Attic/shm.c,v 1.4 2001/03/22 03:59:43 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <sys/mman.h>
20 #include <sys/shm.h>
21
22
23 #define MODE    0777
24
25 #define SHMMAX  1024
26
27
28 struct shm_info
29 {
30         int                     shmid;
31         key_t           key;
32         size_t          size;
33         void       *addr;
34 };
35
36 static struct shm_info *ShmInfo;
37
38
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);
43
44
45 static int
46 shm_putinfo(struct shm_info * info)
47 {
48         int                     i;
49
50         if (ShmInfo == NULL)
51         {
52                 ShmInfo = calloc(SHMMAX, sizeof(struct shm_info));
53                 if (ShmInfo == NULL)
54                         return -1;
55                 /* initialize ShmInfo */
56                 for (i = 0; i < SHMMAX; i++)
57                         ShmInfo[i].shmid = -1;
58         }
59
60         /* search first free element */
61         i = 0;
62         while (i < SHMMAX && ShmInfo[i].shmid != -1)
63                 i++;
64         if (i >= SHMMAX)
65         {
66                 errno = ENOSPC;
67                 return -1;
68         }
69
70         memcpy(&ShmInfo[i], info, sizeof(struct shm_info));
71
72         return i;
73 }
74
75 static int
76 shm_updinfo(int i, struct shm_info * info)
77 {
78         if (i >= SHMMAX)
79                 return -1;
80         if (ShmInfo == NULL)
81                 return -1;
82
83         memcpy(&ShmInfo[i], info, sizeof(struct shm_info));
84
85         return i;
86 }
87
88 static int
89 shm_getinfo(int shmid, struct shm_info * info)
90 {
91         int                     i;
92
93         if (ShmInfo == NULL)
94                 return -1;
95
96         /* search element */
97         i = 0;
98         while (i < SHMMAX && ShmInfo[i].shmid != shmid)
99                 i++;
100         if (i >= SHMMAX)
101                 return -1;
102
103         memcpy(info, &ShmInfo[i], sizeof(struct shm_info));
104
105         return i;
106 }
107
108 static int
109 shm_getinfobyaddr(const void *addr, struct shm_info * info)
110 {
111         int                     i;
112
113         if (ShmInfo == (struct shm_info *) - 1)
114                 return -1;
115
116         /* search element */
117         i = 0;
118         while (i < SHMMAX && ShmInfo[i].addr != addr)
119                 i++;
120         if (i >= SHMMAX)
121                 return -1;
122
123         memcpy(info, &ShmInfo[i], sizeof(struct shm_info));
124
125         return i;
126 }
127
128
129 void *
130 shmat(int shmid, const void *shmaddr, int shmflg)
131 {
132         struct shm_info info;
133         int                     i;
134
135         i = shm_getinfo(shmid, &info);
136         if (i == -1)
137         {
138                 errno = EACCES;
139                 return (void *) -1;
140         }
141
142         info.addr = mmap((void *) shmaddr, info.size,
143                                          PROT_READ | PROT_WRITE, MAP_SHARED, shmid, 0);
144         if (info.addr == MAP_FAILED)
145                 return info.addr;
146
147         if (shm_updinfo(i, &info) == -1)
148         {
149                 errno = EACCES;
150                 return (void *) -1;
151         }
152
153         return info.addr;
154 }
155
156 int
157 shmdt(const void *addr)
158 {
159         struct shm_info info;
160
161         if (shm_getinfobyaddr(addr, &info) == -1)
162         {
163                 errno = EACCES;
164                 return -1;
165         }
166
167         return munmap((void *) addr, info.size);
168 }
169
170 int
171 shmctl(int shmid, int cmd, struct shmid_ds * buf)
172 {
173         struct shm_info info;
174         char            name[NAME_MAX + 1];
175
176         if (cmd == IPC_RMID)
177         {
178                 if (shm_getinfo(shmid, &info) == -1)
179                 {
180                         errno = EACCES;
181                         return -1;
182                 }
183                 return shm_unlink(itoa(info.key, name, 16));
184         }
185         if (cmd == IPC_STAT)
186         {
187
188                 /*
189                  * Can we support IPC_STAT?  We only need shm_nattch ... For now,
190                  * punt and assume the shm seg does not exist.
191                  */
192                 errno = EINVAL;
193                 return -1;
194         }
195         errno = EINVAL;
196         return -1;
197 }
198
199 int
200 shmget(key_t key, size_t size, int flags)
201 {
202         char            name[NAME_MAX + 1];
203         int                     oflag = 0;
204         struct shm_info info;
205
206         if (flags & IPC_CREAT)
207                 oflag |= O_CREAT;
208         if (flags & IPC_EXCL)
209                 oflag |= O_EXCL;
210         if (flags & SHM_R)
211         {
212                 if (flags & SHM_W)
213                         oflag |= O_RDWR;
214                 else
215                         oflag |= O_RDONLY;
216         }
217         info.shmid = shm_open(itoa(key, name, 16), oflag, MODE);
218
219         /* store shared memory information */
220         if (info.shmid != -1)
221         {
222                 info.key = key;
223                 info.size = size;
224                 info.addr = NULL;
225                 if (shm_putinfo(&info) == -1)
226                         return -1;
227         }
228
229         /* The size may only be set once. Ignore errors. */
230         ltrunc(info.shmid, size, SEEK_SET);
231
232         return info.shmid;
233 }