Teach MemoryContext infrastructure not to depend on Node. aset_frontend
authorRobert Haas <[email protected]>
Fri, 24 Jan 2020 22:01:59 +0000 (14:01 -0800)
committerRobert Haas <[email protected]>
Fri, 24 Jan 2020 22:01:59 +0000 (14:01 -0800)
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.

src/backend/utils/mmgr/aset.c
src/backend/utils/mmgr/generation.c
src/backend/utils/mmgr/mcxt.c
src/backend/utils/mmgr/slab.c
src/include/nodes/memnodes.h
src/include/nodes/nodes.h
src/include/utils/memutils.h

index 5ac381121273eb52e8328c90225ea823242a064d..23ed57c4cf24b4b266d920dba74d8bea1f19ef9f 100644 (file)
@@ -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);
index b60a19f5a5f67be20f586680766211a3b0af5375..c3de3fd3d9b3b927413189dbf9d3cebd2d7a0c67 100644 (file)
@@ -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);
index d52bd2c073ce0af6d30463f438d6a56d670ba869..484b84d4d243a3c3c719ad623a122c5776e9c05e 100644 (file)
@@ -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;
index db1dac30222c1fd2b819c5d005e1b46854b4b3e6..011577627fc0742e3a7742a3d498cacf5eb06828 100644 (file)
@@ -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;
 
index 854bd5d22568c800ff250f17db5aa786c20c213b..37d27032c224263ac8b6a183f07cb4c830960097 100644 (file)
 #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 */
index bce2d59b0dbcf871289e396f66c9e2c5295f2a1a..d4bba8a80ce3353fe84e309ee73336f8643d37dd 100644 (file)
@@ -273,14 +273,6 @@ typedef enum NodeTag
        T_GroupingSetData,
        T_StatisticExtInfo,
 
-       /*
-        * TAGS FOR MEMORY NODES (memnodes.h)
-        */
-       T_MemoryContext,
-       T_AllocSetContext,
-       T_SlabContext,
-       T_GenerationContext,
-
        /*
         * TAGS FOR VALUE NODES (value.h)
         */
index 106c83da45fe176ef66e099b90b2896336a063d1..ad5d1276ac234bb875c5cf4f886e7aa9a82f8098 100644 (file)
@@ -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);