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