PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
shm_toc.c File Reference
#include "postgres.h"
#include "port/atomics.h"
#include "storage/shm_toc.h"
#include "storage/spin.h"
Include dependency graph for shm_toc.c:

Go to the source code of this file.

Data Structures

struct  shm_toc_entry
 
struct  shm_toc
 

Typedefs

typedef struct shm_toc_entry shm_toc_entry
 

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, bool noError)
 
Size shm_toc_estimate (shm_toc_estimator *e)
 

Typedef Documentation

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
uint32 toc_nentry
Definition: shm_toc.c:32
#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_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
uint32 toc_nentry
Definition: shm_toc.c:32
Size toc_total_bytes
Definition: shm_toc.c:30
slock_t toc_mutex
Definition: shm_toc.c:29
#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 e)

Definition at line 253 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().

254 {
255  return add_size(offsetof(shm_toc, toc_entry),
257  e->space_for_chunks));
258 }
Size number_of_keys
Definition: shm_toc.h:46
Definition: shm_toc.c:20
Size space_for_chunks
Definition: shm_toc.h:45
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
#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
uint32 toc_nentry
Definition: shm_toc.c:32
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
#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_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 ExecBitmapHeapInitializeDSM(), ExecCustomScanInitializeDSM(), ExecForeignScanInitializeDSM(), ExecIndexOnlyScanInitializeDSM(), ExecIndexScanInitializeDSM(), ExecInitParallelPlan(), ExecParallelSetupTupleQueues(), ExecSeqScanInitializeDSM(), InitializeParallelDSM(), and setup_dynamic_shared_memory().

162 {
163  volatile shm_toc *vtoc = toc;
164  Size total_bytes;
165  Size allocated_bytes;
166  Size nentry;
167  Size toc_bytes;
168  Size 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  nentry >= PG_UINT32_MAX)
186  {
187  SpinLockRelease(&toc->toc_mutex);
188  ereport(ERROR,
189  (errcode(ERRCODE_OUT_OF_MEMORY),
190  errmsg("out of shared memory")));
191  }
192 
193  Assert(offset < total_bytes);
194  vtoc->toc_entry[nentry].key = key;
195  vtoc->toc_entry[nentry].offset = offset;
196 
197  /*
198  * By placing a write barrier after filling in the entry and before
199  * updating the number of entries, we make it safe to read the TOC
200  * unlocked.
201  */
203 
204  vtoc->toc_nentry++;
205 
206  SpinLockRelease(&toc->toc_mutex);
207 }
shm_toc_entry toc_entry[FLEXIBLE_ARRAY_MEMBER]
Definition: shm_toc.c:33
int errcode(int sqlerrcode)
Definition: elog.c:575
Size offset
Definition: shm_toc.c:23
#define PG_UINT32_MAX
Definition: c.h:341
Definition: shm_toc.c:20
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define ERROR
Definition: elog.h:43
uint32 toc_nentry
Definition: shm_toc.c:32
#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
#define Assert(condition)
Definition: c.h:675
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 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,
bool  noError 
)

Definition at line 222 of file shm_toc.c.

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

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

223 {
224  uint32 nentry;
225  uint32 i;
226 
227  /*
228  * Read the number of entries before we examine any entry. We assume that
229  * reading a uint32 is atomic.
230  */
231  nentry = toc->toc_nentry;
232  pg_read_barrier();
233 
234  /* Now search for a matching entry. */
235  for (i = 0; i < nentry; ++i)
236  {
237  if (toc->toc_entry[i].key == key)
238  return ((char *) toc) + toc->toc_entry[i].offset;
239  }
240 
241  /* No matching entry was found. */
242  if (!noError)
243  elog(ERROR, "could not find key " UINT64_FORMAT " in shm TOC at %p",
244  key, toc);
245  return NULL;
246 }
shm_toc_entry toc_entry[FLEXIBLE_ARRAY_MEMBER]
Definition: shm_toc.c:33
Size offset
Definition: shm_toc.c:23
#define ERROR
Definition: elog.h:43
uint32 toc_nentry
Definition: shm_toc.c:32
unsigned int uint32
Definition: c.h:268
#define NULL
Definition: c.h:229
#define pg_read_barrier()
Definition: atomics.h:161
int i
uint64 key
Definition: shm_toc.c:22
#define elog
Definition: elog.h:219
#define UINT64_FORMAT
Definition: c.h:316