10
10
* Portions Copyright (c) 1994, Regents of the University of California
11
11
*
12
12
* IDENTIFICATION
13
- * $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.10 2003/05/08 19:17:07 momjian Exp $
13
+ * $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.11 2003/07/14 20:00:22 tgl Exp $
14
14
*
15
15
*-------------------------------------------------------------------------
16
16
*/
@@ -45,10 +45,8 @@ void *UsedShmemSegAddr = NULL;
45
45
static void * InternalIpcMemoryCreate (IpcMemoryKey memKey , uint32 size );
46
46
static void IpcMemoryDetach (int status , Datum shmaddr );
47
47
static void IpcMemoryDelete (int status , Datum shmId );
48
- static void * PrivateMemoryCreate (uint32 size );
49
- static void PrivateMemoryDelete (int status , Datum memaddr );
50
48
static PGShmemHeader * PGSharedMemoryAttach (IpcMemoryKey key ,
51
- IpcMemoryId * shmid , void * addr );
49
+ IpcMemoryId * shmid );
52
50
53
51
54
52
/*
@@ -243,41 +241,6 @@ PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2)
243
241
}
244
242
245
243
246
- /* ----------------------------------------------------------------
247
- * private memory support
248
- *
249
- * Rather than allocating shmem segments with IPC_PRIVATE key, we
250
- * just malloc() the requested amount of space. This code emulates
251
- * the needed shmem functions.
252
- * ----------------------------------------------------------------
253
- */
254
-
255
- static void *
256
- PrivateMemoryCreate (uint32 size )
257
- {
258
- void * memAddress ;
259
-
260
- memAddress = malloc (size );
261
- if (!memAddress )
262
- {
263
- fprintf (stderr , "PrivateMemoryCreate: malloc(%u) failed\n" , size );
264
- proc_exit (1 );
265
- }
266
- MemSet (memAddress , 0 , size ); /* keep Purify quiet */
267
-
268
- /* Register on-exit routine to release storage */
269
- on_shmem_exit (PrivateMemoryDelete , PointerGetDatum (memAddress ));
270
-
271
- return memAddress ;
272
- }
273
-
274
- static void
275
- PrivateMemoryDelete (int status , Datum memaddr )
276
- {
277
- free (DatumGetPointer (memaddr ));
278
- }
279
-
280
-
281
244
/*
282
245
* PGSharedMemoryCreate
283
246
*
@@ -289,6 +252,9 @@ PrivateMemoryDelete(int status, Datum memaddr)
289
252
* collision with non-Postgres shmem segments. The idea here is to detect and
290
253
* re-use keys that may have been assigned by a crashed postmaster or backend.
291
254
*
255
+ * makePrivate means to always create a new segment, rather than attach to
256
+ * or recycle any existing segment.
257
+ *
292
258
* The port number is passed for possible use as a key (for SysV, we use
293
259
* it to generate the starting shmem key). In a standalone backend,
294
260
* zero will be passed.
@@ -307,8 +273,7 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
307
273
/* Just attach and return the pointer */
308
274
if (ExecBackend && UsedShmemSegAddr != NULL && !makePrivate )
309
275
{
310
- if ((hdr = (PGShmemHeader * ) memAddress = PGSharedMemoryAttach (
311
- UsedShmemSegID , & shmid , UsedShmemSegAddr )) == NULL )
276
+ if ((hdr = PGSharedMemoryAttach (UsedShmemSegID , & shmid )) == NULL )
312
277
{
313
278
fprintf (stderr , "Unable to attach to proper memory at fixed address: shmget(key=%d, addr=%p) failed: %s\n" ,
314
279
(int ) UsedShmemSegID , UsedShmemSegAddr , strerror (errno ));
@@ -317,34 +282,29 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
317
282
return hdr ;
318
283
}
319
284
320
- /* Create shared memory */
321
-
322
- NextShmemSegID = port * 1000 + 1 ;
285
+ /* Loop till we find a free IPC key */
286
+ NextShmemSegID = port * 1000 ;
323
287
324
- for (;; NextShmemSegID ++ )
288
+ for (NextShmemSegID ++ ;; NextShmemSegID ++ )
325
289
{
326
- /* Special case if creating a private segment --- just malloc() it */
327
- if (makePrivate )
328
- {
329
- memAddress = PrivateMemoryCreate (size );
330
- break ;
331
- }
332
-
333
290
/* Try to create new segment */
334
291
memAddress = InternalIpcMemoryCreate (NextShmemSegID , size );
335
292
if (memAddress )
336
293
break ; /* successful create and attach */
337
294
338
295
/* Check shared memory and possibly remove and recreate */
339
-
340
- if ((hdr = (PGShmemHeader * ) memAddress = PGSharedMemoryAttach (
341
- NextShmemSegID , & shmid , UsedShmemSegAddr )) == NULL )
296
+
297
+ if (makePrivate ) /* a standalone backend shouldn't do this */
298
+ continue ;
299
+
300
+ if ((memAddress = PGSharedMemoryAttach (NextShmemSegID , & shmid )) == NULL )
342
301
continue ; /* can't attach, not one of mine */
343
302
344
303
/*
345
304
* If I am not the creator and it belongs to an extant process,
346
305
* continue.
347
306
*/
307
+ hdr = (PGShmemHeader * ) memAddress ;
348
308
if (hdr -> creatorPID != getpid ())
349
309
{
350
310
if (kill (hdr -> creatorPID , 0 ) == 0 || errno != ESRCH )
@@ -407,22 +367,25 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
407
367
408
368
409
369
/*
410
- * Attach to shared memory and make sure it has a Postgres header
370
+ * Attach to shared memory and make sure it has a Postgres header
371
+ *
372
+ * Returns attach address if OK, else NULL
411
373
*/
412
374
static PGShmemHeader *
413
- PGSharedMemoryAttach (IpcMemoryKey key , IpcMemoryId * shmid , void * addr )
375
+ PGSharedMemoryAttach (IpcMemoryKey key , IpcMemoryId * shmid )
414
376
{
415
377
PGShmemHeader * hdr ;
416
378
417
379
if ((* shmid = shmget (key , sizeof (PGShmemHeader ), 0 )) < 0 )
418
380
return NULL ;
419
381
420
- hdr = (PGShmemHeader * ) shmat (* shmid , UsedShmemSegAddr ,
382
+ hdr = (PGShmemHeader * ) shmat (* shmid ,
383
+ UsedShmemSegAddr ,
421
384
#if defined(solaris ) && defined (__sparc__ )
422
- /* use intimate shared memory on SPARC Solaris */
423
- SHM_SHARE_MMU
385
+ /* use intimate shared memory on Solaris */
386
+ SHM_SHARE_MMU
424
387
#else
425
- 0
388
+ 0
426
389
#endif
427
390
);
428
391
@@ -434,5 +397,6 @@ PGSharedMemoryAttach(IpcMemoryKey key, IpcMemoryId *shmid, void *addr)
434
397
shmdt (hdr );
435
398
return NULL ; /* segment belongs to a non-Postgres app */
436
399
}
400
+
437
401
return hdr ;
438
402
}
0 commit comments