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 1043 of file shmem.c.

1044{
1045 Size result;
1046
1048 ereport(ERROR,
1050 errmsg("requested shared memory size overflows size_t")));
1051 return result;
1052}
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 1058 of file shmem.c.

1059{
1060 Size result;
1061
1063 ereport(ERROR,
1065 errmsg("requested shared memory size overflows size_t")));
1066 return result;
1067}
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 1299 of file shmem.c.

1300{
1302#ifdef WIN32
1304
1306 os_page_size = sysinfo.dwPageSize;
1307#else
1309#endif
1310
1313
1316
1317 return os_page_size;
1318}
#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 871 of file shmem.c.

872{
874 {
875 /*
876 * After-startup initialization or attachment. Call the appropriate
877 * callbacks immediately.
878 */
879 if ((callbacks->flags & SHMEM_CALLBACKS_ALLOW_AFTER_STARTUP) == 0)
880 elog(ERROR, "cannot request shared memory at this time");
881
883 }
884 else
885 {
886 /* Remember the callbacks for later */
888 (void *) callbacks);
889 }
890}
#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:896
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:1043

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

◆ ShmemAddrIsValid()

bool ShmemAddrIsValid ( const void addr)
extern

Definition at line 847 of file shmem.c.

848{
849 return (addr >= ShmemBase) && (addr < ShmemEnd);
850}
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 761 of file shmem.c.

762{
763 void *newSpace;
764 Size allocated_size;
765
766 newSpace = ShmemAllocRaw(size, 0, &allocated_size);
767 if (!newSpace)
770 errmsg("out of shared memory (%zu bytes requested)",
771 size)));
772 return newSpace;
773}
static void * ShmemAllocRaw(Size size, Size alignment, Size *allocated_size)
Definition shmem.c:795

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

Referenced by InitShmemAllocator(), and StatsShmemInit().

◆ ShmemAllocNoError()

void * ShmemAllocNoError ( Size  size)
extern

Definition at line 781 of file shmem.c.

782{
783 Size allocated_size;
784
785 return ShmemAllocRaw(size, 0, &allocated_size);
786}

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:1006
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 1006 of file shmem.c.

1007{
1008 void *ptr = NULL;
1010 .name = name,
1011 .size = size,
1012 .ptr = &ptr,
1013 };
1015
1019
1021
1022 /*
1023 * During postmaster startup, look up the existing entry if any.
1024 */
1025 *foundPtr = false;
1028
1029 /* Initialize it if not found */
1030 if (!*foundPtr)
1032
1034
1035 Assert(ptr != NULL);
1036 return ptr;
1037}
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:509
@ 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:578
@ 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:336
@ SHMEM_KIND_HASH

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

◆ ShmemRequestStructWithOpts()