PostgreSQL Source Code git master
Loading...
Searching...
No Matches
shm_toc.h File Reference
#include "storage/shmem.h"
Include dependency graph for shm_toc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  shm_toc_estimator
 

Macros

#define shm_toc_initialize_estimator(e)    ((e)->space_for_chunks = 0, (e)->number_of_keys = 0)
 
#define shm_toc_estimate_chunk(e, sz)    ((e)->space_for_chunks = add_size((e)->space_for_chunks, BUFFERALIGN(sz)))
 
#define shm_toc_estimate_keys(e, cnt)    ((e)->number_of_keys = add_size((e)->number_of_keys, cnt))
 

Typedefs

typedef struct shm_toc shm_toc
 

Functions

shm_tocshm_toc_create (uint64 magic, void *address, Size nbytes)
 
shm_tocshm_toc_attach (uint64 magic, void *address)
 
voidshm_toc_allocate (shm_toc *toc, Size nbytes)
 
Size shm_toc_freespace (shm_toc *toc)
 
void shm_toc_insert (shm_toc *toc, uint64 key, void *address)
 
voidshm_toc_lookup (shm_toc *toc, uint64 key, bool noError)
 
Size shm_toc_estimate (shm_toc_estimator *e)
 

Macro Definition Documentation

◆ shm_toc_estimate_chunk

#define shm_toc_estimate_chunk (   e,
  sz 
)     ((e)->space_for_chunks = add_size((e)->space_for_chunks, BUFFERALIGN(sz)))

Definition at line 51 of file shm_toc.h.

◆ shm_toc_estimate_keys

#define shm_toc_estimate_keys (   e,
  cnt 
)     ((e)->number_of_keys = add_size((e)->number_of_keys, cnt))

Definition at line 53 of file shm_toc.h.

◆ shm_toc_initialize_estimator

#define shm_toc_initialize_estimator (   e)     ((e)->space_for_chunks = 0, (e)->number_of_keys = 0)

Definition at line 49 of file shm_toc.h.

Typedef Documentation

◆ shm_toc

Definition at line 28 of file shm_toc.h.

Function Documentation

◆ shm_toc_allocate()

void * shm_toc_allocate ( shm_toc toc,
Size  nbytes 
)
extern

Definition at line 88 of file shm_toc.c.

89{
90 Size total_bytes;
92 Size nentry;
94
95 /*
96 * Make sure request is well-aligned. XXX: MAXALIGN is not enough,
97 * because atomic ops might need a wider alignment. We don't have a
98 * proper definition for the minimum to make atomic ops safe, but
99 * BUFFERALIGN ought to be enough.
100 */
101 nbytes = BUFFERALIGN(nbytes);
102
104
105 total_bytes = toc->toc_total_bytes;
107 nentry = toc->toc_nentry;
108 toc_bytes = offsetof(shm_toc, toc_entry) + nentry * sizeof(shm_toc_entry)
110
111 /* Check for memory exhaustion and overflow. */
112 if (toc_bytes + nbytes > total_bytes || toc_bytes + nbytes < toc_bytes)
113 {
117 errmsg("out of shared memory")));
118 }
119 toc->toc_allocated_bytes += nbytes;
120
122
123 return ((char *) toc) + (total_bytes - allocated_bytes - nbytes);
124}
#define BUFFERALIGN(LEN)
Definition c.h:957
size_t Size
Definition c.h:748
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
static char * errmsg
static int fb(int x)
static void SpinLockRelease(volatile slock_t *lock)
Definition spin.h:62
static void SpinLockAcquire(volatile slock_t *lock)
Definition spin.h:56
Definition shm_toc.c:21
Size toc_total_bytes
Definition shm_toc.c:30
uint32 toc_nentry
Definition shm_toc.c:32
slock_t toc_mutex
Definition shm_toc.c:29
Size toc_allocated_bytes
Definition shm_toc.c:31

References BUFFERALIGN, ereport, errcode(), errmsg, ERROR, fb(), SpinLockAcquire(), SpinLockRelease(), shm_toc::toc_allocated_bytes, shm_toc::toc_mutex, shm_toc::toc_nentry, and shm_toc::toc_total_bytes.

Referenced by _brin_begin_parallel(), _bt_begin_parallel(), _gin_begin_parallel(), ExecAggInitializeDSM(), ExecAppendInitializeDSM(), ExecBitmapHeapInitializeDSM(), ExecBitmapHeapInstrumentInitDSM(), ExecBitmapIndexScanInitializeDSM(), ExecCustomScanInitializeDSM(), ExecForeignScanInitializeDSM(), ExecHashInitializeDSM(), ExecHashJoinInitializeDSM(), ExecIncrementalSortInitializeDSM(), ExecIndexOnlyScanInitializeDSM(), ExecIndexOnlyScanInstrumentInitDSM(), ExecIndexScanInitializeDSM(), ExecIndexScanInstrumentInitDSM(), ExecInitParallelPlan(), ExecMemoizeInitializeDSM(), ExecParallelSetupTupleQueues(), ExecSeqScanInitializeDSM(), ExecSeqScanInstrumentInitDSM(), ExecSortInitializeDSM(), ExecTidRangeScanInitializeDSM(), ExecTidRangeScanInstrumentInitDSM(), GetSessionDsmHandle(), InitializeParallelDSM(), pa_setup_dsm(), parallel_vacuum_init(), and setup_dynamic_shared_memory().

◆ shm_toc_attach()

shm_toc * shm_toc_attach ( uint64  magic,
void address 
)
extern

Definition at line 64 of file shm_toc.c.

65{
66 shm_toc *toc = (shm_toc *) address;
67
68 if (toc->toc_magic != magic)
69 return NULL;
70
72 Assert(toc->toc_total_bytes > offsetof(shm_toc, toc_entry));
73
74 return toc;
75}
#define Assert(condition)
Definition c.h:1002
uint64 toc_magic
Definition shm_toc.c:28

References Assert, fb(), shm_toc::toc_allocated_bytes, shm_toc::toc_magic, and shm_toc::toc_total_bytes.

Referenced by AttachSession(), ParallelApplyWorkerMain(), ParallelWorkerMain(), and test_shm_mq_main().

◆ shm_toc_create()

shm_toc * shm_toc_create ( uint64  magic,
void address,
Size  nbytes 
)
extern

Definition at line 40 of file shm_toc.c.

41{
42 shm_toc *toc = (shm_toc *) address;
43
44 Assert(nbytes > offsetof(shm_toc, toc_entry));
45 toc->toc_magic = magic;
47
48 /*
49 * The alignment code in shm_toc_allocate() assumes that the starting
50 * value is buffer-aligned.
51 */
52 toc->toc_total_bytes = BUFFERALIGN_DOWN(nbytes);
53 toc->toc_allocated_bytes = 0;
54 toc->toc_nentry = 0;
55
56 return toc;
57}
#define BUFFERALIGN_DOWN(LEN)
Definition c.h:968
static void SpinLockInit(volatile slock_t *lock)
Definition spin.h:50

References Assert, BUFFERALIGN_DOWN, fb(), SpinLockInit(), shm_toc::toc_allocated_bytes, shm_toc::toc_magic, shm_toc::toc_mutex, shm_toc::toc_nentry, and shm_toc::toc_total_bytes.

Referenced by GetSessionDsmHandle(), InitializeParallelDSM(), pa_setup_dsm(), and setup_dynamic_shared_memory().

◆ shm_toc_estimate()

Size shm_toc_estimate ( shm_toc_estimator e)
extern

Definition at line 267 of file shm_toc.c.

268{
269 Size sz;
270
271 sz = offsetof(shm_toc, toc_entry);
272 sz = add_size(sz, mul_size(e->number_of_keys, sizeof(shm_toc_entry)));
273 sz = add_size(sz, e->space_for_chunks);
274
275 return BUFFERALIGN(sz);
276}
Size add_size(Size s1, Size s2)
Definition mcxt.c:1733
Size mul_size(Size s1, Size s2)
Definition mcxt.c:1752
e

References add_size(), BUFFERALIGN, fb(), and mul_size().

Referenced by GetSessionDsmHandle(), InitializeParallelDSM(), pa_setup_dsm(), and setup_dynamic_shared_memory().

◆ shm_toc_freespace()

Size shm_toc_freespace ( shm_toc toc)
extern

Definition at line 130 of file shm_toc.c.

131{
132 Size total_bytes;
134 Size nentry;
136
138 total_bytes = toc->toc_total_bytes;
140 nentry = toc->toc_nentry;
142
143 toc_bytes = offsetof(shm_toc, toc_entry) + nentry * sizeof(shm_toc_entry);
144 Assert(allocated_bytes + BUFFERALIGN(toc_bytes) <= total_bytes);
145 return total_bytes - (allocated_bytes + BUFFERALIGN(toc_bytes));
146}

References Assert, BUFFERALIGN, fb(), SpinLockAcquire(), SpinLockRelease(), shm_toc::toc_allocated_bytes, shm_toc::toc_mutex, shm_toc::toc_nentry, and shm_toc::toc_total_bytes.

◆ shm_toc_insert()

void shm_toc_insert ( shm_toc toc,
uint64  key,
void address 
)
extern

Definition at line 169 of file shm_toc.c.

170{
171 Size total_bytes;
173 Size nentry;
175 Size offset;
176
177 /* Relativize pointer. */
178 Assert(address > (void *) toc);
179 offset = ((char *) address) - (char *) toc;
180
182
183 total_bytes = toc->toc_total_bytes;
185 nentry = toc->toc_nentry;
186
187#ifdef USE_ASSERT_CHECKING
188 /* Verify no duplicate keys */
189 for (Size i = 0; i < nentry; i++)
190 Assert(toc->toc_entry[i].key != key);
191#endif
192
193 toc_bytes = offsetof(shm_toc, toc_entry) + nentry * sizeof(shm_toc_entry)
195
196 /* Check for memory exhaustion and overflow. */
197 if (toc_bytes + sizeof(shm_toc_entry) > total_bytes ||
198 toc_bytes + sizeof(shm_toc_entry) < toc_bytes ||
199 nentry >= PG_UINT32_MAX)
200 {
204 errmsg("out of shared memory")));
205 }
206
207 Assert(offset < total_bytes);
208 toc->toc_entry[nentry].key = key;
209 toc->toc_entry[nentry].offset = offset;
210
211 /*
212 * By placing a write barrier after filling in the entry and before
213 * updating the number of entries, we make it safe to read the TOC
214 * unlocked.
215 */
217
218 toc->toc_nentry++;
219
221}
#define pg_write_barrier()
Definition atomics.h:155
#define PG_UINT32_MAX
Definition c.h:733
int i
Definition isn.c:77
Size offset
Definition shm_toc.c:23
uint64 key
Definition shm_toc.c:22
shm_toc_entry toc_entry[FLEXIBLE_ARRAY_MEMBER]
Definition shm_toc.c:33

References Assert, ereport, errcode(), errmsg, ERROR, fb(), i, shm_toc_entry::key, shm_toc_entry::offset, PG_UINT32_MAX, pg_write_barrier, SpinLockAcquire(), SpinLockRelease(), shm_toc::toc_allocated_bytes, shm_toc::toc_entry, shm_toc::toc_mutex, shm_toc::toc_nentry, and shm_toc::toc_total_bytes.

Referenced by _brin_begin_parallel(), _bt_begin_parallel(), _gin_begin_parallel(), ExecAggInitializeDSM(), ExecAppendInitializeDSM(), ExecBitmapHeapInitializeDSM(), ExecBitmapHeapInstrumentInitDSM(), ExecBitmapIndexScanInitializeDSM(), ExecCustomScanInitializeDSM(), ExecForeignScanInitializeDSM(), ExecHashInitializeDSM(), ExecHashJoinInitializeDSM(), ExecIncrementalSortInitializeDSM(), ExecIndexOnlyScanInitializeDSM(), ExecIndexOnlyScanInstrumentInitDSM(), ExecIndexScanInitializeDSM(), ExecIndexScanInstrumentInitDSM(), ExecInitParallelPlan(), ExecMemoizeInitializeDSM(), ExecParallelSetupTupleQueues(), ExecSeqScanInitializeDSM(), ExecSeqScanInstrumentInitDSM(), ExecSortInitializeDSM(), ExecTidRangeScanInitializeDSM(), ExecTidRangeScanInstrumentInitDSM(), GetSessionDsmHandle(), InitializeParallelDSM(), pa_setup_dsm(), parallel_vacuum_init(), and setup_dynamic_shared_memory().

◆ shm_toc_lookup()

void * shm_toc_lookup ( shm_toc toc,
uint64  key,
bool  noError 
)
extern

Definition at line 236 of file shm_toc.c.

237{
238 uint32 nentry;
239 uint32 i;
240
241 /*
242 * Read the number of entries before we examine any entry. We assume that
243 * reading a uint32 is atomic.
244 */
245 nentry = toc->toc_nentry;
247
248 /* Now search for a matching entry. */
249 for (i = 0; i < nentry; ++i)
250 {
251 if (toc->toc_entry[i].key == key)
252 return ((char *) toc) + toc->toc_entry[i].offset;
253 }
254
255 /* No matching entry was found. */
256 if (!noError)
257 elog(ERROR, "could not find key " UINT64_FORMAT " in shm TOC at %p",
258 key, toc);
259 return NULL;
260}
#define pg_read_barrier()
Definition atomics.h:154
#define UINT64_FORMAT
Definition c.h:694
uint32_t uint32
Definition c.h:683
#define elog(elevel,...)
Definition elog.h:228

References elog, ERROR, fb(), i, shm_toc_entry::key, pg_read_barrier, shm_toc::toc_entry, shm_toc::toc_nentry, and UINT64_FORMAT.

Referenced by _brin_parallel_build_main(), _bt_parallel_build_main(), _gin_parallel_build_main(), attach_to_queues(), AttachSession(), ExecAggInitializeWorker(), ExecAppendInitializeWorker(), ExecBitmapHeapInitializeWorker(), ExecBitmapHeapInstrumentInitWorker(), ExecBitmapIndexScanInitializeWorker(), ExecCustomScanInitializeWorker(), ExecCustomScanReInitializeDSM(), ExecForeignScanInitializeWorker(), ExecForeignScanReInitializeDSM(), ExecHashInitializeWorker(), ExecHashJoinInitializeWorker(), ExecHashJoinReInitializeDSM(), ExecIncrementalSortInitializeWorker(), ExecIndexOnlyScanInitializeWorker(), ExecIndexOnlyScanInstrumentInitWorker(), ExecIndexScanInitializeWorker(), ExecIndexScanInstrumentInitWorker(), ExecMemoizeInitializeWorker(), ExecParallelGetQueryDesc(), ExecParallelGetReceiver(), ExecParallelReinitialize(), ExecParallelSetupTupleQueues(), ExecSeqScanInitializeWorker(), ExecSeqScanInstrumentInitWorker(), ExecSortInitializeWorker(), ExecTidRangeScanInitializeWorker(), ExecTidRangeScanInstrumentInitWorker(), parallel_vacuum_main(), ParallelApplyWorkerMain(), ParallelQueryMain(), ParallelWorkerMain(), ReinitializeParallelDSM(), test_shm_mq_main(), and WaitForParallelWorkersToFinish().