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-2012, 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 /*
00039  * CurrentMemoryContext is the default allocation context for palloc().
00040  * We declare it here so that palloc() can be a macro.  Avoid accessing it
00041  * directly!  Instead, use MemoryContextSwitchTo() to change the setting.
00042  */
00043 extern PGDLLIMPORT MemoryContext CurrentMemoryContext;
00044 
00045 /*
00046  * Fundamental memory-allocation operations (more are in utils/memutils.h)
00047  */
00048 extern void *MemoryContextAlloc(MemoryContext context, Size size);
00049 extern void *MemoryContextAllocZero(MemoryContext context, Size size);
00050 extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);
00051 
00052 #define palloc(sz)  MemoryContextAlloc(CurrentMemoryContext, (sz))
00053 
00054 #define palloc0(sz) MemoryContextAllocZero(CurrentMemoryContext, (sz))
00055 
00056 /*
00057  * The result of palloc() is always word-aligned, so we can skip testing
00058  * alignment of the pointer when deciding which MemSet variant to use.
00059  * Note that this variant does not offer any advantage, and should not be
00060  * used, unless its "sz" argument is a compile-time constant; therefore, the
00061  * issue that it evaluates the argument multiple times isn't a problem in
00062  * practice.
00063  */
00064 #define palloc0fast(sz) \
00065     ( MemSetTest(0, sz) ? \
00066         MemoryContextAllocZeroAligned(CurrentMemoryContext, sz) : \
00067         MemoryContextAllocZero(CurrentMemoryContext, sz) )
00068 
00069 extern void pfree(void *pointer);
00070 
00071 extern void *repalloc(void *pointer, Size size);
00072 
00073 /*
00074  * MemoryContextSwitchTo can't be a macro in standard C compilers.
00075  * But we can make it an inline function if the compiler supports it.
00076  *
00077  * This file has to be includable by some non-backend code such as
00078  * pg_resetxlog, so don't expose the CurrentMemoryContext reference
00079  * if FRONTEND is defined.
00080  */
00081 #if defined(USE_INLINE) && !defined(FRONTEND)
00082 
00083 static inline MemoryContext
00084 MemoryContextSwitchTo(MemoryContext context)
00085 {
00086     MemoryContext old = CurrentMemoryContext;
00087 
00088     CurrentMemoryContext = context;
00089     return old;
00090 }
00091 #else
00092 
00093 extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
00094 #endif   /* USE_INLINE && !FRONTEND */
00095 
00096 /*
00097  * These are like standard strdup() except the copied string is
00098  * allocated in a context, not with malloc().
00099  */
00100 extern char *MemoryContextStrdup(MemoryContext context, const char *string);
00101 
00102 #define pstrdup(str)  MemoryContextStrdup(CurrentMemoryContext, (str))
00103 
00104 extern char *pnstrdup(const char *in, Size len);
00105 
00106 #if defined(WIN32) || defined(__CYGWIN__)
00107 extern void *pgport_palloc(Size sz);
00108 extern char *pgport_pstrdup(const char *str);
00109 extern void pgport_pfree(void *pointer);
00110 #endif
00111 
00112 #endif   /* PALLOC_H */