PostgreSQL Source Code  git master
memutils.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * memutils.h
4  * This file contains declarations for memory allocation utility
5  * functions. These are functions that are not quite widely used
6  * enough to justify going in utils/palloc.h, but are still part
7  * of the API of the memory management subsystem.
8  *
9  *
10  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1994, Regents of the University of California
12  *
13  * src/include/utils/memutils.h
14  *
15  *-------------------------------------------------------------------------
16  */
17 #ifndef MEMUTILS_H
18 #define MEMUTILS_H
19 
20 #include "nodes/memnodes.h"
21 
22 
23 /*
24  * MaxAllocSize, MaxAllocHugeSize
25  * Quasi-arbitrary limits on size of allocations.
26  *
27  * Note:
28  * There is no guarantee that smaller allocations will succeed, but
29  * larger requests will be summarily denied.
30  *
31  * palloc() enforces MaxAllocSize, chosen to correspond to the limiting size
32  * of varlena objects under TOAST. See VARSIZE_4B() and related macros in
33  * postgres.h. Many datatypes assume that any allocatable size can be
34  * represented in a varlena header. This limit also permits a caller to use
35  * an "int" variable for an index into or length of an allocation. Callers
36  * careful to avoid these hazards can access the higher limit with
37  * MemoryContextAllocHuge(). Both limits permit code to assume that it may
38  * compute twice an allocation's size without overflow.
39  */
40 #define MaxAllocSize ((Size) 0x3fffffff) /* 1 gigabyte - 1 */
41 
42 #define AllocSizeIsValid(size) ((Size) (size) <= MaxAllocSize)
43 
44 #define MaxAllocHugeSize (SIZE_MAX / 2)
45 
46 #define AllocHugeSizeIsValid(size) ((Size) (size) <= MaxAllocHugeSize)
47 
48 
49 /*
50  * Standard top-level memory contexts.
51  *
52  * Only TopMemoryContext and ErrorContext are initialized by
53  * MemoryContextInit() itself.
54  */
62 
63 /* This is a transient link to the active portal's memory context: */
65 
66 /* Backwards compatibility macro */
67 #define MemoryContextResetAndDeleteChildren(ctx) MemoryContextReset(ctx)
68 
69 
70 /*
71  * Memory-context-type-independent functions in mcxt.c
72  */
73 extern void MemoryContextInit(void);
74 extern void MemoryContextReset(MemoryContext context);
75 extern void MemoryContextDelete(MemoryContext context);
76 extern void MemoryContextResetOnly(MemoryContext context);
77 extern void MemoryContextResetChildren(MemoryContext context);
78 extern void MemoryContextDeleteChildren(MemoryContext context);
79 extern void MemoryContextSetParent(MemoryContext context,
80  MemoryContext new_parent);
81 extern Size GetMemoryChunkSpace(void *pointer);
83 extern bool MemoryContextIsEmpty(MemoryContext context);
84 extern void MemoryContextStats(MemoryContext context);
85 extern void MemoryContextStatsDetail(MemoryContext context, int max_children);
87  bool allow);
88 
89 #ifdef MEMORY_CONTEXT_CHECKING
90 extern void MemoryContextCheck(MemoryContext context);
91 #endif
92 extern bool MemoryContextContains(MemoryContext context, void *pointer);
93 
94 /*
95  * GetMemoryChunkContext
96  * Given a currently-allocated chunk, determine the context
97  * it belongs to.
98  *
99  * All chunks allocated by any memory context manager are required to be
100  * preceded by the corresponding MemoryContext stored, without padding, in the
101  * preceding sizeof(void*) bytes. A currently-allocated chunk must contain a
102  * backpointer to its owning context. The backpointer is used by pfree() and
103  * repalloc() to find the context to call.
104  */
105 #ifndef FRONTEND
106 static inline MemoryContext
107 GetMemoryChunkContext(void *pointer)
108 {
109  MemoryContext context;
110 
111  /*
112  * Try to detect bogus pointers handed to us, poorly though we can.
113  * Presumably, a pointer that isn't MAXALIGNED isn't pointing at an
114  * allocated chunk.
115  */
116  Assert(pointer != NULL);
117  Assert(pointer == (void *) MAXALIGN(pointer));
118 
119  /*
120  * OK, it's probably safe to look at the context.
121  */
122  context = *(MemoryContext *) (((char *) pointer) - sizeof(void *));
123 
125 
126  return context;
127 }
128 #endif
129 
130 /*
131  * This routine handles the context-type-independent part of memory
132  * context creation. It's intended to be called from context-type-
133  * specific creation routines, and noplace else.
134  */
135 extern void MemoryContextCreate(MemoryContext node,
136  NodeTag tag, Size size, Size nameoffset,
137  const MemoryContextMethods *methods,
138  MemoryContext parent,
139  const char *name,
140  int flags);
141 
142 
143 /*
144  * Memory-context-type-specific functions
145  */
146 
147 /* aset.c */
149  const char *name,
150  int flags,
151  Size minContextSize,
152  Size initBlockSize,
153  Size maxBlockSize);
154 
155 /*
156  * This backwards compatibility macro only works for constant context names,
157  * and you must specify block sizes with one of the abstraction macros below.
158  */
159 #ifdef HAVE__BUILTIN_CONSTANT_P
160 #define AllocSetContextCreate(parent, name, allocparams) \
161  (StaticAssertExpr(__builtin_constant_p(name), \
162  "Use AllocSetContextCreateExtended with MEMCONTEXT_COPY_NAME for non-constant context names"), \
163  AllocSetContextCreateExtended(parent, name, 0, allocparams))
164 #else
165 #define AllocSetContextCreate(parent, name, allocparams) \
166  AllocSetContextCreateExtended(parent, name, 0, allocparams)
167 #endif
168 
169 /* slab.c */
171  const char *name,
172  int flags,
173  Size blockSize,
174  Size chunkSize);
175 
176 /* generation.c */
178  const char *name,
179  int flags,
180  Size blockSize);
181 
182 /*
183  * Flag option bits for FooContextCreate functions.
184  * In future, some of these might be relevant to only some context types.
185  *
186  * COPY_NAME: FooContextCreate's name argument is not a constant string
187  */
188 #define MEMCONTEXT_COPY_NAME 0x0001 /* is passed name transient? */
189 
190 /*
191  * Recommended default alloc parameters, suitable for "ordinary" contexts
192  * that might hold quite a lot of data.
193  */
194 #define ALLOCSET_DEFAULT_MINSIZE 0
195 #define ALLOCSET_DEFAULT_INITSIZE (8 * 1024)
196 #define ALLOCSET_DEFAULT_MAXSIZE (8 * 1024 * 1024)
197 #define ALLOCSET_DEFAULT_SIZES \
198  ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE
199 
200 /*
201  * Recommended alloc parameters for "small" contexts that are never expected
202  * to contain much data (for example, a context to contain a query plan).
203  */
204 #define ALLOCSET_SMALL_MINSIZE 0
205 #define ALLOCSET_SMALL_INITSIZE (1 * 1024)
206 #define ALLOCSET_SMALL_MAXSIZE (8 * 1024)
207 #define ALLOCSET_SMALL_SIZES \
208  ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_SMALL_MAXSIZE
209 
210 /*
211  * Recommended alloc parameters for contexts that should start out small,
212  * but might sometimes grow big.
213  */
214 #define ALLOCSET_START_SMALL_SIZES \
215  ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE
216 
217 
218 /*
219  * Threshold above which a request in an AllocSet context is certain to be
220  * allocated separately (and thereby have constant allocation overhead).
221  * Few callers should be interested in this, but tuplesort/tuplestore need
222  * to know it.
223  */
224 #define ALLOCSET_SEPARATE_THRESHOLD 8192
225 
226 #define SLAB_DEFAULT_BLOCK_SIZE (8 * 1024)
227 #define SLAB_LARGE_BLOCK_SIZE (8 * 1024 * 1024)
228 
229 #endif /* MEMUTILS_H */
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:198
PGDLLIMPORT MemoryContext CacheMemoryContext
Definition: mcxt.c:46
bool MemoryContextIsEmpty(MemoryContext context)
Definition: mcxt.c:414
void MemoryContextAllowInCriticalSection(MemoryContext context, bool allow)
Definition: mcxt.c:374
MemoryContext GenerationContextCreate(MemoryContext parent, const char *name, int flags, Size blockSize)
Definition: generation.c:208
void MemoryContextDeleteChildren(MemoryContext context)
Definition: mcxt.c:236
void MemoryContextResetOnly(MemoryContext context)
Definition: mcxt.c:153
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:134
PGDLLIMPORT MemoryContext PortalContext
Definition: mcxt.c:52
PGDLLIMPORT MemoryContext PostmasterContext
Definition: mcxt.c:45
NodeTag
Definition: nodes.h:26
#define PGDLLIMPORT
Definition: c.h:1094
bool MemoryContextContains(MemoryContext context, void *pointer)
Definition: mcxt.c:566
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:437
void MemoryContextCreate(MemoryContext node, NodeTag tag, Size size, Size nameoffset, const MemoryContextMethods *methods, MemoryContext parent, const char *name, int flags)
Definition: mcxt.c:627
PGDLLIMPORT MemoryContext TopTransactionContext
Definition: mcxt.c:48
PGDLLIMPORT MemoryContext MessageContext
Definition: mcxt.c:47
MemoryContext MemoryContextGetParent(MemoryContext context)
Definition: mcxt.c:402
Size GetMemoryChunkSpace(void *pointer)
Definition: mcxt.c:390
void MemoryContextResetChildren(MemoryContext context)
Definition: mcxt.c:175
#define AssertArg(condition)
Definition: c.h:690
PGDLLIMPORT MemoryContext CurTransactionContext
Definition: mcxt.c:49
void MemoryContextStatsDetail(MemoryContext context, int max_children)
Definition: mcxt.c:449
MemoryContext SlabContextCreate(MemoryContext parent, const char *name, int flags, Size blockSize, Size chunkSize)
Definition: slab.c:190
#define MemoryContextIsValid(context)
Definition: memnodes.h:96
#define Assert(condition)
Definition: c.h:688
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:317
size_t Size
Definition: c.h:422
#define MAXALIGN(LEN)
Definition: c.h:641
MemoryContext AllocSetContextCreateExtended(MemoryContext parent, const char *name, int flags, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:394
const char * name
Definition: encode.c:521
PGDLLIMPORT MemoryContext TopMemoryContext
Definition: mcxt.c:43
PGDLLIMPORT MemoryContext ErrorContext
Definition: mcxt.c:44
void MemoryContextInit(void)
Definition: mcxt.c:89
static MemoryContext GetMemoryChunkContext(void *pointer)
Definition: memutils.h:107