PostgreSQL Source Code git master
test_dsm_registry.c
Go to the documentation of this file.
1/*--------------------------------------------------------------------------
2 *
3 * test_dsm_registry.c
4 * Test the dynamic shared memory registry.
5 *
6 * Copyright (c) 2024-2025, PostgreSQL Global Development Group
7 *
8 * IDENTIFICATION
9 * src/test/modules/test_dsm_registry/test_dsm_registry.c
10 *
11 * -------------------------------------------------------------------------
12 */
13#include "postgres.h"
14
15#include "fmgr.h"
17#include "storage/lwlock.h"
18#include "utils/builtins.h"
19
21
23{
24 int val;
27
29{
30 char key[64];
33
37
44};
45
46static void
47init_tdr_dsm(void *ptr, void *arg)
48{
50
51 if ((int) (intptr_t) arg != 5432)
52 elog(ERROR, "unexpected arg value %d", (int) (intptr_t) arg);
53
54 LWLockInitialize(&dsm->lck, LWLockNewTrancheId("test_dsm_registry"));
55 dsm->val = 0;
56}
57
58static void
60{
61 bool found;
62
63 tdr_dsm = GetNamedDSMSegment("test_dsm_registry_dsm",
66 &found, (void *) (intptr_t) 5432);
67
68 if (tdr_dsa == NULL)
69 tdr_dsa = GetNamedDSA("test_dsm_registry_dsa", &found);
70
71 if (tdr_hash == NULL)
72 tdr_hash = GetNamedDSHash("test_dsm_registry_hash", &dsh_params, &found);
73}
74
78{
80
84
86}
87
91{
92 int ret;
93
95
97 ret = tdr_dsm->val;
99
100 PG_RETURN_INT32(ret);
101}
102
104Datum
106{
110 bool found;
111
112 if (strlen(key) >= offsetof(TestDSMRegistryHashEntry, val))
114 (errmsg("key too long")));
115
117
118 entry = dshash_find_or_insert(tdr_hash, key, &found);
119 if (found)
120 dsa_free(tdr_dsa, entry->val);
121
122 entry->val = dsa_allocate(tdr_dsa, strlen(val) + 1);
123 strcpy(dsa_get_address(tdr_dsa, entry->val), val);
124
126
128}
129
131Datum
133{
136 text *val = NULL;
137
139
140 entry = dshash_find(tdr_hash, key, false);
141 if (entry == NULL)
143
145
147
149}
#define TextDatumGetCString(d)
Definition: builtins.h:98
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:957
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:841
uint64 dsa_pointer
Definition: dsa.h:62
#define dsa_allocate(area, size)
Definition: dsa.h:109
void dshash_strcpy(void *dest, const void *src, size_t size, void *arg)
Definition: dshash.c:624
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition: dshash.c:560
void * dshash_find(dshash_table *hash_table, const void *key, bool exclusive)
Definition: dshash.c:392
dshash_hash dshash_strhash(const void *v, size_t size, void *arg)
Definition: dshash.c:613
int dshash_strcmp(const void *a, const void *b, size_t size, void *arg)
Definition: dshash.c:601
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition: dshash.c:435
dsa_area * GetNamedDSA(const char *name, bool *found)
Definition: dsm_registry.c:276
void * GetNamedDSMSegment(const char *name, size_t size, void(*init_callback)(void *ptr, void *arg), bool *found, void *arg)
Definition: dsm_registry.c:187
dshash_table * GetNamedDSHash(const char *name, const dshash_parameters *params, bool *found)
Definition: dsm_registry.c:357
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:150
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
long val
Definition: informix.c:689
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1174
int LWLockNewTrancheId(const char *name)
Definition: lwlock.c:596
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1894
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:698
@ LW_SHARED
Definition: lwlock.h:113
@ LW_EXCLUSIVE
Definition: lwlock.h:112
void * arg
uint64_t Datum
Definition: postgres.h:70
Definition: lwlock.h:42
Definition: dsa.c:348
Definition: c.h:706
static dshash_table * tdr_hash
struct TestDSMRegistryHashEntry TestDSMRegistryHashEntry
Datum set_val_in_shmem(PG_FUNCTION_ARGS)
static void init_tdr_dsm(void *ptr, void *arg)
PG_MODULE_MAGIC
Datum get_val_in_shmem(PG_FUNCTION_ARGS)
static void tdr_attach_shmem(void)
Datum set_val_in_hash(PG_FUNCTION_ARGS)
static const dshash_parameters dsh_params
static dsa_area * tdr_dsa
Datum get_val_in_hash(PG_FUNCTION_ARGS)
static TestDSMRegistryStruct * tdr_dsm
PG_FUNCTION_INFO_V1(set_val_in_shmem)
struct TestDSMRegistryStruct TestDSMRegistryStruct
text * cstring_to_text(const char *s)
Definition: varlena.c:181