PostgreSQL Source Code git master
Loading...
Searching...
No Matches
shmem_hash.c File Reference
#include "postgres.h"
#include "storage/shmem.h"
#include "storage/shmem_internal.h"
#include "utils/memutils.h"
Include dependency graph for shmem_hash.c:

Go to the source code of this file.

Data Structures

struct  shmem_hash_allocator
 

Typedefs

typedef struct shmem_hash_allocator shmem_hash_allocator
 

Functions

static voidShmemHashAlloc (Size size, void *alloc_arg)
 
void ShmemRequestHashWithOpts (const ShmemHashOpts *options)
 
void shmem_hash_init (void *location, ShmemStructOpts *base_options)
 
void shmem_hash_attach (void *location, ShmemStructOpts *base_options)
 
HTABShmemInitHash (const char *name, int64 nelems, HASHCTL *infoP, int hash_flags)
 
HTABshmem_hash_create (void *location, size_t size, bool found, const char *name, int64 nelems, HASHCTL *infoP, int hash_flags)
 

Typedef Documentation

◆ shmem_hash_allocator

Function Documentation

◆ shmem_hash_attach()

void shmem_hash_attach ( void location,
ShmemStructOpts base_options 
)

Definition at line 79 of file shmem_hash.c.

80{
82 int hash_flags = options->hash_flags;
83 HTAB *htab;
84
85 /* attach to it rather than allocate and initialize new space */
86 hash_flags |= HASH_ATTACH;
87 options->hash_info.hctl = location;
88 Assert(options->hash_info.hctl != NULL);
89 htab = shmem_hash_create(location, options->base.size, true,
90 options->name,
91 options->nelems, &options->hash_info, hash_flags);
92
93 if (options->ptr)
94 *options->ptr = htab;
95}
#define Assert(condition)
Definition c.h:943
#define HASH_ATTACH
Definition hsearch.h:99
static int fb(int x)
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

References Assert, fb(), HASH_ATTACH, and shmem_hash_create().

Referenced by AttachShmemIndexEntry().

◆ shmem_hash_create()

HTAB * shmem_hash_create ( void location,
size_t  size,
bool  found,
const char name,
int64  nelems,
HASHCTL infoP,
int  hash_flags 
)

Definition at line 149 of file shmem_hash.c.

151{
153
154 /*
155 * Hash tables allocated in shared memory have a fixed directory and have
156 * all elements allocated upfront. We don't support growing because we'd
157 * need to grow the underlying shmem region with it.
158 *
159 * The shared memory allocator must be specified too.
160 */
161 infoP->alloc = ShmemHashAlloc;
162 infoP->alloc_arg = NULL;
164
165 /*
166 * if it already exists, attach to it rather than allocate and initialize
167 * new space
168 */
169 if (!found)
170 {
171 allocator.next = (char *) location;
172 allocator.end = (char *) location + size;
173 infoP->alloc_arg = &allocator;
174 }
175 else
176 {
177 /* Pass location of hashtable header to hash_create */
178 infoP->hctl = (HASHHDR *) location;
179 hash_flags |= HASH_ATTACH;
180 }
181
182 return hash_create(name, nelems, infoP, hash_flags);
183}
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:360
#define HASH_ALLOC
Definition hsearch.h:96
#define HASH_SHARED_MEM
Definition hsearch.h:98
#define HASH_FIXED_SIZE
Definition hsearch.h:100
static void * ShmemHashAlloc(Size size, void *alloc_arg)
Definition shmem_hash.c:193
const char * name

References fb(), HASH_ALLOC, HASH_ATTACH, hash_create(), HASH_FIXED_SIZE, HASH_SHARED_MEM, name, shmem_hash_allocator::next, and ShmemHashAlloc().

Referenced by InitShmemAllocator(), shmem_hash_attach(), shmem_hash_init(), and ShmemInitHash().

◆ shmem_hash_init()

void shmem_hash_init ( void location,
ShmemStructOpts base_options 
)

Definition at line 63 of file shmem_hash.c.

64{
66 int hash_flags = options->hash_flags;
67 HTAB *htab;
68
69 options->hash_info.hctl = location;
70 htab = shmem_hash_create(location, options->base.size, false,
71 options->name,
72 options->nelems, &options->hash_info, hash_flags);
73
74 if (options->ptr)
75 *options->ptr = htab;
76}

References fb(), and shmem_hash_create().

Referenced by InitShmemIndexEntry().

◆ ShmemHashAlloc()

static void * ShmemHashAlloc ( Size  size,
void alloc_arg 
)
static

Definition at line 193 of file shmem_hash.c.

194{
196 void *result;
197
198 size = MAXALIGN(size);
199
200 if (allocator->end - allocator->next < size)
201 return NULL;
202 result = allocator->next;
203 allocator->next += size;
204
205 return result;
206}
#define MAXALIGN(LEN)
Definition c.h:896
uint32 result

References fb(), MAXALIGN, and result.

Referenced by shmem_hash_create().

◆ ShmemInitHash()

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

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

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

◆ ShmemRequestHashWithOpts()

void ShmemRequestHashWithOpts ( const ShmemHashOpts options)

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.