From f08f4d2cc196a210dca1edd7195656c62481fea3 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Sat, 21 Mar 2026 13:07:28 +0200 Subject: [PATCH v20260405 01/15] refactor: Move ShmemInitHash to separate file In preparation for next commits --- src/backend/storage/ipc/Makefile | 1 + src/backend/storage/ipc/meson.build | 1 + src/backend/storage/ipc/shmem.c | 108 ---------------------- src/backend/storage/ipc/shmem_hash.c | 130 +++++++++++++++++++++++++++ src/include/storage/shmem.h | 9 +- 5 files changed, 139 insertions(+), 110 deletions(-) create mode 100644 src/backend/storage/ipc/shmem_hash.c diff --git a/src/backend/storage/ipc/Makefile b/src/backend/storage/ipc/Makefile index 9a07f6e1d92..f71653bbe48 100644 --- a/src/backend/storage/ipc/Makefile +++ b/src/backend/storage/ipc/Makefile @@ -22,6 +22,7 @@ OBJS = \ shm_mq.o \ shm_toc.o \ shmem.o \ + shmem_hash.o \ signalfuncs.o \ sinval.o \ sinvaladt.o \ diff --git a/src/backend/storage/ipc/meson.build b/src/backend/storage/ipc/meson.build index 9c1ca954d9d..b8c31e29967 100644 --- a/src/backend/storage/ipc/meson.build +++ b/src/backend/storage/ipc/meson.build @@ -14,6 +14,7 @@ backend_sources += files( 'shm_mq.c', 'shm_toc.c', 'shmem.c', + 'shmem_hash.c', 'signalfuncs.c', 'sinval.c', 'sinvaladt.c', diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c index 3cb51ad62f8..c994f7674ec 100644 --- a/src/backend/storage/ipc/shmem.c +++ b/src/backend/storage/ipc/shmem.c @@ -96,9 +96,6 @@ typedef struct ShmemAllocatorData #define ShmemIndexLock (&ShmemAllocator->index_lock) -static HTAB *shmem_hash_create(void *location, size_t size, bool found, - const char *name, int64 nelems, HASHCTL *infoP, int hash_flags); -static void *ShmemHashAlloc(Size size, void *alloc_arg); static void *ShmemAllocRaw(Size size, Size *allocated_size); /* shared memory global variables */ @@ -257,29 +254,6 @@ ShmemAllocNoError(Size size) return ShmemAllocRaw(size, &allocated_size); } -/* - * ShmemHashAlloc -- alloc callback for shared memory hash tables - * - * Carve out the allocation from a pre-allocated region. All shared memory - * hash tables are initialized with HASH_FIXED_SIZE, so all the allocations - * happen upfront during initialization and no locking is required. - */ -static void * -ShmemHashAlloc(Size size, void *alloc_arg) -{ - shmem_hash_allocator *allocator = (shmem_hash_allocator *) alloc_arg; - void *result; - - size = MAXALIGN(size); - - if (allocator->end - allocator->next < size) - return NULL; - result = allocator->next; - allocator->next += size; - - return result; -} - /* * ShmemAllocRaw -- allocate align chunk and return allocated size * @@ -341,88 +315,6 @@ ShmemAddrIsValid(const void *addr) return (addr >= ShmemBase) && (addr < ShmemEnd); } -/* - * ShmemInitHash -- Create and initialize, or attach to, a - * shared memory hash table. - * - * We assume caller is doing some kind of synchronization - * so that two processes don't try to create/initialize the same - * table at once. (In practice, all creations are done in the postmaster - * process; child processes should always be attaching to existing tables.) - * - * nelems is the maximum number of hashtable entries. - * - * *infoP and hash_flags must specify at least the entry sizes and key - * comparison semantics (see hash_create()). Flag bits and values specific - * to shared-memory hash tables are added here, except that callers may - * choose to specify HASH_PARTITION. - * - * Note: before Postgres 9.0, this function returned NULL for some failure - * cases. Now, it always throws error instead, so callers need not check - * for NULL. - */ -HTAB * -ShmemInitHash(const char *name, /* table string name for shmem index */ - int64 nelems, /* size of the table */ - HASHCTL *infoP, /* info about key and bucket size */ - int hash_flags) /* info about infoP */ -{ - bool found; - size_t size; - void *location; - - size = hash_estimate_size(nelems, infoP->entrysize); - - /* look it up in the shmem index or allocate */ - location = ShmemInitStruct(name, size, &found); - - return shmem_hash_create(location, size, found, - name, nelems, infoP, hash_flags); -} - -/* - * Initialize or attach to a shared hash table in the given shmem region. - * - * This is extracted from ShmemInitHash() to allow InitShmemAllocator() to - * share the logic for bootstrapping the ShmemIndex hash table. - */ -static HTAB * -shmem_hash_create(void *location, size_t size, bool found, - const char *name, int64 nelems, HASHCTL *infoP, int hash_flags) -{ - shmem_hash_allocator allocator; - - /* - * Hash tables allocated in shared memory have a fixed directory and have - * all elements allocated upfront. We don't support growing because we'd - * need to grow the underlying shmem region with it. - * - * The shared memory allocator must be specified too. - */ - infoP->alloc = ShmemHashAlloc; - infoP->alloc_arg = NULL; - hash_flags |= HASH_SHARED_MEM | HASH_ALLOC | HASH_FIXED_SIZE; - - /* - * if it already exists, attach to it rather than allocate and initialize - * new space - */ - if (!found) - { - allocator.next = (char *) location; - allocator.end = (char *) location + size; - infoP->alloc_arg = &allocator; - } - else - { - /* Pass location of hashtable header to hash_create */ - infoP->hctl = (HASHHDR *) location; - hash_flags |= HASH_ATTACH; - } - - return hash_create(name, nelems, infoP, hash_flags); -} - /* * ShmemInitStruct -- Create/attach to a structure in shared memory. * diff --git a/src/backend/storage/ipc/shmem_hash.c b/src/backend/storage/ipc/shmem_hash.c new file mode 100644 index 00000000000..0b05730129e --- /dev/null +++ b/src/backend/storage/ipc/shmem_hash.c @@ -0,0 +1,130 @@ +/*------------------------------------------------------------------------- + * + * shmem_hash.c + * hash table implementation in shared memory + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * A shared memory hash table implementation on top of the named, fixed-size + * shared memory areas managed by shmem.c. Hash tables have a fixed maximum + * size, but their actual size can vary dynamically. When entries are added + * to the table, more space is allocated. Each shared data structure and hash + * has a string name to identify it. + * + * IDENTIFICATION + * src/backend/storage/ipc/shmem_hash.c + * + *------------------------------------------------------------------------- + */ + +#include "postgres.h" + +#include "storage/shmem.h" + +static void *ShmemHashAlloc(Size size, void *alloc_arg); + +/* + * ShmemInitHash -- Create and initialize, or attach to, a + * shared memory hash table. + * + * We assume caller is doing some kind of synchronization + * so that two processes don't try to create/initialize the same + * table at once. (In practice, all creations are done in the postmaster + * process; child processes should always be attaching to existing tables.) + * + * nelems is the maximum number of hashtable entries. + * + * *infoP and hash_flags must specify at least the entry sizes and key + * comparison semantics (see hash_create()). Flag bits and values specific + * to shared-memory hash tables are added here, except that callers may + * choose to specify HASH_PARTITION. + * + * Note: before Postgres 9.0, this function returned NULL for some failure + * cases. Now, it always throws error instead, so callers need not check + * for NULL. + */ +HTAB * +ShmemInitHash(const char *name, /* table string name for shmem index */ + int64 nelems, /* size of the table */ + HASHCTL *infoP, /* info about key and bucket size */ + int hash_flags) /* info about infoP */ +{ + bool found; + size_t size; + void *location; + + size = hash_estimate_size(nelems, infoP->entrysize); + + /* look it up in the shmem index or allocate */ + location = ShmemInitStruct(name, size, &found); + + return shmem_hash_create(location, size, found, + name, nelems, infoP, hash_flags); +} + +/* + * Initialize or attach to a shared hash table in the given shmem region. + * + * This is extracted from ShmemInitHash() to allow InitShmemAllocator() to + * share the logic for bootstrapping the ShmemIndex hash table. + */ +HTAB * +shmem_hash_create(void *location, size_t size, bool found, + const char *name, int64 nelems, HASHCTL *infoP, int hash_flags) +{ + shmem_hash_allocator allocator; + + /* + * Hash tables allocated in shared memory have a fixed directory and have + * all elements allocated upfront. We don't support growing because we'd + * need to grow the underlying shmem region with it. + * + * The shared memory allocator must be specified too. + */ + infoP->alloc = ShmemHashAlloc; + infoP->alloc_arg = NULL; + hash_flags |= HASH_SHARED_MEM | HASH_ALLOC | HASH_FIXED_SIZE; + + /* + * if it already exists, attach to it rather than allocate and initialize + * new space + */ + if (!found) + { + allocator.next = (char *) location; + allocator.end = (char *) location + size; + infoP->alloc_arg = &allocator; + } + else + { + /* Pass location of hashtable header to hash_create */ + infoP->hctl = (HASHHDR *) location; + hash_flags |= HASH_ATTACH; + } + + return hash_create(name, nelems, infoP, hash_flags); +} + +/* + * ShmemHashAlloc -- alloc callback for shared memory hash tables + * + * Carve out the allocation from a pre-allocated region. All shared memory + * hash tables are initialized with HASH_FIXED_SIZE, so all the allocations + * happen upfront during initialization and no locking is required. + */ +static void * +ShmemHashAlloc(Size size, void *alloc_arg) +{ + shmem_hash_allocator *allocator = (shmem_hash_allocator *) alloc_arg; + void *result; + + size = MAXALIGN(size); + + if (allocator->end - allocator->next < size) + return NULL; + result = allocator->next; + allocator->next += size; + + return result; +} diff --git a/src/include/storage/shmem.h b/src/include/storage/shmem.h index a2eb499d63c..82f5403c952 100644 --- a/src/include/storage/shmem.h +++ b/src/include/storage/shmem.h @@ -30,15 +30,20 @@ typedef struct PGShmemHeader PGShmemHeader; /* avoid including extern void InitShmemAllocator(PGShmemHeader *seghdr); extern void *ShmemAlloc(Size size); extern void *ShmemAllocNoError(Size size); +extern void *ShmemHashAlloc(Size size, void *alloc_arg); extern bool ShmemAddrIsValid(const void *addr); -extern HTAB *ShmemInitHash(const char *name, int64 nelems, - HASHCTL *infoP, int hash_flags); extern void *ShmemInitStruct(const char *name, Size size, bool *foundPtr); extern Size add_size(Size s1, Size s2); extern Size mul_size(Size s1, Size s2); extern PGDLLIMPORT Size pg_get_shmem_pagesize(void); +/* shmem_hash.c */ +extern HTAB *ShmemInitHash(const char *name, int64 nelems, + HASHCTL *infoP, int hash_flags); +extern HTAB *shmem_hash_create(void *location, size_t size, bool found, + const char *name, int64 nelems, HASHCTL *infoP, int hash_flags) + /* ipci.c */ extern void RequestAddinShmemSpace(Size size); base-commit: de28140ded8d4ba00faf905ec3530ffeb8a34a53 -- 2.34.1