1
/*-------------------------------------------------------------------------
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.
10
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
11
* Portions Copyright (c) 1994, Regents of the University of California
13
* src/include/utils/memutils.h
15
*-------------------------------------------------------------------------
20
#include "nodes/memnodes.h"
25
* Quasi-arbitrary limit on size of allocations.
28
* There is no guarantee that allocations smaller than MaxAllocSize
29
* will succeed. Allocation requests larger than MaxAllocSize will
30
* be summarily denied.
32
* XXX This is deliberately chosen to correspond to the limiting size
33
* of varlena objects under TOAST. See VARSIZE_4B() and related macros
34
* in postgres.h. Many datatypes assume that any allocatable size can
35
* be represented in a varlena header.
37
* XXX Also, various places in aset.c assume they can compute twice an
38
* allocation's size without overflow, so beware of raising this.
40
#define MaxAllocSize ((Size) 0x3fffffff) /* 1 gigabyte - 1 */
42
#define AllocSizeIsValid(size) ((Size) (size) <= MaxAllocSize)
45
* All chunks allocated by any memory context manager are required to be
46
* preceded by a StandardChunkHeader at a spacing of STANDARDCHUNKHEADERSIZE.
47
* A currently-allocated chunk must contain a backpointer to its owning
48
* context as well as the allocated size of the chunk. The backpointer is
49
* used by pfree() and repalloc() to find the context to call. The allocated
50
* size is not absolutely essential, but it's expected to be needed by any
51
* reasonable implementation.
53
typedef struct StandardChunkHeader
55
MemoryContext context; /* owning context */
56
Size size; /* size of data space allocated in chunk */
57
#ifdef MEMORY_CONTEXT_CHECKING
58
/* when debugging memory usage, also store actual requested size */
61
} StandardChunkHeader;
63
#define STANDARDCHUNKHEADERSIZE MAXALIGN(sizeof(StandardChunkHeader))
67
* Standard top-level memory contexts.
69
* Only TopMemoryContext and ErrorContext are initialized by
70
* MemoryContextInit() itself.
72
extern PGDLLIMPORT MemoryContext TopMemoryContext;
73
extern PGDLLIMPORT MemoryContext ErrorContext;
74
extern PGDLLIMPORT MemoryContext PostmasterContext;
75
extern PGDLLIMPORT MemoryContext CacheMemoryContext;
76
extern PGDLLIMPORT MemoryContext MessageContext;
77
extern PGDLLIMPORT MemoryContext TopTransactionContext;
78
extern PGDLLIMPORT MemoryContext CurTransactionContext;
80
/* This is a transient link to the active portal's memory context: */
81
extern PGDLLIMPORT MemoryContext PortalContext;
85
* Memory-context-type-independent functions in mcxt.c
87
extern void MemoryContextInit(void);
88
extern void MemoryContextReset(MemoryContext context);
89
extern void MemoryContextDelete(MemoryContext context);
90
extern void MemoryContextResetChildren(MemoryContext context);
91
extern void MemoryContextDeleteChildren(MemoryContext context);
92
extern void MemoryContextResetAndDeleteChildren(MemoryContext context);
93
extern Size GetMemoryChunkSpace(void *pointer);
94
extern MemoryContext GetMemoryChunkContext(void *pointer);
95
extern bool MemoryContextIsEmpty(MemoryContext context);
96
extern void MemoryContextStats(MemoryContext context);
98
#ifdef MEMORY_CONTEXT_CHECKING
99
extern void MemoryContextCheck(MemoryContext context);
101
extern bool MemoryContextContains(MemoryContext context, void *pointer);
104
* This routine handles the context-type-independent part of memory
105
* context creation. It's intended to be called from context-type-
106
* specific creation routines, and noplace else.
108
extern MemoryContext MemoryContextCreate(NodeTag tag, Size size,
109
MemoryContextMethods *methods,
110
MemoryContext parent,
115
* Memory-context-type-specific functions
119
extern MemoryContext AllocSetContextCreate(MemoryContext parent,
126
* Recommended default alloc parameters, suitable for "ordinary" contexts
127
* that might hold quite a lot of data.
129
#define ALLOCSET_DEFAULT_MINSIZE 0
130
#define ALLOCSET_DEFAULT_INITSIZE (8 * 1024)
131
#define ALLOCSET_DEFAULT_MAXSIZE (8 * 1024 * 1024)
134
* Recommended alloc parameters for "small" contexts that are not expected
135
* to contain much data (for example, a context to contain a query plan).
137
#define ALLOCSET_SMALL_MINSIZE 0
138
#define ALLOCSET_SMALL_INITSIZE (1 * 1024)
139
#define ALLOCSET_SMALL_MAXSIZE (8 * 1024)
141
#endif /* MEMUTILS_H */