From: Robert Haas Date: Thu, 9 Feb 2017 19:59:57 +0000 (-0500) Subject: simplehash: Additional tweaks to make specifying an allocator work. X-Git-Tag: REL_10_BETA1~920 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=72257f95781af97108fa9a9e7224ec81a90e7693;p=postgresql simplehash: Additional tweaks to make specifying an allocator work. Even if we don't emit definitions for SH_ALLOCATE and SH_FREE, we still need prototypes. The user can't define them before including simplehash.h because SH_TYPE isn't available yet. For the allocator to be able to access private_data, it needs to become an argument to SH_CREATE. Previously we relied on callers to set that after returning from SH_CREATE, but SH_CREATE calls SH_ALLOCATE before returning. Dilip Kumar, reviewed by me. --- diff --git a/src/backend/executor/execGrouping.c b/src/backend/executor/execGrouping.c index 47c9656e1b..4b1f634e21 100644 --- a/src/backend/executor/execGrouping.c +++ b/src/backend/executor/execGrouping.c @@ -330,8 +330,7 @@ BuildTupleHashTable(int numCols, AttrNumber *keyColIdx, else hashtable->hash_iv = 0; - hashtable->hashtab = tuplehash_create(tablecxt, nbuckets); - hashtable->hashtab->private_data = hashtable; + hashtable->hashtab = tuplehash_create(tablecxt, nbuckets, hashtable); return hashtable; } diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c index 7b31948fd2..0885812420 100644 --- a/src/backend/nodes/tidbitmap.c +++ b/src/backend/nodes/tidbitmap.c @@ -244,7 +244,7 @@ tbm_create_pagetable(TIDBitmap *tbm) Assert(tbm->status != TBM_HASH); Assert(tbm->pagetable == NULL); - tbm->pagetable = pagetable_create(tbm->mcxt, 128); + tbm->pagetable = pagetable_create(tbm->mcxt, 128, NULL); /* If entry1 is valid, push it into the hashtable */ if (tbm->status == TBM_ONE_PAGE) diff --git a/src/include/lib/simplehash.h b/src/include/lib/simplehash.h index ff50633549..2635bd720e 100644 --- a/src/include/lib/simplehash.h +++ b/src/include/lib/simplehash.h @@ -137,7 +137,8 @@ typedef struct SH_ITERATOR } SH_ITERATOR; /* externally visible function prototypes */ -SH_SCOPE SH_TYPE *SH_CREATE(MemoryContext ctx, uint32 nelements); +SH_SCOPE SH_TYPE *SH_CREATE(MemoryContext ctx, uint32 nelements, + void *private_data); SH_SCOPE void SH_DESTROY(SH_TYPE *tb); SH_SCOPE void SH_GROW(SH_TYPE *tb, uint32 newsize); SH_SCOPE SH_ELEMENT_TYPE *SH_INSERT(SH_TYPE *tb, SH_KEY_TYPE key, bool *found); @@ -280,6 +281,10 @@ SH_ENTRY_HASH(SH_TYPE *tb, SH_ELEMENT_TYPE * entry) #endif } +/* default memory allocator function */ +static inline void *SH_ALLOCATE(SH_TYPE *type, Size size); +static inline void SH_FREE(SH_TYPE *type, void *pointer); + #ifndef SH_USE_NONDEFAULT_ALLOCATOR /* default memory allocator function */ @@ -309,13 +314,14 @@ SH_FREE(SH_TYPE *type, void *pointer) * the passed-in context. */ SH_SCOPE SH_TYPE * -SH_CREATE(MemoryContext ctx, uint32 nelements) +SH_CREATE(MemoryContext ctx, uint32 nelements, void *private_data) { SH_TYPE *tb; uint64 size; tb = MemoryContextAllocZero(ctx, sizeof(SH_TYPE)); tb->ctx = ctx; + tb->private_data = private_data; /* increase nelements by fillfactor, want to store nelements elements */ size = Min((double) SH_MAX_SIZE, ((double) nelements) / SH_FILLFACTOR);