0001-simplehash-Allow-use-of-simplehash-without-MemoryCon.patch

application/octet-stream

Filename: 0001-simplehash-Allow-use-of-simplehash-without-MemoryCon.patch
Type: application/octet-stream
Part: 0
Message: allowing broader use of simplehash

Patch

Format: format-patch
Series: patch 0001
Subject: simplehash: Allow use of simplehash without MemoryContext.
File+
src/include/lib/simplehash.h 23 0
From a821efad87a4ea1799299bdc6d04c7b8060fc4cc Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Tue, 10 Dec 2019 12:02:24 -0500
Subject: [PATCH 1/2] 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.
---
 src/include/lib/simplehash.h | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/src/include/lib/simplehash.h b/src/include/lib/simplehash.h
index d827270a6a..403837a407 100644
--- a/src/include/lib/simplehash.h
+++ b/src/include/lib/simplehash.h
@@ -23,6 +23,8 @@
  *	  - SH_DEFINE - if defined function definitions are generated
  *	  - SH_SCOPE - in which scope (e.g. extern, static inline) do function
  *		declarations reside
+ *	  - SH_RAW_ALLOCATOR - if defined, memory contexts are not used; instead,
+ *	    use this to allocate bytes
  *	  - SH_USE_NONDEFAULT_ALLOCATOR - if defined no element allocator functions
  *		are defined, so you can supply your own
  *	  The following parameters are only relevant when SH_DEFINE is defined:
@@ -121,8 +123,10 @@ typedef struct SH_TYPE
 	/* hash buckets */
 	SH_ELEMENT_TYPE *data;
 
+#ifndef SH_RAW_ALLOCATOR
 	/* memory context to use for allocations */
 	MemoryContext ctx;
+#endif
 
 	/* user defined data, useful for callbacks */
 	void	   *private_data;
@@ -142,8 +146,12 @@ typedef struct SH_ITERATOR
 }			SH_ITERATOR;
 
 /* externally visible function prototypes */
+#ifdef SH_RAW_ALLOCATOR
+SH_SCOPE	SH_TYPE *SH_CREATE(uint32 nelements, void *private_data);
+#else
 SH_SCOPE	SH_TYPE *SH_CREATE(MemoryContext ctx, uint32 nelements,
 							   void *private_data);
+#endif
 SH_SCOPE void SH_DESTROY(SH_TYPE * tb);
 SH_SCOPE void SH_RESET(SH_TYPE * tb);
 SH_SCOPE void SH_GROW(SH_TYPE * tb, uint32 newsize);
@@ -165,7 +173,9 @@ SH_SCOPE void SH_STAT(SH_TYPE * tb);
 /* generate implementation of the hash table */
 #ifdef SH_DEFINE
 
+#ifndef SH_RAW_ALLOCATOR
 #include "utils/memutils.h"
+#endif
 
 /* max data array size,we allow up to PG_UINT32_MAX buckets, including 0 */
 #define SH_MAX_SIZE (((uint64) PG_UINT32_MAX) + 1)
@@ -328,8 +338,12 @@ static inline void SH_FREE(SH_TYPE * type, void *pointer);
 static inline void *
 SH_ALLOCATE(SH_TYPE * type, Size size)
 {
+#ifdef SH_RAW_ALLOCATOR
+	return SH_RAW_ALLOCATOR(size);
+#else
 	return MemoryContextAllocExtended(type->ctx, size,
 									  MCXT_ALLOC_HUGE | MCXT_ALLOC_ZERO);
+#endif
 }
 
 /* default memory free function */
@@ -350,14 +364,23 @@ SH_FREE(SH_TYPE * type, void *pointer)
  * Memory other than for the array of elements will still be allocated from
  * the passed-in context.
  */
+#ifdef SH_RAW_ALLOCATOR
+SH_SCOPE	SH_TYPE *
+SH_CREATE(uint32 nelements, void *private_data)
+#else
 SH_SCOPE	SH_TYPE *
 SH_CREATE(MemoryContext ctx, uint32 nelements, void *private_data)
+#endif
 {
 	SH_TYPE    *tb;
 	uint64		size;
 
+#ifdef SH_RAW_ALLOCATOR
+	tb = SH_RAW_ALLOCATOR(sizeof(SH_TYPE));
+#else
 	tb = MemoryContextAllocZero(ctx, sizeof(SH_TYPE));
 	tb->ctx = ctx;
+#endif
 	tb->private_data = private_data;
 
 	/* increase nelements by fillfactor, want to store nelements elements */
-- 
2.17.2 (Apple Git-113)