1 /*-------------------------------------------------------------------------
2  *
3  * memnodes.h
4  *	  POSTGRES memory context node definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/nodes/memnodes.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef MEMNODES_H
15 #define MEMNODES_H
16 
17 #include "parser/nodes.h"
18 /*
19  * MemoryContextCounters
20  *		Summarization state for MemoryContextStats collection.
21  *
22  * The set of counters in this struct is biased towards AllocSet; if we ever
23  * add any context types that are based on fundamentally different approaches,
24  * we might need more or different counters here.  A possible API spec then
25  * would be to print only nonzero counters, but for now we just summarize in
26  * the format historically used by AllocSet.
27  */
28 typedef struct MemoryContextCounters
29 {
30 	Size		nblocks;		/* Total number of malloc blocks */
31 	Size		freechunks;		/* Total number of free chunks */
32 	Size		totalspace;		/* Total bytes requested from malloc */
33 	Size		freespace;		/* The unused portion of totalspace */
34 } MemoryContextCounters;
35 
36 /*
37  * MemoryContext
38  *		A logical context in which memory allocations occur.
39  *
40  * MemoryContext itself is an abstract type that can have multiple
41  * implementations, though for now we have only AllocSetContext.
42  * The function pointers in MemoryContextMethods define one specific
43  * implementation of MemoryContext --- they are a virtual function table
44  * in C++ terms.
45  *
46  * Node types that are actual implementations of memory contexts must
47  * begin with the same fields as MemoryContext.
48  *
49  * Note: for largely historical reasons, typedef MemoryContext is a pointer
50  * to the context struct rather than the struct type itself.
51  */
52 
53 typedef struct MemoryContextMethods
54 {
55 	void	   *(*alloc) (MemoryContext context, Size size);
56 	/* call this free_p in case someone #define's free() */
57 	void		(*free_p) (MemoryContext context, void *pointer);
58 	void	   *(*realloc) (MemoryContext context, void *pointer, Size size);
59 	void		(*init) (MemoryContext context);
60 	void		(*reset) (MemoryContext context);
61 	void		(*delete_context) (MemoryContext context);
62 	Size		(*get_chunk_space) (MemoryContext context, void *pointer);
63 	bool		(*is_empty) (MemoryContext context);
64 	void		(*stats) (MemoryContext context, int level, bool print,
65 						  MemoryContextCounters *totals);
66 #ifdef MEMORY_CONTEXT_CHECKING
67 	void		(*check) (MemoryContext context);
68 #endif
69 } MemoryContextMethods;
70 
71 
72 typedef struct MemoryContextData
73 {
74 	NodeTag		type;			/* identifies exact kind of context */
75 	/* these two fields are placed here to minimize alignment wastage: */
76 	bool		isReset;		/* T = no space alloced since last reset */
77 	bool		allowInCritSection; /* allow palloc in critical section */
78 	MemoryContextMethods *methods;	/* virtual function table */
79 	MemoryContext parent;		/* NULL if no parent (toplevel context) */
80 	MemoryContext firstchild;	/* head of linked list of children */
81 	MemoryContext prevchild;	/* previous child of same parent */
82 	MemoryContext nextchild;	/* next child of same parent */
83 	char	   *name;			/* context name (just for debugging) */
84 	MemoryContextCallback *reset_cbs;	/* list of reset/delete callbacks */
85 } MemoryContextData;
86 
87 /* utils/palloc.h contains typedef struct MemoryContextData *MemoryContext */
88 
89 
90 /*
91  * MemoryContextIsValid
92  *		True iff memory context is valid.
93  *
94  * Add new context types to the set accepted by this macro.
95  */
96 #define MemoryContextIsValid(context) \
97 	((context) != NULL && \
98 	 (IsA((context), AllocSetContext) || IsA((context), SlabContext)))
99 
100 #endif							/* MEMNODES_H */
101