Eneboo - Documentación para desarrolladores
|
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-2005, PostgreSQL Global Development Group 00022 * Portions Copyright (c) 1994, Regents of the University of California 00023 * 00024 * $PostgreSQL: pgsql/src/include/utils/palloc.h,v 1.34 2005/10/15 02:49:46 momjian Exp $ 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 DLLIMPORT 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 when using GCC. 00076 */ 00077 #ifdef __GNUC__ 00078 00079 static __inline__ MemoryContext 00080 MemoryContextSwitchTo(MemoryContext context) 00081 { 00082 MemoryContext old = CurrentMemoryContext; 00083 00084 CurrentMemoryContext = context; 00085 return old; 00086 } 00087 #else 00088 00089 extern MemoryContext MemoryContextSwitchTo(MemoryContext context); 00090 #endif /* __GNUC__ */ 00091 00092 /* 00093 * These are like standard strdup() except the copied string is 00094 * allocated in a context, not with malloc(). 00095 */ 00096 extern char *MemoryContextStrdup(MemoryContext context, const char *string); 00097 00098 #define pstrdup(str) MemoryContextStrdup(CurrentMemoryContext, (str)) 00099 00100 #if defined(WIN32) || defined(__CYGWIN__) 00101 extern void *pgport_palloc(Size sz); 00102 extern char *pgport_pstrdup(const char *str); 00103 extern void pgport_pfree(void *pointer); 00104 #endif 00105 00106 #endif /* PALLOC_H */