Header And Logo

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

palloc.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * palloc.h
00004  *    POSTGRES memory allocator definitions.
00005  *
00006  * This file contains the basic memory allocation interface that is
00007  * needed by almost every backend module.  It is included directly by
00008  * postgres.h, so the definitions here are automatically available
00009  * everywhere.  Keep it lean!
00010  *
00011  * Memory allocation occurs within "contexts".  Every chunk obtained from
00012  * palloc()/MemoryContextAlloc() is allocated within a specific context.
00013  * The entire contents of a context can be freed easily and quickly by
00014  * resetting or deleting the context --- this is both faster and less
00015  * prone to memory-leakage bugs than releasing chunks individually.
00016  * We organize contexts into context trees to allow fine-grain control
00017  * over chunk lifetime while preserving the certainty that we will free
00018  * everything that should be freed.  See utils/mmgr/README for more info.
00019  *
00020  *
00021  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00022  * Portions Copyright (c) 1994, Regents of the University of California
00023  *
00024  * src/include/utils/palloc.h
00025  *
00026  *-------------------------------------------------------------------------
00027  */
00028 #ifndef PALLOC_H
00029 #define PALLOC_H
00030 
00031 /*
00032  * Type MemoryContextData is declared in nodes/memnodes.h.  Most users
00033  * of memory allocation should just treat it as an abstract type, so we
00034  * do not provide the struct contents here.
00035  */
00036 typedef struct MemoryContextData *MemoryContext;
00037 
00038 #ifndef FRONTEND
00039 
00040 /*
00041  * CurrentMemoryContext is the default allocation context for palloc().
00042  * We declare it here so that palloc() can be a macro.  Avoid accessing it
00043  * directly!  Instead, use MemoryContextSwitchTo() to change the setting.
00044  */
00045 extern PGDLLIMPORT MemoryContext CurrentMemoryContext;
00046 
00047 /*
00048  * Fundamental memory-allocation operations (more are in utils/memutils.h)
00049  */
00050 extern void *MemoryContextAlloc(MemoryContext context, Size size);
00051 extern void *MemoryContextAllocZero(MemoryContext context, Size size);
00052 extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);
00053 
00054 /*
00055  * The result of palloc() is always word-aligned, so we can skip testing
00056  * alignment of the pointer when deciding which MemSet variant to use.
00057  * Note that this variant does not offer any advantage, and should not be
00058  * used, unless its "sz" argument is a compile-time constant; therefore, the
00059  * issue that it evaluates the argument multiple times isn't a problem in
00060  * practice.
00061  */
00062 #define palloc0fast(sz) \
00063     ( MemSetTest(0, sz) ? \
00064         MemoryContextAllocZeroAligned(CurrentMemoryContext, sz) : \
00065         MemoryContextAllocZero(CurrentMemoryContext, sz) )
00066 
00067 /*
00068  * MemoryContextSwitchTo can't be a macro in standard C compilers.
00069  * But we can make it an inline function if the compiler supports it.
00070  * See STATIC_IF_INLINE in c.h.
00071  */
00072 
00073 #ifndef PG_USE_INLINE
00074 extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
00075 #endif   /* !PG_USE_INLINE */
00076 #if defined(PG_USE_INLINE) || defined(MCXT_INCLUDE_DEFINITIONS)
00077 STATIC_IF_INLINE MemoryContext
00078 MemoryContextSwitchTo(MemoryContext context)
00079 {
00080     MemoryContext old = CurrentMemoryContext;
00081 
00082     CurrentMemoryContext = context;
00083     return old;
00084 }
00085 #endif   /* PG_USE_INLINE || MCXT_INCLUDE_DEFINITIONS */
00086 
00087 /*
00088  * These are like standard strdup() except the copied string is
00089  * allocated in a context, not with malloc().
00090  */
00091 extern char *MemoryContextStrdup(MemoryContext context, const char *string);
00092 #endif /* !FRONTEND */
00093 
00094 extern char *pstrdup(const char *in);
00095 extern char *pnstrdup(const char *in, Size len);
00096 extern void *palloc(Size size);
00097 extern void *palloc0(Size size);
00098 extern void pfree(void *pointer);
00099 extern void *repalloc(void *pointer, Size size);
00100 
00101 #endif   /* PALLOC_H */