/* Reinitialize its header, installing correct name and parent */
MemoryContextCreate((MemoryContext) set,
- T_AllocSetContext,
+ MemoryContext_AllocSet,
&AllocSetMethods,
parent,
name);
/* Finally, do the type-independent part of context creation */
MemoryContextCreate((MemoryContext) set,
- T_AllocSetContext,
+ MemoryContext_AllocSet,
&AllocSetMethods,
parent,
name);
/* Finally, do the type-independent part of context creation */
MemoryContextCreate((MemoryContext) set,
- T_GenerationContext,
+ MemoryContext_Generation,
&GenerationMethods,
parent,
name);
*/
void
MemoryContextCreate(MemoryContext node,
- NodeTag tag,
+ MemoryContextType type,
const MemoryContextMethods *methods,
MemoryContext parent,
const char *name)
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;
/* Finally, do the type-independent part of context creation */
MemoryContextCreate((MemoryContext) slab,
- T_SlabContext,
+ MemoryContext_Slab,
&SlabMethods,
parent,
name);
SlabReset(MemoryContext context)
{
int i;
- SlabContext *slab = castNode(SlabContext, context);
+ SlabContext *slab = MemoryContextCast(Slab, context);
Assert(slab);
static void *
SlabAlloc(MemoryContext context, Size size)
{
- SlabContext *slab = castNode(SlabContext, context);
+ SlabContext *slab = MemoryContextCast(Slab, context);
SlabBlock *block;
SlabChunk *chunk;
int idx;
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;
static void *
SlabRealloc(MemoryContext context, void *pointer, Size size)
{
- SlabContext *slab = castNode(SlabContext, context);
+ SlabContext *slab = MemoryContextCast(Slab, context);
Assert(slab);
static Size
SlabGetChunkSpace(MemoryContext context, void *pointer)
{
- SlabContext *slab = castNode(SlabContext, context);
+ SlabContext *slab = MemoryContextCast(Slab, context);
Assert(slab);
static bool
SlabIsEmpty(MemoryContext context)
{
- SlabContext *slab = castNode(SlabContext, context);
+ SlabContext *slab = MemoryContextCast(Slab, context);
Assert(slab);
MemoryStatsPrintFunc printfunc, void *passthru,
MemoryContextCounters *totals)
{
- SlabContext *slab = castNode(SlabContext, context);
+ SlabContext *slab = MemoryContextCast(Slab, context);
Size nblocks = 0;
Size freechunks = 0;
Size totalspace;
SlabCheck(MemoryContext context)
{
int i;
- SlabContext *slab = castNode(SlabContext, context);
+ SlabContext *slab = MemoryContextCast(Slab, context);
const char *name = slab->header.name;
char *freechunks;
#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
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 */
*
* 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 */
T_GroupingSetData,
T_StatisticExtInfo,
- /*
- * TAGS FOR MEMORY NODES (memnodes.h)
- */
- T_MemoryContext,
- T_AllocSetContext,
- T_SlabContext,
- T_GenerationContext,
-
/*
* TAGS FOR VALUE NODES (value.h)
*/
* specific creation routines, and noplace else.
*/
extern void MemoryContextCreate(MemoryContext node,
- NodeTag tag,
+ MemoryContextType type,
const MemoryContextMethods *methods,
MemoryContext parent,
const char *name);