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

Go to the source code of this file.

Data Structures

struct  ShmemStructOpts
 
struct  ShmemHashOpts
 
struct  ShmemCallbacks
 

Macros

#define SHMEM_ATTACH_UNKNOWN_SIZE   (-1)
 
#define SHMEM_CALLBACKS_ALLOW_AFTER_STARTUP   0x00000001
 
#define ShmemRequestStruct(...)    ShmemRequestStructWithOpts(&(ShmemStructOpts){__VA_ARGS__})
 
#define ShmemRequestHash(...)    ShmemRequestHashWithOpts(&(ShmemHashOpts){__VA_ARGS__})
 

Typedefs

typedef struct ShmemStructOpts ShmemStructOpts
 
typedef struct ShmemHashOpts ShmemHashOpts
 
typedef void(* ShmemRequestCallback) (void *opaque_arg)
 
typedef void(* ShmemInitCallback) (void *opaque_arg)
 
typedef void(* ShmemAttachCallback) (void *opaque_arg)
 
typedef struct ShmemCallbacks ShmemCallbacks
 

Functions

void RegisterShmemCallbacks (const ShmemCallbacks *callbacks)
 
bool ShmemAddrIsValid (const void *addr)
 
void ShmemRequestStructWithOpts (const ShmemStructOpts *options)
 
void ShmemRequestHashWithOpts (const ShmemHashOpts *options)
 
voidShmemInitStruct (const char *name, Size size, bool *foundPtr)
 
HTABShmemInitHash (const char *name, int64 nelems, HASHCTL *infoP, int hash_flags)
 
voidShmemAlloc (Size size)
 
voidShmemAllocNoError (Size size)
 
Size add_size (Size s1, Size s2)
 
Size mul_size (Size s1, Size s2)
 
PGDLLIMPORT Size pg_get_shmem_pagesize (void)
 
void RequestAddinShmemSpace (Size size)
 

Macro Definition Documentation

◆ SHMEM_ATTACH_UNKNOWN_SIZE

#define SHMEM_ATTACH_UNKNOWN_SIZE   (-1)

Definition at line 69 of file shmem.h.

◆ SHMEM_CALLBACKS_ALLOW_AFTER_STARTUP

#define SHMEM_CALLBACKS_ALLOW_AFTER_STARTUP   0x00000001

Definition at line 167 of file shmem.h.

◆ ShmemRequestHash

#define ShmemRequestHash (   ...)     ShmemRequestHashWithOpts(&(ShmemHashOpts){__VA_ARGS__})

Definition at line 179 of file shmem.h.

180 {__VA_ARGS__})
static int fb(int x)

◆ ShmemRequestStruct

#define ShmemRequestStruct (   ...)     ShmemRequestStructWithOpts(&(ShmemStructOpts){__VA_ARGS__})

Definition at line 176 of file shmem.h.

177 {__VA_ARGS__})

Typedef Documentation

◆ ShmemAttachCallback

typedef void(* ShmemAttachCallback) (void *opaque_arg)

Definition at line 114 of file shmem.h.

◆ ShmemCallbacks

◆ ShmemHashOpts

◆ ShmemInitCallback

typedef void(* ShmemInitCallback) (void *opaque_arg)

Definition at line 113 of file shmem.h.

◆ ShmemRequestCallback

typedef void(* ShmemRequestCallback) (void *opaque_arg)

Definition at line 112 of file shmem.h.

◆ ShmemStructOpts

Function Documentation

◆ add_size()

Size add_size ( Size  s1,
Size  s2 
)
extern

Definition at line 1048 of file shmem.c.

1049{
1050 Size result;
1051
1053 ereport(ERROR,
1055 errmsg("requested shared memory size overflows size_t")));
1056 return result;
1057}
size_t Size
Definition c.h:689
uint32 result
int errcode(int sqlerrcode)
Definition elog.c:874
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:151
static bool pg_add_size_overflow(size_t a, size_t b, size_t *result)
Definition int.h:608
static char * errmsg
char * s1
char * s2

References ereport, errcode(), errmsg, ERROR, fb(), pg_add_size_overflow(), result, s1, and s2.

Referenced by _brin_parallel_estimate_shared(), _bt_parallel_estimate_shared(), _gin_parallel_estimate_shared(), ApplyLauncherShmemRequest(), AsyncShmemRequest(), AutoVacuumShmemRequest(), BackgroundWorkerShmemRequest(), btestimateparallelscan(), BTreeShmemRequest(), CalculateFastPathLockShmemSize(), CalculateShmemSize(), CheckpointerShmemRequest(), CreateAnonymousSegment(), estimate_variable_size(), EstimateClientConnectionInfoSpace(), EstimateComboCIDStateSpace(), EstimateGUCStateSpace(), EstimateLibraryStateSpace(), EstimateParamExecSpace(), EstimateParamListSpace(), EstimateSnapshotSpace(), EstimateTransactionStateSpace(), ExecAggEstimate(), ExecAppendEstimate(), ExecBitmapHeapEstimate(), ExecBitmapHeapInitializeDSM(), ExecHashEstimate(), ExecIncrementalSortEstimate(), ExecMemoizeEstimate(), ExecSortEstimate(), expand_planner_arrays(), hash_estimate_size(), index_parallelscan_estimate(), index_parallelscan_initialize(), InitializeShmemGUCs(), MultiXactShmemRequest(), PMSignalShmemRequest(), PredicateLockShmemRequest(), ProcArrayShmemRequest(), ProcGlobalShmemRequest(), ProcSignalShmemRequest(), ReplicationOriginShmemRequest(), ReplicationSlotsShmemRequest(), RequestAddinShmemSpace(), SerializeTransactionState(), SharedInvalShmemRequest(), shm_toc_estimate(), ShmemGetRequestedSize(), StatsShmemSize(), table_parallelscan_estimate(), tuplesort_estimate_shared(), TwoPhaseShmemRequest(), WaitLSNShmemRequest(), WalSndShmemRequest(), and XLOGShmemRequest().

◆ mul_size()

Size mul_size ( Size  s1,
Size  s2 
)
extern

Definition at line 1063 of file shmem.c.

1064{
1065 Size result;
1066
1068 ereport(ERROR,
1070 errmsg("requested shared memory size overflows size_t")));
1071 return result;
1072}
static bool pg_mul_size_overflow(size_t a, size_t b, size_t *result)
Definition int.h:642

References ereport, errcode(), errmsg, ERROR, fb(), pg_mul_size_overflow(), result, s1, and s2.

Referenced by _brin_begin_parallel(), _bt_begin_parallel(), _gin_begin_parallel(), AioBackendShmemSize(), AioHandleDataShmemSize(), AioHandleIOVShmemSize(), AioHandleShmemSize(), ApplyLauncherShmemRequest(), AsyncShmemRequest(), AutoVacuumShmemRequest(), BackendStatusShmemAttach(), BackendStatusShmemRequest(), BackgroundWorkerShmemRequest(), BTreeShmemRequest(), CalculateFastPathLockShmemSize(), CheckpointerShmemRequest(), EstimateComboCIDStateSpace(), EstimatePendingSyncsSpace(), EstimateReindexStateSpace(), EstimateSnapshotSpace(), EstimateTransactionStateSpace(), ExecAggEstimate(), ExecBitmapHeapEstimate(), ExecBitmapHeapInitializeDSM(), ExecHashEstimate(), ExecIncrementalSortEstimate(), ExecInitParallelPlan(), ExecMemoizeEstimate(), ExecParallelRetrieveInstrumentation(), ExecParallelRetrieveJitInstrumentation(), ExecParallelSetupTupleQueues(), ExecSortEstimate(), hash_estimate_size(), InitializeParallelDSM(), MultiXactShmemRequest(), parallel_vacuum_init(), PGSemaphoreShmemRequest(), PMSignalShmemRequest(), PredicateLockShmemRequest(), ProcArrayShmemRequest(), ProcGlobalShmemRequest(), ProcSignalShmemRequest(), ReplicationOriginShmemRequest(), ReplicationSlotsShmemRequest(), SharedInvalShmemRequest(), shm_toc_estimate(), tuplesort_estimate_shared(), TwoPhaseShmemRequest(), WaitLSNShmemRequest(), WalSndShmemRequest(), and XLOGShmemRequest().

◆ pg_get_shmem_pagesize()

PGDLLIMPORT Size pg_get_shmem_pagesize ( void  )
extern

Definition at line 1304 of file shmem.c.

1305{
1307#ifdef WIN32
1309
1311 os_page_size = sysinfo.dwPageSize;
1312#else
1314#endif
1315
1318
1321
1322 return os_page_size;
1323}
#define Assert(condition)
Definition c.h:943
bool IsUnderPostmaster
Definition globals.c:120
int huge_pages_status
Definition guc_tables.c:600
@ HUGE_PAGES_UNKNOWN
Definition pg_shmem.h:56
@ HUGE_PAGES_ON
Definition pg_shmem.h:54
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition sysv_shmem.c:480

◆ RegisterShmemCallbacks()

void RegisterShmemCallbacks ( const ShmemCallbacks callbacks)
extern

Definition at line 874 of file shmem.c.

875{
877 {
878 /*
879 * After-startup initialization or attachment. Call the appropriate
880 * callbacks immediately.
881 */
882 if ((callbacks->flags & SHMEM_CALLBACKS_ALLOW_AFTER_STARTUP) == 0)
883 elog(ERROR, "cannot request shared memory at this time");
884
886 }
887 else
888 {
889 /* Remember the callbacks for later */
891 (void *) callbacks);
892 }
893}
#define elog(elevel,...)
Definition elog.h:227
List * lappend(List *list, void *datum)
Definition list.c:339
static List * registered_shmem_callbacks
Definition shmem.c:158
static void CallShmemCallbacksAfterStartup(const ShmemCallbacks *callbacks)
Definition shmem.c:899
shmem_request_state
Definition shmem.c:186
@ SRS_DONE
Definition shmem.c:213
#define SHMEM_CALLBACKS_ALLOW_AFTER_STARTUP
Definition shmem.h:167

References CallShmemCallbacksAfterStartup(), elog, ERROR, ShmemCallbacks::flags, IsUnderPostmaster, lappend(), registered_shmem_callbacks, SHMEM_CALLBACKS_ALLOW_AFTER_STARTUP, and SRS_DONE.

Referenced by _PG_init().

◆ RequestAddinShmemSpace()

void RequestAddinShmemSpace ( Size  size)
extern

Definition at line 45 of file ipci.c.

46{
48 elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
50}
#define FATAL
Definition elog.h:41
static Size total_addin_request
Definition ipci.c:33
bool process_shmem_requests_in_progress
Definition miscinit.c:1792
Size add_size(Size s1, Size s2)
Definition shmem.c:1048

References add_size(), elog, FATAL, process_shmem_requests_in_progress, and total_addin_request.

◆ ShmemAddrIsValid()

bool ShmemAddrIsValid ( const void addr)
extern

Definition at line 850 of file shmem.c.

851{
852 return (addr >= ShmemBase) && (addr < ShmemEnd);
853}
static void * ShmemBase
Definition shmem.c:244
static void * ShmemEnd
Definition shmem.c:245

References ShmemBase, and ShmemEnd.

Referenced by ReleasePredXact().

◆ ShmemAlloc()

void * ShmemAlloc ( Size  size)
extern

Definition at line 764 of file shmem.c.

765{
766 void *newSpace;
767 Size allocated_size;
768
769 newSpace = ShmemAllocRaw(size, 0, &allocated_size);
770 if (!newSpace)
773 errmsg("out of shared memory (%zu bytes requested)",
774 size)));
775 return newSpace;
776}
static void * ShmemAllocRaw(Size size, Size alignment, Size *allocated_size)
Definition shmem.c:798

References ereport, errcode(), errmsg, ERROR, fb(), and ShmemAllocRaw().

Referenced by InitShmemAllocator(), and StatsShmemInit().

◆ ShmemAllocNoError()

void * ShmemAllocNoError ( Size  size)
extern

Definition at line 784 of file shmem.c.

785{
786 Size allocated_size;
787
788 return ShmemAllocRaw(size, 0, &allocated_size);
789}

References ShmemAllocRaw().

◆ ShmemInitHash()

HTAB * ShmemInitHash ( const char name,
int64  nelems,
HASHCTL infoP,
int  hash_flags 
)
extern

Definition at line 117 of file shmem_hash.c.

121{
122 bool found;
123 size_t size;
124 void *location;
125
126 size = hash_estimate_size(nelems, infoP->entrysize);
127
128 /*
129 * Look it up in the shmem index or allocate.
130 *
131 * NOTE: The area is requested internally as SHMEM_KIND_STRUCT instead of
132 * SHMEM_KIND_HASH. That's correct because we do the hash table
133 * initialization by calling shmem_hash_create() ourselves. (We don't
134 * expose the request kind to users; if we did, that would be confusing.)
135 */
136 location = ShmemInitStruct(name, size, &found);
137
138 return shmem_hash_create(location, size, found,
139 name, nelems, infoP, hash_flags);
140}
Size hash_estimate_size(int64 num_entries, Size entrysize)
Definition dynahash.c:763
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition shmem.c:1011
HTAB * shmem_hash_create(void *location, size_t size, bool found, const char *name, int64 nelems, HASHCTL *infoP, int hash_flags)
Definition shmem_hash.c:149
const char * name

References fb(), hash_estimate_size(), name, shmem_hash_create(), and ShmemInitStruct().

◆ ShmemInitStruct()

void * ShmemInitStruct ( const char name,
Size  size,
bool foundPtr 
)
extern

Definition at line 1011 of file shmem.c.

1012{
1013 void *ptr = NULL;
1015 .name = name,
1016 .size = size,
1017 .ptr = &ptr,
1018 };
1020
1024
1026
1027 /*
1028 * During postmaster startup, look up the existing entry if any.
1029 */
1030 *foundPtr = false;
1033
1034 /* Initialize it if not found */
1035 if (!*foundPtr)
1037
1039
1040 Assert(ptr != NULL);
1041 return ptr;
1042}
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
@ LW_EXCLUSIVE
Definition lwlock.h:104
static void InitShmemIndexEntry(ShmemRequest *request)
Definition shmem.c:512
@ SRS_INITIALIZING
Definition shmem.c:201
@ SRS_REQUESTING
Definition shmem.c:195
#define ShmemIndexLock
Definition shmem.c:237
static bool AttachShmemIndexEntry(ShmemRequest *request, bool missing_ok)
Definition shmem.c:581
@ SHMEM_KIND_STRUCT

References Assert, AttachShmemIndexEntry(), fb(), InitShmemIndexEntry(), IsUnderPostmaster, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), name, SHMEM_KIND_STRUCT, ShmemIndexLock, SRS_DONE, SRS_INITIALIZING, and SRS_REQUESTING.

Referenced by ShmemInitHash().

◆ ShmemRequestHashWithOpts()

void ShmemRequestHashWithOpts ( const ShmemHashOpts options)
extern

Definition at line 44 of file shmem_hash.c.

45{
47
48 Assert(options->name != NULL);
49
51 sizeof(ShmemHashOpts));
53
54 /* Set options for the fixed-size area holding the hash table */
55 options_copy->base.name = options->name;
56 options_copy->base.size = hash_estimate_size(options_copy->nelems,
57 options_copy->hash_info.entrysize);
58
60}
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
MemoryContext TopMemoryContext
Definition mcxt.c:166
void ShmemRequestInternal(ShmemStructOpts *options, ShmemRequestKind kind)
Definition shmem.c:337
@ SHMEM_KIND_HASH

References Assert, fb(), hash_estimate_size(), memcpy(), MemoryContextAlloc(), SHMEM_KIND_HASH, ShmemRequestInternal(), and TopMemoryContext.

◆ ShmemRequestStructWithOpts()