Header And Logo

PostgreSQL
| The world's most advanced open source database.

memutils.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * memutils.h
00004  *    This file contains declarations for memory allocation utility
00005  *    functions.  These are functions that are not quite widely used
00006  *    enough to justify going in utils/palloc.h, but are still part
00007  *    of the API of the memory management subsystem.
00008  *
00009  *
00010  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00011  * Portions Copyright (c) 1994, Regents of the University of California
00012  *
00013  * src/include/utils/memutils.h
00014  *
00015  *-------------------------------------------------------------------------
00016  */
00017 #ifndef MEMUTILS_H
00018 #define MEMUTILS_H
00019 
00020 #include "nodes/memnodes.h"
00021 
00022 
00023 /*
00024  * MaxAllocSize
00025  *      Quasi-arbitrary limit on size of allocations.
00026  *
00027  * Note:
00028  *      There is no guarantee that allocations smaller than MaxAllocSize
00029  *      will succeed.  Allocation requests larger than MaxAllocSize will
00030  *      be summarily denied.
00031  *
00032  * XXX This is deliberately chosen to correspond to the limiting size
00033  * of varlena objects under TOAST.  See VARSIZE_4B() and related macros
00034  * in postgres.h.  Many datatypes assume that any allocatable size can
00035  * be represented in a varlena header.
00036  *
00037  * XXX Also, various places in aset.c assume they can compute twice an
00038  * allocation's size without overflow, so beware of raising this.
00039  */
00040 #define MaxAllocSize    ((Size) 0x3fffffff)     /* 1 gigabyte - 1 */
00041 
00042 #define AllocSizeIsValid(size)  ((Size) (size) <= MaxAllocSize)
00043 
00044 /*
00045  * All chunks allocated by any memory context manager are required to be
00046  * preceded by a StandardChunkHeader at a spacing of STANDARDCHUNKHEADERSIZE.
00047  * A currently-allocated chunk must contain a backpointer to its owning
00048  * context as well as the allocated size of the chunk.  The backpointer is
00049  * used by pfree() and repalloc() to find the context to call.  The allocated
00050  * size is not absolutely essential, but it's expected to be needed by any
00051  * reasonable implementation.
00052  */
00053 typedef struct StandardChunkHeader
00054 {
00055     MemoryContext context;      /* owning context */
00056     Size        size;           /* size of data space allocated in chunk */
00057 #ifdef MEMORY_CONTEXT_CHECKING
00058     /* when debugging memory usage, also store actual requested size */
00059     Size        requested_size;
00060 #endif
00061 } StandardChunkHeader;
00062 
00063 #define STANDARDCHUNKHEADERSIZE  MAXALIGN(sizeof(StandardChunkHeader))
00064 
00065 
00066 /*
00067  * Standard top-level memory contexts.
00068  *
00069  * Only TopMemoryContext and ErrorContext are initialized by
00070  * MemoryContextInit() itself.
00071  */
00072 extern PGDLLIMPORT MemoryContext TopMemoryContext;
00073 extern PGDLLIMPORT MemoryContext ErrorContext;
00074 extern PGDLLIMPORT MemoryContext PostmasterContext;
00075 extern PGDLLIMPORT MemoryContext CacheMemoryContext;
00076 extern PGDLLIMPORT MemoryContext MessageContext;
00077 extern PGDLLIMPORT MemoryContext TopTransactionContext;
00078 extern PGDLLIMPORT MemoryContext CurTransactionContext;
00079 
00080 /* This is a transient link to the active portal's memory context: */
00081 extern PGDLLIMPORT MemoryContext PortalContext;
00082 
00083 
00084 /*
00085  * Memory-context-type-independent functions in mcxt.c
00086  */
00087 extern void MemoryContextInit(void);
00088 extern void MemoryContextReset(MemoryContext context);
00089 extern void MemoryContextDelete(MemoryContext context);
00090 extern void MemoryContextResetChildren(MemoryContext context);
00091 extern void MemoryContextDeleteChildren(MemoryContext context);
00092 extern void MemoryContextResetAndDeleteChildren(MemoryContext context);
00093 extern void MemoryContextSetParent(MemoryContext context,
00094                        MemoryContext new_parent);
00095 extern Size GetMemoryChunkSpace(void *pointer);
00096 extern MemoryContext GetMemoryChunkContext(void *pointer);
00097 extern MemoryContext MemoryContextGetParent(MemoryContext context);
00098 extern bool MemoryContextIsEmpty(MemoryContext context);
00099 extern void MemoryContextStats(MemoryContext context);
00100 
00101 #ifdef MEMORY_CONTEXT_CHECKING
00102 extern void MemoryContextCheck(MemoryContext context);
00103 #endif
00104 extern bool MemoryContextContains(MemoryContext context, void *pointer);
00105 
00106 /*
00107  * This routine handles the context-type-independent part of memory
00108  * context creation.  It's intended to be called from context-type-
00109  * specific creation routines, and noplace else.
00110  */
00111 extern MemoryContext MemoryContextCreate(NodeTag tag, Size size,
00112                     MemoryContextMethods *methods,
00113                     MemoryContext parent,
00114                     const char *name);
00115 
00116 
00117 /*
00118  * Memory-context-type-specific functions
00119  */
00120 
00121 /* aset.c */
00122 extern MemoryContext AllocSetContextCreate(MemoryContext parent,
00123                       const char *name,
00124                       Size minContextSize,
00125                       Size initBlockSize,
00126                       Size maxBlockSize);
00127 
00128 /*
00129  * Recommended default alloc parameters, suitable for "ordinary" contexts
00130  * that might hold quite a lot of data.
00131  */
00132 #define ALLOCSET_DEFAULT_MINSIZE   0
00133 #define ALLOCSET_DEFAULT_INITSIZE  (8 * 1024)
00134 #define ALLOCSET_DEFAULT_MAXSIZE   (8 * 1024 * 1024)
00135 
00136 /*
00137  * Recommended alloc parameters for "small" contexts that are not expected
00138  * to contain much data (for example, a context to contain a query plan).
00139  */
00140 #define ALLOCSET_SMALL_MINSIZE   0
00141 #define ALLOCSET_SMALL_INITSIZE  (1 * 1024)
00142 #define ALLOCSET_SMALL_MAXSIZE   (8 * 1024)
00143 
00144 #endif   /* MEMUTILS_H */