PostgreSQL Source Code git master
Loading...
Searching...
No Matches
shmem.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * shmem.h
4 * shared memory management structures
5 *
6 * This file contains public functions for other core subsystems and
7 * extensions to allocate shared memory. Internal functions for the shmem
8 * allocator itself and hooking it to the rest of the system are in
9 * shmem_internal.h
10 *
11 * Historical note:
12 * A long time ago, Postgres' shared memory region was allowed to be mapped
13 * at a different address in each process, and shared memory "pointers" were
14 * passed around as offsets relative to the start of the shared memory region.
15 * That is no longer the case: each process must map the shared memory region
16 * at the same address. This means shared memory pointers can be passed
17 * around directly between different processes.
18 *
19 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
20 * Portions Copyright (c) 1994, Regents of the University of California
21 *
22 * src/include/storage/shmem.h
23 *
24 *-------------------------------------------------------------------------
25 */
26#ifndef SHMEM_H
27#define SHMEM_H
28
29#include "utils/hsearch.h"
30
31/*
32 * Options for ShmemRequestStruct()
33 *
34 * 'name' and 'size' are required. Initialize any optional fields that you
35 * don't use to zeros.
36 *
37 * After registration, the shmem machinery reserves memory for the area, sets
38 * '*ptr' to point to the allocation, and calls the callbacks at the right
39 * moments.
40 */
41typedef struct ShmemStructOpts
42{
43 const char *name;
44
45 /*
46 * Requested size of the shmem allocation.
47 *
48 * When attaching to an existing allocation, the size must match the size
49 * given when the shmem region was allocated. This cross-check can be
50 * disabled specifying SHMEM_ATTACH_UNKNOWN_SIZE.
51 */
53
54 /*
55 * Alignment of the starting address. If not set, defaults to cacheline
56 * boundary. Must be a power of two.
57 */
58 size_t alignment;
59
60 /*
61 * When the shmem area is initialized or attached to, pointer to it is
62 * stored in *ptr. It usually points to a global variable, used to access
63 * the shared memory area later. *ptr is set before the init_fn or
64 * attach_fn callback is called.
65 */
66 void **ptr;
68
69#define SHMEM_ATTACH_UNKNOWN_SIZE (-1)
70
71/*
72 * Options for ShmemRequestHash()
73 *
74 * Each hash table is backed by a contiguous shmem area.
75 */
76typedef struct ShmemHashOpts
77{
78 /* Options for allocating the underlying shmem area; do not touch directly */
80
81 /*
82 * Name of the shared memory area. Required. Must be unique across the
83 * system.
84 */
85 const char *name;
86
87 /*
88 * 'nelems' is the max number of elements for the hash table.
89 */
91
92 /*
93 * Hash table options passed to hash_create()
94 *
95 * hash_info and hash_flags must specify at least the entry sizes and key
96 * comparison semantics (see hash_create()). Flag bits and values
97 * specific to shared-memory hash tables are added implicitly in
98 * ShmemRequestHash(), except that callers may choose to specify
99 * HASH_PARTITION and/or HASH_FIXED_SIZE.
100 */
103
104 /*
105 * When the hash table is initialized or attached to, pointer to its
106 * backend-private handle is stored in *ptr. It usually points to a
107 * global variable, used to access the hash table later.
108 */
111
112typedef void (*ShmemRequestCallback) (void *opaque_arg);
113typedef void (*ShmemInitCallback) (void *opaque_arg);
114typedef void (*ShmemAttachCallback) (void *opaque_arg);
115
116/*
117 * Shared memory is reserved and allocated in stages at postmaster startup,
118 * and in EXEC_BACKEND mode, there's some extra work done to "attach" to them
119 * at backend startup. ShmemCallbacks holds callback functions that are
120 * called at different stages.
121 */
122typedef struct ShmemCallbacks
123{
124 /* SHMEM_CALLBACKS_* flags */
125 int flags;
126
127 /*
128 * 'request_fn' is called during postmaster startup, before the shared
129 * memory has been allocated. The function should call
130 * ShmemRequestStruct() and ShmemRequestHash() to register the subsystem's
131 * shared memory needs.
132 */
134
135 /*
136 * Initialization callback function. This is called after the shared
137 * memory area has been allocated, usually at postmaster startup.
138 */
140
141 /*
142 * Attachment callback function. In EXEC_BACKEND mode, this is called at
143 * startup of each backend. In !EXEC_BACKEND mode, this is only called if
144 * the shared memory area is registered after postmaster startup (see
145 * SHMEM_CALLBACKS_ALLOW_AFTER_STARTUP).
146 */
148
149 /*
150 * Argument passed to the callbacks. This is opaque to the shmem system,
151 * callbacks can use it for their own purposes.
152 */
155
156/*
157 * Flags to control the behavior of RegisterShmemCallbacks().
158 *
159 * SHMEM_CALLBACKS_ALLOW_AFTER_STARTUP: Normally, calling
160 * RegisterShmemCallbacks() after postmaster startup, e.g. in an add-in
161 * library loaded on-demand in a backend, results in an error, because shared
162 * memory should generally be requested at postmaster startup time. But if
163 * this flag is set, it is allowed and the callbacks are called immediately to
164 * initialize or attach to the requested shared memory areas. This is not
165 * used by any built-in subsystems, but extensions may find it useful.
166 */
167#define SHMEM_CALLBACKS_ALLOW_AFTER_STARTUP 0x00000001
168
169extern void RegisterShmemCallbacks(const ShmemCallbacks *callbacks);
170extern bool ShmemAddrIsValid(const void *addr);
171
172/*
173 * These macros provide syntactic sugar for calling the underlying functions
174 * with named arguments -like syntax.
175 */
176#define ShmemRequestStruct(...) \
177 ShmemRequestStructWithOpts(&(ShmemStructOpts){__VA_ARGS__})
178
179#define ShmemRequestHash(...) \
180 ShmemRequestHashWithOpts(&(ShmemHashOpts){__VA_ARGS__})
181
184
185/* legacy shmem allocation functions */
186extern void *ShmemInitStruct(const char *name, Size size, bool *foundPtr);
187extern HTAB *ShmemInitHash(const char *name, int64 nelems,
188 HASHCTL *infoP, int hash_flags);
189extern void *ShmemAlloc(Size size);
190extern void *ShmemAllocNoError(Size size);
191
192extern Size add_size(Size s1, Size s2);
193extern Size mul_size(Size s1, Size s2);
194
196
197/* ipci.c */
198extern void RequestAddinShmemSpace(Size size);
199
200#endif /* SHMEM_H */
#define PGDLLIMPORT
Definition c.h:1421
int64_t int64
Definition c.h:621
size_t Size
Definition c.h:689
static int fb(int x)
char * s1
char * s2
bool ShmemAddrIsValid(const void *addr)
Definition shmem.c:850
void ShmemRequestStructWithOpts(const ShmemStructOpts *options)
Definition shmem.c:316
Size add_size(Size s1, Size s2)
Definition shmem.c:1048
void(* ShmemInitCallback)(void *opaque_arg)
Definition shmem.h:113
PGDLLIMPORT Size pg_get_shmem_pagesize(void)
Definition shmem.c:1304
void RegisterShmemCallbacks(const ShmemCallbacks *callbacks)
Definition shmem.c:874
void * ShmemAllocNoError(Size size)
Definition shmem.c:784
Size mul_size(Size s1, Size s2)
Definition shmem.c:1063
void * ShmemAlloc(Size size)
Definition shmem.c:764
void(* ShmemAttachCallback)(void *opaque_arg)
Definition shmem.h:114
void RequestAddinShmemSpace(Size size)
Definition ipci.c:45
void ShmemRequestHashWithOpts(const ShmemHashOpts *options)
Definition shmem_hash.c:44
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition shmem.c:1011
HTAB * ShmemInitHash(const char *name, int64 nelems, HASHCTL *infoP, int hash_flags)
Definition shmem_hash.c:117
void(* ShmemRequestCallback)(void *opaque_arg)
Definition shmem.h:112
ShmemRequestCallback request_fn
Definition shmem.h:133
ShmemInitCallback init_fn
Definition shmem.h:139
void * opaque_arg
Definition shmem.h:153
ShmemAttachCallback attach_fn
Definition shmem.h:147
int64 nelems
Definition shmem.h:90
ShmemStructOpts base
Definition shmem.h:79
HTAB ** ptr
Definition shmem.h:109
const char * name
Definition shmem.h:85
int hash_flags
Definition shmem.h:102
HASHCTL hash_info
Definition shmem.h:101
size_t alignment
Definition shmem.h:58
ssize_t size
Definition shmem.h:52
const char * name
Definition shmem.h:43
void ** ptr
Definition shmem.h:66
const char * name