summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Haas2020-01-24 22:01:59 +0000
committerRobert Haas2020-01-24 22:01:59 +0000
commit73600bfbcef11e79cfff27ce5f4300658f8695ab (patch)
treef95dfb6dc2ccf7c726aa7683ce0a4e027a602939
parent0ce38730ac72029f3f2c95ae80b44f5b9060cbcc (diff)
Teach MemoryContext infrastructure not to depend on Node.aset_frontend
There's no real reason why a MemoryContext's type needs to be a Node type. So use a separate enum instead. This makes memory contexts less dependent on backend-only infrastructure.
-rw-r--r--src/backend/utils/mmgr/aset.c4
-rw-r--r--src/backend/utils/mmgr/generation.c2
-rw-r--r--src/backend/utils/mmgr/mcxt.c4
-rw-r--r--src/backend/utils/mmgr/slab.c18
-rw-r--r--src/include/nodes/memnodes.h41
-rw-r--r--src/include/nodes/nodes.h8
-rw-r--r--src/include/utils/memutils.h2
7 files changed, 49 insertions, 30 deletions
diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c
index 5ac3811212..23ed57c4cf 100644
--- a/src/backend/utils/mmgr/aset.c
+++ b/src/backend/utils/mmgr/aset.c
@@ -459,7 +459,7 @@ AllocSetContextCreateInternal(MemoryContext parent,
/* Reinitialize its header, installing correct name and parent */
MemoryContextCreate((MemoryContext) set,
- T_AllocSetContext,
+ MemoryContext_AllocSet,
&AllocSetMethods,
parent,
name);
@@ -550,7 +550,7 @@ AllocSetContextCreateInternal(MemoryContext parent,
/* Finally, do the type-independent part of context creation */
MemoryContextCreate((MemoryContext) set,
- T_AllocSetContext,
+ MemoryContext_AllocSet,
&AllocSetMethods,
parent,
name);
diff --git a/src/backend/utils/mmgr/generation.c b/src/backend/utils/mmgr/generation.c
index b60a19f5a5..c3de3fd3d9 100644
--- a/src/backend/utils/mmgr/generation.c
+++ b/src/backend/utils/mmgr/generation.c
@@ -263,7 +263,7 @@ GenerationContextCreate(MemoryContext parent,
/* Finally, do the type-independent part of context creation */
MemoryContextCreate((MemoryContext) set,
- T_GenerationContext,
+ MemoryContext_Generation,
&GenerationMethods,
parent,
name);
diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c
index d52bd2c073..484b84d4d2 100644
--- a/src/backend/utils/mmgr/mcxt.c
+++ b/src/backend/utils/mmgr/mcxt.c
@@ -746,7 +746,7 @@ MemoryContextContains(MemoryContext context, void *pointer)
*/
void
MemoryContextCreate(MemoryContext node,
- NodeTag tag,
+ MemoryContextType type,
const MemoryContextMethods *methods,
MemoryContext parent,
const char *name)
@@ -755,7 +755,7 @@ MemoryContextCreate(MemoryContext node,
Assert(CritSectionCount == 0);
/* Initialize all standard fields of memory context header */
- node->type = tag;
+ node->type = type;
node->isReset = true;
node->methods = methods;
node->parent = parent;
diff --git a/src/backend/utils/mmgr/slab.c b/src/backend/utils/mmgr/slab.c
index db1dac3022..011577627f 100644
--- a/src/backend/utils/mmgr/slab.c
+++ b/src/backend/utils/mmgr/slab.c
@@ -260,7 +260,7 @@ SlabContextCreate(MemoryContext parent,
/* Finally, do the type-independent part of context creation */
MemoryContextCreate((MemoryContext) slab,
- T_SlabContext,
+ MemoryContext_Slab,
&SlabMethods,
parent,
name);
@@ -279,7 +279,7 @@ static void
SlabReset(MemoryContext context)
{
int i;
- SlabContext *slab = castNode(SlabContext, context);
+ SlabContext *slab = MemoryContextCast(Slab, context);
Assert(slab);
@@ -335,7 +335,7 @@ SlabDelete(MemoryContext context)
static void *
SlabAlloc(MemoryContext context, Size size)
{
- SlabContext *slab = castNode(SlabContext, context);
+ SlabContext *slab = MemoryContextCast(Slab, context);
SlabBlock *block;
SlabChunk *chunk;
int idx;
@@ -496,7 +496,7 @@ static void
SlabFree(MemoryContext context, void *pointer)
{
int idx;
- SlabContext *slab = castNode(SlabContext, context);
+ SlabContext *slab = MemoryContextCast(Slab, context);
SlabChunk *chunk = SlabPointerGetChunk(pointer);
SlabBlock *block = chunk->block;
@@ -585,7 +585,7 @@ SlabFree(MemoryContext context, void *pointer)
static void *
SlabRealloc(MemoryContext context, void *pointer, Size size)
{
- SlabContext *slab = castNode(SlabContext, context);
+ SlabContext *slab = MemoryContextCast(Slab, context);
Assert(slab);
@@ -605,7 +605,7 @@ SlabRealloc(MemoryContext context, void *pointer, Size size)
static Size
SlabGetChunkSpace(MemoryContext context, void *pointer)
{
- SlabContext *slab = castNode(SlabContext, context);
+ SlabContext *slab = MemoryContextCast(Slab, context);
Assert(slab);
@@ -619,7 +619,7 @@ SlabGetChunkSpace(MemoryContext context, void *pointer)
static bool
SlabIsEmpty(MemoryContext context)
{
- SlabContext *slab = castNode(SlabContext, context);
+ SlabContext *slab = MemoryContextCast(Slab, context);
Assert(slab);
@@ -639,7 +639,7 @@ SlabStats(MemoryContext context,
MemoryStatsPrintFunc printfunc, void *passthru,
MemoryContextCounters *totals)
{
- SlabContext *slab = castNode(SlabContext, context);
+ SlabContext *slab = MemoryContextCast(Slab, context);
Size nblocks = 0;
Size freechunks = 0;
Size totalspace;
@@ -699,7 +699,7 @@ static void
SlabCheck(MemoryContext context)
{
int i;
- SlabContext *slab = castNode(SlabContext, context);
+ SlabContext *slab = MemoryContextCast(Slab, context);
const char *name = slab->header.name;
char *freechunks;
diff --git a/src/include/nodes/memnodes.h b/src/include/nodes/memnodes.h
index 854bd5d225..37d27032c2 100644
--- a/src/include/nodes/memnodes.h
+++ b/src/include/nodes/memnodes.h
@@ -14,7 +14,15 @@
#ifndef MEMNODES_H
#define MEMNODES_H
-#include "nodes/nodes.h"
+/*
+ * List of valid memory context types.
+ */
+typedef enum
+{
+ MemoryContext_AllocSet,
+ MemoryContext_Generation,
+ MemoryContext_Slab
+} MemoryContextType;
/*
* MemoryContextCounters
@@ -75,7 +83,7 @@ typedef struct MemoryContextMethods
typedef struct MemoryContextData
{
- NodeTag type; /* identifies exact kind of context */
+ char type; /* MemoryContextType for this context */
/* these two fields are placed here to minimize alignment wastage: */
bool isReset; /* T = no space alloced since last reset */
bool allowInCritSection; /* allow palloc in critical section */
@@ -99,10 +107,29 @@ typedef struct MemoryContextData
*
* Add new context types to the set accepted by this macro.
*/
-#define MemoryContextIsValid(context) \
- ((context) != NULL && \
- (IsA((context), AllocSetContext) || \
- IsA((context), SlabContext) || \
- IsA((context), GenerationContext)))
+static inline bool
+MemoryContextIsValid(MemoryContext context)
+{
+ return context != NULL &&
+ (context->type == MemoryContext_AllocSet ||
+ context->type == MemoryContext_Slab ||
+ context->type == MemoryContext_Generation);
+}
+
+/*
+ * MemoryContextCast
+ * Cast a MemoryContext to a context of a given type, checking the type.
+ *
+ * NB: The static inline function avoids multiple evaluation.
+ */
+static inline MemoryContext
+MemoryContextCastImpl(MemoryContextType type, MemoryContext context)
+{
+ Assert(context == NULL || context->type == type);
+ return context;
+}
+#define MemoryContextCast(_type_, context) \
+ ((_type_##Context *) \
+ MemoryContextCastImpl(MemoryContext_##_type_, context))
#endif /* MEMNODES_H */
diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h
index bce2d59b0d..d4bba8a80c 100644
--- a/src/include/nodes/nodes.h
+++ b/src/include/nodes/nodes.h
@@ -274,14 +274,6 @@ typedef enum NodeTag
T_StatisticExtInfo,
/*
- * TAGS FOR MEMORY NODES (memnodes.h)
- */
- T_MemoryContext,
- T_AllocSetContext,
- T_SlabContext,
- T_GenerationContext,
-
- /*
* TAGS FOR VALUE NODES (value.h)
*/
T_Value,
diff --git a/src/include/utils/memutils.h b/src/include/utils/memutils.h
index 106c83da45..ad5d1276ac 100644
--- a/src/include/utils/memutils.h
+++ b/src/include/utils/memutils.h
@@ -139,7 +139,7 @@ GetMemoryChunkContext(void *pointer)
* specific creation routines, and noplace else.
*/
extern void MemoryContextCreate(MemoryContext node,
- NodeTag tag,
+ MemoryContextType type,
const MemoryContextMethods *methods,
MemoryContext parent,
const char *name);