1 /*-------------------------------------------------------------------------
2  *
3  * pg_shmem.h
4  *	  Platform-independent API for shared memory support.
5  *
6  * Every port is expected to support shared memory with approximately
7  * SysV-ish semantics; in particular, a memory block is not anonymous
8  * but has an ID, and we must be able to tell whether there are any
9  * remaining processes attached to a block of a specified ID.
10  *
11  * To simplify life for the SysV implementation, the ID is assumed to
12  * consist of two unsigned long values (these are key and ID in SysV
13  * terms).  Other platforms may ignore the second value if they need
14  * only one ID number.
15  *
16  *
17  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
18  * Portions Copyright (c) 1994, Regents of the University of California
19  *
20  * src/include/storage/pg_shmem.h
21  *
22  *-------------------------------------------------------------------------
23  */
24 #ifndef PG_SHMEM_H
25 #define PG_SHMEM_H
26 
27 #include "storage/dsm_impl.h"
28 
29 typedef struct PGShmemHeader	/* standard header for all Postgres shmem */
30 {
31 	int32		magic;			/* magic # to identify Postgres segments */
32 #define PGShmemMagic  679834894
33 	pid_t		creatorPID;		/* PID of creating process (set but unread) */
34 	Size		totalsize;		/* total size of segment */
35 	Size		freeoffset;		/* offset to first free space */
36 	dsm_handle	dsm_control;	/* ID of dynamic shared memory control seg */
37 	void	   *index;			/* pointer to ShmemIndex table */
38 #ifndef WIN32					/* Windows doesn't have useful inode#s */
39 	dev_t		device;			/* device data directory is on */
40 	ino_t		inode;			/* inode number of data directory */
41 #endif
42 } PGShmemHeader;
43 
44 /* GUC variables */
45 extern int	shared_memory_type;
46 extern int	huge_pages;
47 
48 /* Possible values for huge_pages */
49 typedef enum
50 {
51 	HUGE_PAGES_OFF,
52 	HUGE_PAGES_ON,
53 	HUGE_PAGES_TRY
54 }			HugePagesType;
55 
56 /* Possible values for shared_memory_type */
57 typedef enum
58 {
59 	SHMEM_TYPE_WINDOWS,
60 	SHMEM_TYPE_SYSV,
61 	SHMEM_TYPE_MMAP
62 }			PGShmemType;
63 
64 #ifndef WIN32
65 extern unsigned long UsedShmemSegID;
66 #else
67 extern HANDLE UsedShmemSegID;
68 extern void *ShmemProtectiveRegion;
69 #endif
70 extern void *UsedShmemSegAddr;
71 
72 #if !defined(WIN32) && !defined(EXEC_BACKEND)
73 #define DEFAULT_SHARED_MEMORY_TYPE SHMEM_TYPE_MMAP
74 #elif !defined(WIN32)
75 #define DEFAULT_SHARED_MEMORY_TYPE SHMEM_TYPE_SYSV
76 #else
77 #define DEFAULT_SHARED_MEMORY_TYPE SHMEM_TYPE_WINDOWS
78 #endif
79 
80 #ifdef EXEC_BACKEND
81 extern void PGSharedMemoryReAttach(void);
82 extern void PGSharedMemoryNoReAttach(void);
83 #endif
84 
85 extern PGShmemHeader *PGSharedMemoryCreate(Size size,
86 										   PGShmemHeader **shim);
87 extern bool PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2);
88 extern void PGSharedMemoryDetach(void);
89 
90 #endif							/* PG_SHMEM_H */
91