PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
memnodes.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * memnodes.h
4 * POSTGRES memory context node definitions.
5 *
6 *
7 * Portions Copyright (c) 1996-2024, 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 */
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 */
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.
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 MemoryContextData.
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
54typedef void (*MemoryStatsPrintFunc) (MemoryContext context, void *passthru,
55 const char *stats_string,
56 bool print_to_stderr);
57
59{
60 /*
61 * Function to handle memory allocation requests of 'size' to allocate
62 * memory into the given 'context'. The function must handle flags
63 * MCXT_ALLOC_HUGE and MCXT_ALLOC_NO_OOM. MCXT_ALLOC_ZERO is handled by
64 * the calling function.
65 */
66 void *(*alloc) (MemoryContext context, Size size, int flags);
67
68 /* call this free_p in case someone #define's free() */
69 void (*free_p) (void *pointer);
70
71 /*
72 * Function to handle a size change request for an existing allocation.
73 * The implementation must handle flags MCXT_ALLOC_HUGE and
74 * MCXT_ALLOC_NO_OOM. MCXT_ALLOC_ZERO is handled by the calling function.
75 */
76 void *(*realloc) (void *pointer, Size size, int flags);
77
78 /*
79 * Invalidate all previous allocations in the given memory context and
80 * prepare the context for a new set of allocations. Implementations may
81 * optionally free() excess memory back to the OS during this time.
82 */
84
85 /* Free all memory consumed by the given MemoryContext. */
87
88 /* Return the MemoryContext that the given pointer belongs to. */
89 MemoryContext (*get_chunk_context) (void *pointer);
90
91 /*
92 * Return the number of bytes consumed by the given pointer within its
93 * memory context, including the overhead of alignment and chunk headers.
94 */
95 Size (*get_chunk_space) (void *pointer);
96
97 /*
98 * Return true if the given MemoryContext has not had any allocations
99 * since it was created or last reset.
100 */
103 MemoryStatsPrintFunc printfunc, void *passthru,
104 MemoryContextCounters *totals,
105 bool print_to_stderr);
106#ifdef MEMORY_CONTEXT_CHECKING
107
108 /*
109 * Perform validation checks on the given context and raise any discovered
110 * anomalies as WARNINGs.
111 */
112 void (*check) (MemoryContext context);
113#endif
115
116
117typedef struct MemoryContextData
118{
119 pg_node_attr(abstract) /* there are no nodes of this type */
120
121 NodeTag type; /* identifies exact kind of context */
122 /* these two fields are placed here to minimize alignment wastage: */
123 bool isReset; /* T = no space alloced since last reset */
124 bool allowInCritSection; /* allow palloc in critical section */
125 Size mem_allocated; /* track memory allocated for this context */
126 const MemoryContextMethods *methods; /* virtual function table */
127 MemoryContext parent; /* NULL if no parent (toplevel context) */
128 MemoryContext firstchild; /* head of linked list of children */
129 MemoryContext prevchild; /* previous child of same parent */
130 MemoryContext nextchild; /* next child of same parent */
131 const char *name; /* context name */
132 const char *ident; /* context ID if any */
133 MemoryContextCallback *reset_cbs; /* list of reset/delete callbacks */
135
136/* utils/palloc.h contains typedef struct MemoryContextData *MemoryContext */
137
138
139/*
140 * MemoryContextIsValid
141 * True iff memory context is valid.
142 *
143 * Add new context types to the set accepted by this macro.
144 */
145#define MemoryContextIsValid(context) \
146 ((context) != NULL && \
147 (IsA((context), AllocSetContext) || \
148 IsA((context), SlabContext) || \
149 IsA((context), GenerationContext) || \
150 IsA((context), BumpContext)))
151
152#endif /* MEMNODES_H */
size_t Size
Definition: c.h:559
void(* MemoryStatsPrintFunc)(MemoryContext context, void *passthru, const char *stats_string, bool print_to_stderr)
Definition: memnodes.h:54
struct MemoryContextMethods MemoryContextMethods
struct MemoryContextCounters MemoryContextCounters
struct MemoryContextData MemoryContextData
NodeTag
Definition: nodes.h:27
struct MemoryContextData * MemoryContext
Definition: palloc.h:36
tree context
Definition: radixtree.h:1837
static pg_noinline void Size size
Definition: slab.c:607
MemoryContext prevchild
Definition: memnodes.h:129
MemoryContext firstchild
Definition: memnodes.h:128
bool allowInCritSection
Definition: memnodes.h:124
const char * ident
Definition: memnodes.h:132
MemoryContext parent
Definition: memnodes.h:127
MemoryContextCallback * reset_cbs
Definition: memnodes.h:133
const MemoryContextMethods * methods
Definition: memnodes.h:126
MemoryContext nextchild
Definition: memnodes.h:130
pg_node_attr(abstract) NodeTag type
const char * name
Definition: memnodes.h:131
void(* delete_context)(MemoryContext context)
Definition: memnodes.h:86
void(* stats)(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
Definition: memnodes.h:102
void(* free_p)(void *pointer)
Definition: memnodes.h:69
bool(* is_empty)(MemoryContext context)
Definition: memnodes.h:101
void(* reset)(MemoryContext context)
Definition: memnodes.h:83
MemoryContext(* get_chunk_context)(void *pointer)
Definition: memnodes.h:89
Size(* get_chunk_space)(void *pointer)
Definition: memnodes.h:95
const char * type