23
23
* - SH_DEFINE - if defined function definitions are generated
24
24
* - SH_SCOPE - in which scope (e.g. extern, static inline) do function
25
25
* declarations reside
26
+ * - SH_USE_NONDEFAULT_ALLOCATOR - if defined no element allocator functions
27
+ * are defined, so you can supply your own
26
28
* The following parameters are only relevant when SH_DEFINE is defined:
27
29
* - SH_KEY - name of the element in SH_ELEMENT_TYPE containing the hash key
28
30
* - SH_EQUAL(table, a, b) - compare two table keys
77
79
#define SH_START_ITERATE SH_MAKE_NAME(start_iterate)
78
80
#define SH_START_ITERATE_AT SH_MAKE_NAME(start_iterate_at)
79
81
#define SH_ITERATE SH_MAKE_NAME(iterate)
82
+ #define SH_ALLOCATE SH_MAKE_NAME(allocate)
83
+ #define SH_FREE SH_MAKE_NAME(free)
80
84
#define SH_STAT SH_MAKE_NAME(stat)
81
85
82
86
/* internal helper functions (no externally visible prototypes) */
87
91
#define SH_INITIAL_BUCKET SH_MAKE_NAME(initial_bucket)
88
92
#define SH_ENTRY_HASH SH_MAKE_NAME(entry_hash)
89
93
90
- /* Allocation function for hash table elements */
91
- #ifndef SIMPLEHASH_TYPEDEFS
92
- #define SIMPLEHASH_TYPEDEFS
93
- typedef void * (* simplehash_allocate ) (Size size , void * args );
94
- typedef void (* simplehash_free ) (void * pointer , void * args );
95
- #endif
96
-
97
94
/* generate forward declarations necessary to use the hash table */
98
95
#ifdef SH_DECLARE
99
96
@@ -119,11 +116,6 @@ typedef struct SH_TYPE
119
116
/* hash buckets */
120
117
SH_ELEMENT_TYPE * data ;
121
118
122
- /* Allocation and free functions, and the associated context. */
123
- simplehash_allocate element_alloc ;
124
- simplehash_free element_free ;
125
- void * element_args ;
126
-
127
119
/* memory context to use for allocations */
128
120
MemoryContext ctx ;
129
121
@@ -145,8 +137,7 @@ typedef struct SH_ITERATOR
145
137
} SH_ITERATOR ;
146
138
147
139
/* externally visible function prototypes */
148
- SH_SCOPE SH_TYPE * SH_CREATE (MemoryContext ctx , uint32 nelements ,
149
- simplehash_allocate allocfunc , simplehash_free freefunc , void * args );
140
+ SH_SCOPE SH_TYPE * SH_CREATE (MemoryContext ctx , uint32 nelements );
150
141
SH_SCOPE void SH_DESTROY (SH_TYPE * tb );
151
142
SH_SCOPE void SH_GROW (SH_TYPE * tb , uint32 newsize );
152
143
SH_SCOPE SH_ELEMENT_TYPE * SH_INSERT (SH_TYPE * tb , SH_KEY_TYPE key , bool * found );
@@ -289,23 +280,25 @@ SH_ENTRY_HASH(SH_TYPE *tb, SH_ELEMENT_TYPE * entry)
289
280
#endif
290
281
}
291
282
283
+ #ifndef SH_USE_NONDEFAULT_ALLOCATOR
284
+
292
285
/* default memory allocator function */
293
- static void *
294
- SH_DEFAULT_ALLOC ( Size size , void * args )
286
+ static inline void *
287
+ SH_ALLOCATE ( SH_TYPE * type , Size size )
295
288
{
296
- MemoryContext context = (MemoryContext ) args ;
297
-
298
- return MemoryContextAllocExtended (context , size ,
289
+ return MemoryContextAllocExtended (type -> ctx , size ,
299
290
MCXT_ALLOC_HUGE | MCXT_ALLOC_ZERO );
300
291
}
301
292
302
293
/* default memory free function */
303
- static void
304
- SH_DEFAULT_FREE ( void * pointer , void * args )
294
+ static inline void
295
+ SH_FREE ( SH_TYPE * type , void * pointer )
305
296
{
306
297
pfree (pointer );
307
298
}
308
299
300
+ #endif
301
+
309
302
/*
310
303
* Create a hash table with enough space for `nelements` distinct members.
311
304
* Memory for the hash table is allocated from the passed-in context. If
@@ -316,8 +309,7 @@ SH_DEFAULT_FREE(void *pointer, void *args)
316
309
* the passed-in context.
317
310
*/
318
311
SH_SCOPE SH_TYPE *
319
- SH_CREATE (MemoryContext ctx , uint32 nelements , simplehash_allocate allocfunc ,
320
- simplehash_free freefunc , void * args )
312
+ SH_CREATE (MemoryContext ctx , uint32 nelements )
321
313
{
322
314
SH_TYPE * tb ;
323
315
uint64 size ;
@@ -330,22 +322,7 @@ SH_CREATE(MemoryContext ctx, uint32 nelements, simplehash_allocate allocfunc,
330
322
331
323
SH_COMPUTE_PARAMETERS (tb , size );
332
324
333
- if (allocfunc == NULL )
334
- {
335
- tb -> element_alloc = SH_DEFAULT_ALLOC ;
336
- tb -> element_free = SH_DEFAULT_FREE ;
337
- tb -> element_args = ctx ;
338
- }
339
- else
340
- {
341
- tb -> element_alloc = allocfunc ;
342
- tb -> element_free = freefunc ;
343
-
344
- tb -> element_args = args ;
345
- }
346
-
347
- tb -> data = tb -> element_alloc (sizeof (SH_ELEMENT_TYPE ) * tb -> size ,
348
- tb -> element_args );
325
+ tb -> data = SH_ALLOCATE (tb , sizeof (SH_ELEMENT_TYPE ) * tb -> size );
349
326
350
327
return tb ;
351
328
}
@@ -354,7 +331,7 @@ SH_CREATE(MemoryContext ctx, uint32 nelements, simplehash_allocate allocfunc,
354
331
SH_SCOPE void
355
332
SH_DESTROY (SH_TYPE * tb )
356
333
{
357
- tb -> element_free (tb -> data , tb -> element_args );
334
+ SH_FREE (tb , tb -> data );
358
335
pfree (tb );
359
336
}
360
337
@@ -382,8 +359,7 @@ SH_GROW(SH_TYPE *tb, uint32 newsize)
382
359
/* compute parameters for new table */
383
360
SH_COMPUTE_PARAMETERS (tb , newsize );
384
361
385
- tb -> data = tb -> element_alloc (sizeof (SH_ELEMENT_TYPE ) * tb -> size ,
386
- tb -> element_args );
362
+ tb -> data = SH_ALLOCATE (tb , sizeof (SH_ELEMENT_TYPE ) * tb -> size );
387
363
388
364
newdata = tb -> data ;
389
365
@@ -469,7 +445,7 @@ SH_GROW(SH_TYPE *tb, uint32 newsize)
469
445
}
470
446
}
471
447
472
- tb -> element_free ( olddata , tb -> element_args );
448
+ SH_FREE ( tb , olddata );
473
449
}
474
450
475
451
/*
@@ -888,6 +864,7 @@ SH_STAT(SH_TYPE *tb)
888
864
#undef SH_DEFINE
889
865
#undef SH_GET_HASH
890
866
#undef SH_STORE_HASH
867
+ #undef SH_USE_NONDEFAULT_ALLOCATOR
891
868
892
869
/* undefine locally declared macros */
893
870
#undef SH_MAKE_PREFIX
@@ -914,6 +891,8 @@ SH_STAT(SH_TYPE *tb)
914
891
#undef SH_START_ITERATE
915
892
#undef SH_START_ITERATE_AT
916
893
#undef SH_ITERATE
894
+ #undef SH_ALLOCATE
895
+ #undef SH_FREE
917
896
#undef SH_STAT
918
897
919
898
/* internal function names */
0 commit comments