PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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)
 
#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)
 
void * shm_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)
 
void * shm_toc_lookup (shm_toc *toc, uint64 key)
 
Size shm_toc_estimate (shm_toc_estimator *)
 

Macro Definition Documentation

#define shm_toc_estimate_chunk (   e,
  sz 
)
Value:
((e)->space_for_chunks = add_size((e)->space_for_chunks, \
BUFFERALIGN((sz))))
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
e
Definition: preproc-init.c:82
#define BUFFERALIGN(LEN)
Definition: c.h:590

Definition at line 49 of file shm_toc.h.

Referenced by ExecBitmapHeapEstimate(), ExecCustomScanEstimate(), ExecForeignScanEstimate(), ExecIndexOnlyScanEstimate(), ExecIndexScanEstimate(), ExecInitParallelPlan(), ExecSeqScanEstimate(), InitializeParallelDSM(), and setup_dynamic_shared_memory().

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

Definition at line 47 of file shm_toc.h.

Referenced by CreateParallelContext(), and setup_dynamic_shared_memory().

Typedef Documentation

Definition at line 28 of file shm_toc.h.

Function Documentation

void* shm_toc_allocate ( shm_toc toc,
Size  nbytes 
)

Definition at line 83 of file shm_toc.c.

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

Referenced by ExecBitmapHeapInitializeDSM(), ExecCustomScanInitializeDSM(), ExecForeignScanInitializeDSM(), ExecIndexOnlyScanInitializeDSM(), ExecIndexScanInitializeDSM(), ExecInitParallelPlan(), ExecParallelSetupTupleQueues(), ExecSeqScanInitializeDSM(), InitializeParallelDSM(), and setup_dynamic_shared_memory().

84 {
85  volatile shm_toc *vtoc = toc;
86  Size total_bytes;
87  Size allocated_bytes;
88  Size nentry;
89  Size toc_bytes;
90 
91  /* Make sure request is well-aligned. */
92  nbytes = BUFFERALIGN(nbytes);
93 
95 
96  total_bytes = vtoc->toc_total_bytes;
97  allocated_bytes = vtoc->toc_allocated_bytes;
98  nentry = vtoc->toc_nentry;
99  toc_bytes = offsetof(shm_toc, toc_entry) +nentry * sizeof(shm_toc_entry)
100  + allocated_bytes;
101 
102  /* Check for memory exhaustion and overflow. */
103  if (toc_bytes + nbytes > total_bytes || toc_bytes + nbytes < toc_bytes)
104  {
105  SpinLockRelease(&toc->toc_mutex);
106  ereport(ERROR,
107  (errcode(ERRCODE_OUT_OF_MEMORY),
108  errmsg("out of shared memory")));
109  }
110  vtoc->toc_allocated_bytes += nbytes;
111 
112  SpinLockRelease(&toc->toc_mutex);
113 
114  return ((char *) toc) + (total_bytes - allocated_bytes - nbytes);
115 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
Size toc_total_bytes
Definition: shm_toc.c:30
slock_t toc_mutex
Definition: shm_toc.c:29
#define SpinLockRelease(lock)
Definition: spin.h:64
struct shm_toc_entry shm_toc_entry
Size toc_nentry
Definition: shm_toc.c:32
size_t Size
Definition: c.h:356
Size toc_allocated_bytes
Definition: shm_toc.c:31
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define BUFFERALIGN(LEN)
Definition: c.h:590
#define offsetof(type, field)
Definition: c.h:555
shm_toc* shm_toc_attach ( uint64  magic,
void *  address 
)

Definition at line 59 of file shm_toc.c.

References Assert, NULL, offsetof, shm_toc::toc_allocated_bytes, shm_toc::toc_magic, and shm_toc::toc_total_bytes.

Referenced by ParallelWorkerMain(), and test_shm_mq_main().

60 {
61  shm_toc *toc = (shm_toc *) address;
62 
63  if (toc->toc_magic != magic)
64  return NULL;
65 
67  Assert(toc->toc_total_bytes >= offsetof(shm_toc, toc_entry));
68 
69  return toc;
70 }
uint64 toc_magic
Definition: shm_toc.c:28
Size toc_total_bytes
Definition: shm_toc.c:30
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
Size toc_allocated_bytes
Definition: shm_toc.c:31
#define offsetof(type, field)
Definition: c.h:555
shm_toc* shm_toc_create ( uint64  magic,
void *  address,
Size  nbytes 
)

Definition at line 40 of file shm_toc.c.

References Assert, offsetof, 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 InitializeParallelDSM(), and setup_dynamic_shared_memory().

41 {
42  shm_toc *toc = (shm_toc *) address;
43 
44  Assert(nbytes > offsetof(shm_toc, toc_entry));
45  toc->toc_magic = magic;
46  SpinLockInit(&toc->toc_mutex);
47  toc->toc_total_bytes = nbytes;
48  toc->toc_allocated_bytes = 0;
49  toc->toc_nentry = 0;
50 
51  return toc;
52 }
uint64 toc_magic
Definition: shm_toc.c:28
#define SpinLockInit(lock)
Definition: spin.h:60
Size toc_total_bytes
Definition: shm_toc.c:30
slock_t toc_mutex
Definition: shm_toc.c:29
Size toc_nentry
Definition: shm_toc.c:32
#define Assert(condition)
Definition: c.h:675
Size toc_allocated_bytes
Definition: shm_toc.c:31
#define offsetof(type, field)
Definition: c.h:555
Size shm_toc_estimate ( shm_toc_estimator )

Definition at line 241 of file shm_toc.c.

References add_size(), mul_size(), shm_toc_estimator::number_of_keys, offsetof, and shm_toc_estimator::space_for_chunks.

Referenced by InitializeParallelDSM(), and setup_dynamic_shared_memory().

242 {
243  return add_size(offsetof(shm_toc, toc_entry),
244  add_size(mul_size(e->number_of_keys, sizeof(shm_toc_entry)),
245  e->space_for_chunks));
246 }
Definition: shm_toc.c:20
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
e
Definition: preproc-init.c:82
#define offsetof(type, field)
Definition: c.h:555
Size shm_toc_freespace ( shm_toc toc)

Definition at line 121 of file shm_toc.c.

References Assert, BUFFERALIGN, offsetof, SpinLockAcquire, SpinLockRelease, shm_toc::toc_allocated_bytes, shm_toc::toc_mutex, shm_toc::toc_nentry, and shm_toc::toc_total_bytes.

122 {
123  volatile shm_toc *vtoc = toc;
124  Size total_bytes;
125  Size allocated_bytes;
126  Size nentry;
127  Size toc_bytes;
128 
129  SpinLockAcquire(&toc->toc_mutex);
130  total_bytes = vtoc->toc_total_bytes;
131  allocated_bytes = vtoc->toc_allocated_bytes;
132  nentry = vtoc->toc_nentry;
133  SpinLockRelease(&toc->toc_mutex);
134 
135  toc_bytes = offsetof(shm_toc, toc_entry) +nentry * sizeof(shm_toc_entry);
136  Assert(allocated_bytes + BUFFERALIGN(toc_bytes) <= total_bytes);
137  return total_bytes - (allocated_bytes + BUFFERALIGN(toc_bytes));
138 }
#define SpinLockAcquire(lock)
Definition: spin.h:62
Size toc_total_bytes
Definition: shm_toc.c:30
slock_t toc_mutex
Definition: shm_toc.c:29
#define SpinLockRelease(lock)
Definition: spin.h:64
struct shm_toc_entry shm_toc_entry
Size toc_nentry
Definition: shm_toc.c:32
#define Assert(condition)
Definition: c.h:675
size_t Size
Definition: c.h:356
Size toc_allocated_bytes
Definition: shm_toc.c:31
#define BUFFERALIGN(LEN)
Definition: c.h:590
#define offsetof(type, field)
Definition: c.h:555
void shm_toc_insert ( shm_toc toc,
uint64  key,
void *  address 
)

Definition at line 161 of file shm_toc.c.

References Assert, ereport, errcode(), errmsg(), ERROR, shm_toc_entry::key, shm_toc_entry::offset, offsetof, 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 ExecBitmapHeapInitializeDSM(), ExecCustomScanInitializeDSM(), ExecForeignScanInitializeDSM(), ExecIndexOnlyScanInitializeDSM(), ExecIndexScanInitializeDSM(), ExecInitParallelPlan(), ExecParallelSetupTupleQueues(), ExecSeqScanInitializeDSM(), InitializeParallelDSM(), and setup_dynamic_shared_memory().

162 {
163  volatile shm_toc *vtoc = toc;
164  uint64 total_bytes;
165  uint64 allocated_bytes;
166  uint64 nentry;
167  uint64 toc_bytes;
168  uint64 offset;
169 
170  /* Relativize pointer. */
171  Assert(address > (void *) toc);
172  offset = ((char *) address) - (char *) toc;
173 
174  SpinLockAcquire(&toc->toc_mutex);
175 
176  total_bytes = vtoc->toc_total_bytes;
177  allocated_bytes = vtoc->toc_allocated_bytes;
178  nentry = vtoc->toc_nentry;
179  toc_bytes = offsetof(shm_toc, toc_entry) +nentry * sizeof(shm_toc_entry)
180  + allocated_bytes;
181 
182  /* Check for memory exhaustion and overflow. */
183  if (toc_bytes + sizeof(shm_toc_entry) > total_bytes ||
184  toc_bytes + sizeof(shm_toc_entry) < toc_bytes)
185  {
186  SpinLockRelease(&toc->toc_mutex);
187  ereport(ERROR,
188  (errcode(ERRCODE_OUT_OF_MEMORY),
189  errmsg("out of shared memory")));
190  }
191 
192  Assert(offset < total_bytes);
193  vtoc->toc_entry[nentry].key = key;
194  vtoc->toc_entry[nentry].offset = offset;
195 
196  /*
197  * By placing a write barrier after filling in the entry and before
198  * updating the number of entries, we make it safe to read the TOC
199  * unlocked.
200  */
202 
203  vtoc->toc_nentry++;
204 
205  SpinLockRelease(&toc->toc_mutex);
206 }
shm_toc_entry toc_entry[FLEXIBLE_ARRAY_MEMBER]
Definition: shm_toc.c:33
int errcode(int sqlerrcode)
Definition: elog.c:575
Definition: shm_toc.c:20
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
Size toc_total_bytes
Definition: shm_toc.c:30
slock_t toc_mutex
Definition: shm_toc.c:29
#define SpinLockRelease(lock)
Definition: spin.h:64
struct shm_toc_entry shm_toc_entry
Size toc_nentry
Definition: shm_toc.c:32
#define Assert(condition)
Definition: c.h:675
uint64 offset
Definition: shm_toc.c:23
Size toc_allocated_bytes
Definition: shm_toc.c:31
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define pg_write_barrier()
Definition: atomics.h:162
uint64 key
Definition: shm_toc.c:22
#define offsetof(type, field)
Definition: c.h:555
void* shm_toc_lookup ( shm_toc toc,
uint64  key 
)

Definition at line 218 of file shm_toc.c.

References i, shm_toc_entry::key, NULL, shm_toc_entry::offset, pg_read_barrier, shm_toc::toc_entry, and shm_toc::toc_nentry.

Referenced by attach_to_queues(), ExecBitmapHeapInitializeWorker(), ExecCustomScanInitializeWorker(), ExecForeignScanInitializeWorker(), ExecIndexOnlyScanInitializeWorker(), ExecIndexScanInitializeWorker(), ExecParallelGetQueryDesc(), ExecParallelGetReceiver(), ExecParallelSetupTupleQueues(), ExecSeqScanInitializeWorker(), ParallelQueryMain(), ParallelWorkerMain(), ReinitializeParallelDSM(), test_shm_mq_main(), and WaitForParallelWorkersToFinish().

219 {
220  uint64 nentry;
221  uint64 i;
222 
223  /* Read the number of entries before we examine any entry. */
224  nentry = toc->toc_nentry;
225  pg_read_barrier();
226 
227  /* Now search for a matching entry. */
228  for (i = 0; i < nentry; ++i)
229  if (toc->toc_entry[i].key == key)
230  return ((char *) toc) + toc->toc_entry[i].offset;
231 
232  /* No matching entry was found. */
233  return NULL;
234 }
shm_toc_entry toc_entry[FLEXIBLE_ARRAY_MEMBER]
Definition: shm_toc.c:33
Size toc_nentry
Definition: shm_toc.c:32
#define NULL
Definition: c.h:229
#define pg_read_barrier()
Definition: atomics.h:161
uint64 offset
Definition: shm_toc.c:23
int i
uint64 key
Definition: shm_toc.c:22