~ubuntu-branches/ubuntu/hardy/postgresql-8.4/hardy-backports

« back to all changes in this revision

Viewing changes to src/include/utils/palloc.h

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * palloc.h
 
4
 *        POSTGRES memory allocator definitions.
 
5
 *
 
6
 * This file contains the basic memory allocation interface that is
 
7
 * needed by almost every backend module.  It is included directly by
 
8
 * postgres.h, so the definitions here are automatically available
 
9
 * everywhere.  Keep it lean!
 
10
 *
 
11
 * Memory allocation occurs within "contexts".  Every chunk obtained from
 
12
 * palloc()/MemoryContextAlloc() is allocated within a specific context.
 
13
 * The entire contents of a context can be freed easily and quickly by
 
14
 * resetting or deleting the context --- this is both faster and less
 
15
 * prone to memory-leakage bugs than releasing chunks individually.
 
16
 * We organize contexts into context trees to allow fine-grain control
 
17
 * over chunk lifetime while preserving the certainty that we will free
 
18
 * everything that should be freed.  See utils/mmgr/README for more info.
 
19
 *
 
20
 *
 
21
 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
 
22
 * Portions Copyright (c) 1994, Regents of the University of California
 
23
 *
 
24
 * $PostgreSQL$
 
25
 *
 
26
 *-------------------------------------------------------------------------
 
27
 */
 
28
#ifndef PALLOC_H
 
29
#define PALLOC_H
 
30
 
 
31
/*
 
32
 * Type MemoryContextData is declared in nodes/memnodes.h.      Most users
 
33
 * of memory allocation should just treat it as an abstract type, so we
 
34
 * do not provide the struct contents here.
 
35
 */
 
36
typedef struct MemoryContextData *MemoryContext;
 
37
 
 
38
/*
 
39
 * CurrentMemoryContext is the default allocation context for palloc().
 
40
 * We declare it here so that palloc() can be a macro.  Avoid accessing it
 
41
 * directly!  Instead, use MemoryContextSwitchTo() to change the setting.
 
42
 */
 
43
extern PGDLLIMPORT MemoryContext CurrentMemoryContext;
 
44
 
 
45
/*
 
46
 * Fundamental memory-allocation operations (more are in utils/memutils.h)
 
47
 */
 
48
extern void *MemoryContextAlloc(MemoryContext context, Size size);
 
49
extern void *MemoryContextAllocZero(MemoryContext context, Size size);
 
50
extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);
 
51
 
 
52
#define palloc(sz)      MemoryContextAlloc(CurrentMemoryContext, (sz))
 
53
 
 
54
#define palloc0(sz) MemoryContextAllocZero(CurrentMemoryContext, (sz))
 
55
 
 
56
/*
 
57
 * The result of palloc() is always word-aligned, so we can skip testing
 
58
 * alignment of the pointer when deciding which MemSet variant to use.
 
59
 * Note that this variant does not offer any advantage, and should not be
 
60
 * used, unless its "sz" argument is a compile-time constant; therefore, the
 
61
 * issue that it evaluates the argument multiple times isn't a problem in
 
62
 * practice.
 
63
 */
 
64
#define palloc0fast(sz) \
 
65
        ( MemSetTest(0, sz) ? \
 
66
                MemoryContextAllocZeroAligned(CurrentMemoryContext, sz) : \
 
67
                MemoryContextAllocZero(CurrentMemoryContext, sz) )
 
68
 
 
69
extern void pfree(void *pointer);
 
70
 
 
71
extern void *repalloc(void *pointer, Size size);
 
72
 
 
73
/*
 
74
 * MemoryContextSwitchTo can't be a macro in standard C compilers.
 
75
 * But we can make it an inline function when using GCC.
 
76
 */
 
77
#ifdef __GNUC__
 
78
 
 
79
static __inline__ MemoryContext
 
80
MemoryContextSwitchTo(MemoryContext context)
 
81
{
 
82
        MemoryContext old = CurrentMemoryContext;
 
83
 
 
84
        CurrentMemoryContext = context;
 
85
        return old;
 
86
}
 
87
#else
 
88
 
 
89
extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
 
90
#endif   /* __GNUC__ */
 
91
 
 
92
/*
 
93
 * These are like standard strdup() except the copied string is
 
94
 * allocated in a context, not with malloc().
 
95
 */
 
96
extern char *MemoryContextStrdup(MemoryContext context, const char *string);
 
97
 
 
98
#define pstrdup(str)  MemoryContextStrdup(CurrentMemoryContext, (str))
 
99
 
 
100
extern char *pnstrdup(const char *in, Size len);
 
101
 
 
102
#if defined(WIN32) || defined(__CYGWIN__)
 
103
extern void *pgport_palloc(Size sz);
 
104
extern char *pgport_pstrdup(const char *str);
 
105
extern void pgport_pfree(void *pointer);
 
106
#endif
 
107
 
 
108
#endif   /* PALLOC_H */