Skip to content

Commit 4899504

Browse files
committed
simplehash: Allow use of simplehash without MemoryContext.
If the SH_RAW_ALLOCATOR is defined, it will be used to allocate bytes for the hash table, and no dependencies on MemoryContext will exist. This means, in particular, that the SH_CREATE function will not take a MemoryContext argument. Patch by me, reviewed by Andres Freund. Discussion: http://postgr.es/m/CA+Tgmob8oyh02NrZW=xCScB+5GyJ-jVowE3+TWTUmPF=FsGWTA@mail.gmail.com
1 parent b1cc572 commit 4899504

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/include/lib/simplehash.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* - SH_DEFINE - if defined function definitions are generated
2424
* - SH_SCOPE - in which scope (e.g. extern, static inline) do function
2525
* declarations reside
26+
* - SH_RAW_ALLOCATOR - if defined, memory contexts are not used; instead,
27+
* use this to allocate bytes
2628
* - SH_USE_NONDEFAULT_ALLOCATOR - if defined no element allocator functions
2729
* are defined, so you can supply your own
2830
* The following parameters are only relevant when SH_DEFINE is defined:
@@ -121,8 +123,10 @@ typedef struct SH_TYPE
121123
/* hash buckets */
122124
SH_ELEMENT_TYPE *data;
123125

126+
#ifndef SH_RAW_ALLOCATOR
124127
/* memory context to use for allocations */
125128
MemoryContext ctx;
129+
#endif
126130

127131
/* user defined data, useful for callbacks */
128132
void *private_data;
@@ -142,8 +146,12 @@ typedef struct SH_ITERATOR
142146
} SH_ITERATOR;
143147

144148
/* externally visible function prototypes */
149+
#ifdef SH_RAW_ALLOCATOR
150+
SH_SCOPE SH_TYPE *SH_CREATE(uint32 nelements, void *private_data);
151+
#else
145152
SH_SCOPE SH_TYPE *SH_CREATE(MemoryContext ctx, uint32 nelements,
146153
void *private_data);
154+
#endif
147155
SH_SCOPE void SH_DESTROY(SH_TYPE * tb);
148156
SH_SCOPE void SH_RESET(SH_TYPE * tb);
149157
SH_SCOPE void SH_GROW(SH_TYPE * tb, uint32 newsize);
@@ -165,7 +173,9 @@ SH_SCOPE void SH_STAT(SH_TYPE * tb);
165173
/* generate implementation of the hash table */
166174
#ifdef SH_DEFINE
167175

176+
#ifndef SH_RAW_ALLOCATOR
168177
#include "utils/memutils.h"
178+
#endif
169179

170180
/* max data array size,we allow up to PG_UINT32_MAX buckets, including 0 */
171181
#define SH_MAX_SIZE (((uint64) PG_UINT32_MAX) + 1)
@@ -328,8 +338,12 @@ static inline void SH_FREE(SH_TYPE * type, void *pointer);
328338
static inline void *
329339
SH_ALLOCATE(SH_TYPE * type, Size size)
330340
{
341+
#ifdef SH_RAW_ALLOCATOR
342+
return SH_RAW_ALLOCATOR(size);
343+
#else
331344
return MemoryContextAllocExtended(type->ctx, size,
332345
MCXT_ALLOC_HUGE | MCXT_ALLOC_ZERO);
346+
#endif
333347
}
334348

335349
/* default memory free function */
@@ -350,14 +364,23 @@ SH_FREE(SH_TYPE * type, void *pointer)
350364
* Memory other than for the array of elements will still be allocated from
351365
* the passed-in context.
352366
*/
367+
#ifdef SH_RAW_ALLOCATOR
368+
SH_SCOPE SH_TYPE *
369+
SH_CREATE(uint32 nelements, void *private_data)
370+
#else
353371
SH_SCOPE SH_TYPE *
354372
SH_CREATE(MemoryContext ctx, uint32 nelements, void *private_data)
373+
#endif
355374
{
356375
SH_TYPE *tb;
357376
uint64 size;
358377

378+
#ifdef SH_RAW_ALLOCATOR
379+
tb = SH_RAW_ALLOCATOR(sizeof(SH_TYPE));
380+
#else
359381
tb = MemoryContextAllocZero(ctx, sizeof(SH_TYPE));
360382
tb->ctx = ctx;
383+
#endif
361384
tb->private_data = private_data;
362385

363386
/* increase nelements by fillfactor, want to store nelements elements */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy