memutils.h 4.5 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * memutils.h
4 5 6 7
 *	  This file contains declarations for memory allocation utility
 *	  functions.  These are functions that are not quite widely used
 *	  enough to justify going in utils/palloc.h, but are still part
 *	  of the API of the memory management subsystem.
8 9
 *
 *
B
Bruce Momjian 已提交
10
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
11
 * Portions Copyright (c) 1994, Regents of the University of California
12
 *
13
 * $Id: memutils.h,v 1.51 2003/05/02 20:54:36 tgl Exp $
14 15 16 17 18 19
 *
 *-------------------------------------------------------------------------
 */
#ifndef MEMUTILS_H
#define MEMUTILS_H

20
#include "nodes/memnodes.h"
21 22 23


/*
24
 * MaxAllocSize
25
 *		Quasi-arbitrary limit on size of allocations.
26 27
 *
 * Note:
28 29 30
 *		There is no guarantee that allocations smaller than MaxAllocSize
 *		will succeed.  Allocation requests larger than MaxAllocSize will
 *		be summarily denied.
31
 *
32
 * XXX This is deliberately chosen to correspond to the limiting size
B
Bruce Momjian 已提交
33
 * of varlena objects under TOAST.	See VARATT_MASK_SIZE in postgres.h.
34
 */
B
Bruce Momjian 已提交
35
#define MaxAllocSize	((Size) 0x3fffffff)		/* 1 gigabyte - 1 */
36

37
#define AllocSizeIsValid(size)	(0 < (size) && (size) <= MaxAllocSize)
38 39

/*
40 41 42
 * All chunks allocated by any memory context manager are required to be
 * preceded by a StandardChunkHeader at a spacing of STANDARDCHUNKHEADERSIZE.
 * A currently-allocated chunk must contain a backpointer to its owning
B
Bruce Momjian 已提交
43 44
 * context as well as the allocated size of the chunk.	The backpointer is
 * used by pfree() and repalloc() to find the context to call.	The allocated
45 46
 * size is not absolutely essential, but it's expected to be needed by any
 * reasonable implementation.
47
 */
48
typedef struct StandardChunkHeader
49
{
B
Bruce Momjian 已提交
50
	MemoryContext context;		/* owning context */
51
	Size		size;			/* size of data space allocated in chunk */
52
#ifdef MEMORY_CONTEXT_CHECKING
53 54
	/* when debugging memory usage, also store actual requested size */
	Size		requested_size;
55
#endif
56
} StandardChunkHeader;
57

58
#define STANDARDCHUNKHEADERSIZE  MAXALIGN(sizeof(StandardChunkHeader))
59

60

61
/*
62
 * Standard top-level memory contexts.
63
 *
64 65
 * Only TopMemoryContext and ErrorContext are initialized by
 * MemoryContextInit() itself.
66
 */
67 68 69 70
extern DLLIMPORT MemoryContext TopMemoryContext;
extern DLLIMPORT MemoryContext ErrorContext;
extern DLLIMPORT MemoryContext PostmasterContext;
extern DLLIMPORT MemoryContext CacheMemoryContext;
71
extern DLLIMPORT MemoryContext MessageContext;
72
extern DLLIMPORT MemoryContext TopTransactionContext;
73 74 75
/* These two are transient links to contexts owned by other objects: */
extern DLLIMPORT MemoryContext QueryContext;
extern DLLIMPORT MemoryContext PortalContext;
76 77


78
/*
79
 * Memory-context-type-independent functions in mcxt.c
80
 */
81 82 83 84 85 86
extern void MemoryContextInit(void);
extern void MemoryContextReset(MemoryContext context);
extern void MemoryContextDelete(MemoryContext context);
extern void MemoryContextResetChildren(MemoryContext context);
extern void MemoryContextDeleteChildren(MemoryContext context);
extern void MemoryContextResetAndDeleteChildren(MemoryContext context);
87
extern Size GetMemoryChunkSpace(void *pointer);
88
extern MemoryContext GetMemoryChunkContext(void *pointer);
89
extern void MemoryContextStats(MemoryContext context);
B
Bruce Momjian 已提交
90

91
#ifdef MEMORY_CONTEXT_CHECKING
92
extern void MemoryContextCheck(MemoryContext context);
93
#endif
94
extern bool MemoryContextContains(MemoryContext context, void *pointer);
95 96

/*
97 98 99
 * This routine handles the context-type-independent part of memory
 * context creation.  It's intended to be called from context-type-
 * specific creation routines, and noplace else.
100
 */
101
extern MemoryContext MemoryContextCreate(NodeTag tag, Size size,
B
Bruce Momjian 已提交
102 103 104
					MemoryContextMethods *methods,
					MemoryContext parent,
					const char *name);
105 106


107 108 109
/*
 * Memory-context-type-specific functions
 */
110

111 112
/* aset.c */
extern MemoryContext AllocSetContextCreate(MemoryContext parent,
B
Bruce Momjian 已提交
113 114 115 116
					  const char *name,
					  Size minContextSize,
					  Size initBlockSize,
					  Size maxBlockSize);
117

118 119 120 121
/*
 * Recommended default alloc parameters, suitable for "ordinary" contexts
 * that might hold quite a lot of data.
 */
122
#define ALLOCSET_DEFAULT_MINSIZE   0
123 124
#define ALLOCSET_DEFAULT_INITSIZE  (8 * 1024)
#define ALLOCSET_DEFAULT_MAXSIZE   (8 * 1024 * 1024)
125

126 127 128 129 130 131 132 133
/*
 * Recommended alloc parameters for "small" contexts that are not expected
 * to contain much data (for example, a context to contain a query plan).
 */
#define ALLOCSET_SMALL_MINSIZE   0
#define ALLOCSET_SMALL_INITSIZE  (1 * 1024)
#define ALLOCSET_SMALL_MAXSIZE   (8 * 1024)

134
#endif   /* MEMUTILS_H */