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-2005, PostgreSQL Global Development Group
11
* Portions Copyright (c) 1994, Regents of the University of California
13
* $PostgreSQL: pgsql/src/include/utils/memutils.h,v 1.59 2004-12-31 22:03:46 pgsql Exp $
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 VARATT_MASK_SIZE in postgres.h.
35
* XXX Also, various places in aset.c assume they can compute twice an
36
* allocation's size without overflow, so beware of raising this.
38
#define MaxAllocSize ((Size) 0x3fffffff) /* 1 gigabyte - 1 */
40
#define AllocSizeIsValid(size) ((Size) (size) <= MaxAllocSize)
43
* All chunks allocated by any memory context manager are required to be
44
* preceded by a StandardChunkHeader at a spacing of STANDARDCHUNKHEADERSIZE.
45
* A currently-allocated chunk must contain a backpointer to its owning
46
* context as well as the allocated size of the chunk. The backpointer is
47
* used by pfree() and repalloc() to find the context to call. The allocated
48
* size is not absolutely essential, but it's expected to be needed by any
49
* reasonable implementation.
51
typedef struct StandardChunkHeader
53
MemoryContext context; /* owning context */
54
Size size; /* size of data space allocated in chunk */
55
#ifdef MEMORY_CONTEXT_CHECKING
56
/* when debugging memory usage, also store actual requested size */
59
} StandardChunkHeader;
61
#define STANDARDCHUNKHEADERSIZE MAXALIGN(sizeof(StandardChunkHeader))
65
* Standard top-level memory contexts.
67
* Only TopMemoryContext and ErrorContext are initialized by
68
* MemoryContextInit() itself.
70
extern DLLIMPORT MemoryContext TopMemoryContext;
71
extern DLLIMPORT MemoryContext ErrorContext;
72
extern DLLIMPORT MemoryContext PostmasterContext;
73
extern DLLIMPORT MemoryContext CacheMemoryContext;
74
extern DLLIMPORT MemoryContext MessageContext;
75
extern DLLIMPORT MemoryContext TopTransactionContext;
76
extern DLLIMPORT MemoryContext CurTransactionContext;
78
/* These two are transient links to contexts owned by other objects: */
79
extern DLLIMPORT MemoryContext QueryContext;
80
extern DLLIMPORT MemoryContext PortalContext;
84
* Memory-context-type-independent functions in mcxt.c
86
extern void MemoryContextInit(void);
87
extern void MemoryContextReset(MemoryContext context);
88
extern void MemoryContextDelete(MemoryContext context);
89
extern void MemoryContextResetChildren(MemoryContext context);
90
extern void MemoryContextDeleteChildren(MemoryContext context);
91
extern void MemoryContextResetAndDeleteChildren(MemoryContext context);
92
extern Size GetMemoryChunkSpace(void *pointer);
93
extern MemoryContext GetMemoryChunkContext(void *pointer);
94
extern bool MemoryContextIsEmpty(MemoryContext context);
95
extern void MemoryContextStats(MemoryContext context);
97
#ifdef MEMORY_CONTEXT_CHECKING
98
extern void MemoryContextCheck(MemoryContext context);
100
extern bool MemoryContextContains(MemoryContext context, void *pointer);
103
* This routine handles the context-type-independent part of memory
104
* context creation. It's intended to be called from context-type-
105
* specific creation routines, and noplace else.
107
extern MemoryContext MemoryContextCreate(NodeTag tag, Size size,
108
MemoryContextMethods *methods,
109
MemoryContext parent,
114
* Memory-context-type-specific functions
118
extern MemoryContext AllocSetContextCreate(MemoryContext parent,
125
* Recommended default alloc parameters, suitable for "ordinary" contexts
126
* that might hold quite a lot of data.
128
#define ALLOCSET_DEFAULT_MINSIZE 0
129
#define ALLOCSET_DEFAULT_INITSIZE (8 * 1024)
130
#define ALLOCSET_DEFAULT_MAXSIZE (8 * 1024 * 1024)
133
* Recommended alloc parameters for "small" contexts that are not expected
134
* to contain much data (for example, a context to contain a query plan).
136
#define ALLOCSET_SMALL_MINSIZE 0
137
#define ALLOCSET_SMALL_INITSIZE (1 * 1024)
138
#define ALLOCSET_SMALL_MAXSIZE (8 * 1024)
140
#endif /* MEMUTILS_H */