1
/*-------------------------------------------------------------------------
4
* POSTGRES memory allocator definitions.
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!
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.
21
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
22
* Portions Copyright (c) 1994, Regents of the University of California
24
* src/include/utils/palloc.h
26
*-------------------------------------------------------------------------
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.
36
typedef struct MemoryContextData *MemoryContext;
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.
43
extern PGDLLIMPORT MemoryContext CurrentMemoryContext;
46
* Fundamental memory-allocation operations (more are in utils/memutils.h)
48
extern void *MemoryContextAlloc(MemoryContext context, Size size);
49
extern void *MemoryContextAllocZero(MemoryContext context, Size size);
50
extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);
52
#define palloc(sz) MemoryContextAlloc(CurrentMemoryContext, (sz))
54
#define palloc0(sz) MemoryContextAllocZero(CurrentMemoryContext, (sz))
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
64
#define palloc0fast(sz) \
65
( MemSetTest(0, sz) ? \
66
MemoryContextAllocZeroAligned(CurrentMemoryContext, sz) : \
67
MemoryContextAllocZero(CurrentMemoryContext, sz) )
69
extern void pfree(void *pointer);
71
extern void *repalloc(void *pointer, Size size);
74
* MemoryContextSwitchTo can't be a macro in standard C compilers.
75
* But we can make it an inline function if the compiler supports it.
77
* This file has to be includable by some non-backend code such as
78
* pg_resetxlog, so don't expose the CurrentMemoryContext reference
79
* if FRONTEND is defined.
81
#if defined(USE_INLINE) && !defined(FRONTEND)
83
static inline MemoryContext
84
MemoryContextSwitchTo(MemoryContext context)
86
MemoryContext old = CurrentMemoryContext;
88
CurrentMemoryContext = context;
93
extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
94
#endif /* USE_INLINE && !FRONTEND */
97
* These are like standard strdup() except the copied string is
98
* allocated in a context, not with malloc().
100
extern char *MemoryContextStrdup(MemoryContext context, const char *string);
102
#define pstrdup(str) MemoryContextStrdup(CurrentMemoryContext, (str))
104
extern char *pnstrdup(const char *in, Size len);
106
#if defined(WIN32) || defined(__CYGWIN__)
107
extern void *pgport_palloc(Size sz);
108
extern char *pgport_pstrdup(const char *str);
109
extern void pgport_pfree(void *pointer);
112
#endif /* PALLOC_H */