OSDN Git Service

Update copyrights to 2003.
[pg-rex/syncrep.git] / src / include / utils / palloc.h
index 5ea3d9b..352108b 100644 (file)
@@ -3,35 +3,81 @@
  * palloc.h
  *       POSTGRES memory allocator definitions.
  *
+ * This file contains the basic memory allocation interface that is
+ * needed by almost every backend module.  It is included directly by
+ * postgres.h, so the definitions here are automatically available
+ * everywhere. Keep it lean!
  *
- * Copyright (c) 1994, Regents of the University of California
+ * Memory allocation occurs within "contexts". Every chunk obtained from
+ * palloc()/MemoryContextAlloc() is allocated within a specific context.
+ * The entire contents of a context can be freed easily and quickly by
+ * resetting or deleting the context --- this is both faster and less
+ * prone to memory-leakage bugs than releasing chunks individually.
+ * We organize contexts into context trees to allow fine-grain control
+ * over chunk lifetime while preserving the certainty that we will free
+ * everything that should be freed.  See utils/mmgr/README for more info.
  *
- * $Id: palloc.h,v 1.10 1999/07/14 01:20:30 momjian Exp $
+ *
+ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * $Id: palloc.h,v 1.26 2003/08/04 02:40:15 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
 #ifndef PALLOC_H
 #define PALLOC_H
 
-#ifdef PALLOC_IS_MALLOC
+/*
+ * Type MemoryContextData is declared in nodes/memnodes.h.     Most users
+ * of memory allocation should just treat it as an abstract type, so we
+ * do not provide the struct contents here.
+ */
+typedef struct MemoryContextData *MemoryContext;
+
+/*
+ * CurrentMemoryContext is the default allocation context for palloc().
+ * We declare it here so that palloc() can be a macro. Avoid accessing it
+ * directly!  Instead, use MemoryContextSwitchTo() to change the setting.
+ */
+extern DLLIMPORT MemoryContext CurrentMemoryContext;
 
-#define palloc(s)        malloc(s)
-#define pfree(p)         free(p)
-#define repalloc(p,s) realloc((p),(s))
+/*
+ * Fundamental memory-allocation operations (more are in utils/memutils.h)
+ */
+extern void *MemoryContextAlloc(MemoryContext context, Size size);
+extern void *MemoryContextAllocZero(MemoryContext context, Size size);
+extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);
 
-#else                                                  /* ! PALLOC_IS_MALLOC */
+#define palloc(sz)     MemoryContextAlloc(CurrentMemoryContext, (sz))
 
-/* ----------
- * In the case we use memory contexts, use macro's for palloc() etc.
- * ----------
+#define palloc0(sz) MemoryContextAllocZero(CurrentMemoryContext, (sz))
+
+/*
+ * The result of palloc() is always word-aligned, so we can skip testing
+ * alignment of the pointer when deciding which MemSet variant to use.
+ * Note that this variant does not offer any advantage, and should not be
+ * used, unless its "sz" argument is a compile-time constant; therefore, the
+ * issue that it evaluates the argument multiple times isn't a problem in
+ * practice.
  */
-#define palloc(s)        ((void *)MemoryContextAlloc(CurrentMemoryContext,(Size)(s)))
-#define pfree(p)         MemoryContextFree(CurrentMemoryContext,(Pointer)(p))
-#define repalloc(p,s) ((void *)MemoryContextRealloc(CurrentMemoryContext,(Pointer)(p),(Size)(s)))
+#define palloc0fast(sz) \
+       ( MemSetTest(0, sz) ? \
+               MemoryContextAllocZeroAligned(CurrentMemoryContext, sz) : \
+               MemoryContextAllocZero(CurrentMemoryContext, sz) )
+
+extern void pfree(void *pointer);
+
+extern void *repalloc(void *pointer, Size size);
 
-#endif  /* PALLOC_IS_MALLOC */
+extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
+
+/*
+ * These are like standard strdup() except the copied string is
+ * allocated in a context, not with malloc().
+ */
+extern char *MemoryContextStrdup(MemoryContext context, const char *string);
 
-/* like strdup except uses palloc */
-extern char *pstrdup(char *pointer);
+#define pstrdup(str)  MemoryContextStrdup(CurrentMemoryContext, (str))
 
-#endif  /* PALLOC_H */
+#endif   /* PALLOC_H */