palloc.h 3.7 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * palloc.h
4
 *	  POSTGRES memory allocator definitions.
5
 *
6 7 8
 * 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
B
Bruce Momjian 已提交
9
 * everywhere.	Keep it lean!
10
 *
B
Bruce Momjian 已提交
11
 * Memory allocation occurs within "contexts".	Every chunk obtained from
12 13 14 15 16 17 18 19
 * 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.
 *
20
 *
21
 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
22
 * Portions Copyright (c) 1994, Regents of the University of California
23
 *
24
 * $PostgreSQL: pgsql/src/include/utils/palloc.h,v 1.35 2006/03/05 15:59:07 momjian Exp $
25 26 27
 *
 *-------------------------------------------------------------------------
 */
28
#ifndef PALLOC_H
29 30
#define PALLOC_H

31
/*
B
Bruce Momjian 已提交
32
 * Type MemoryContextData is declared in nodes/memnodes.h.	Most users
33 34 35 36 37 38 39
 * 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().
B
Bruce Momjian 已提交
40
 * We declare it here so that palloc() can be a macro.	Avoid accessing it
41 42 43 44 45 46 47 48
 * directly!  Instead, use MemoryContextSwitchTo() to change the setting.
 */
extern DLLIMPORT MemoryContext CurrentMemoryContext;

/*
 * Fundamental memory-allocation operations (more are in utils/memutils.h)
 */
extern void *MemoryContextAlloc(MemoryContext context, Size size);
49 50
extern void *MemoryContextAllocZero(MemoryContext context, Size size);
extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);
51

B
Bruce Momjian 已提交
52
#define palloc(sz)	MemoryContextAlloc(CurrentMemoryContext, (sz))
53

B
Bruce Momjian 已提交
54
#define palloc0(sz) MemoryContextAllocZero(CurrentMemoryContext, (sz))
55 56 57 58 59 60 61 62 63

/*
 * 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.
 */
B
Bruce Momjian 已提交
64
#define palloc0fast(sz) \
65 66 67
	( MemSetTest(0, sz) ? \
		MemoryContextAllocZeroAligned(CurrentMemoryContext, sz) : \
		MemoryContextAllocZero(CurrentMemoryContext, sz) )
68

69
extern void pfree(void *pointer);
70

71
extern void *repalloc(void *pointer, Size size);
72

73 74 75 76 77 78 79 80 81 82
/*
 * MemoryContextSwitchTo can't be a macro in standard C compilers.
 * But we can make it an inline function when using GCC.
 */
#ifdef __GNUC__

static __inline__ MemoryContext
MemoryContextSwitchTo(MemoryContext context)
{
	MemoryContext old = CurrentMemoryContext;
B
Bruce Momjian 已提交
83

84 85 86 87 88
	CurrentMemoryContext = context;
	return old;
}
#else

89
extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
90 91
#endif   /* __GNUC__ */

92 93 94 95 96 97 98 99
/*
 * 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);

#define pstrdup(str)  MemoryContextStrdup(CurrentMemoryContext, (str))

100
#if defined(WIN32) || defined(__CYGWIN__)
101 102 103 104
extern void *pgport_palloc(Size sz);
extern char *pgport_pstrdup(const char *str);
extern void pgport_pfree(void *pointer);
#endif
105

106
#endif   /* PALLOC_H */