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-2013, 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;
41 * CurrentMemoryContext is the default allocation context for palloc().
42 * We declare it here so that palloc() can be a macro. Avoid accessing it
43 * directly! Instead, use MemoryContextSwitchTo() to change the setting.
45 extern PGDLLIMPORT MemoryContext CurrentMemoryContext;
48 * Fundamental memory-allocation operations (more are in utils/memutils.h)
50 extern void *MemoryContextAlloc(MemoryContext context, Size size);
51 extern void *MemoryContextAllocZero(MemoryContext context, Size size);
52 extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);
55 * The result of palloc() is always word-aligned, so we can skip testing
56 * alignment of the pointer when deciding which MemSet variant to use.
57 * Note that this variant does not offer any advantage, and should not be
58 * used, unless its "sz" argument is a compile-time constant; therefore, the
59 * issue that it evaluates the argument multiple times isn't a problem in
62 #define palloc0fast(sz) \
63 ( MemSetTest(0, sz) ? \
64 MemoryContextAllocZeroAligned(CurrentMemoryContext, sz) : \
65 MemoryContextAllocZero(CurrentMemoryContext, sz) )
68 * MemoryContextSwitchTo can't be a macro in standard C compilers.
69 * But we can make it an inline function if the compiler supports it.
70 * See STATIC_IF_INLINE in c.h.
74 extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
75 #endif /* !PG_USE_INLINE */
76 #if defined(PG_USE_INLINE) || defined(MCXT_INCLUDE_DEFINITIONS)
77 STATIC_IF_INLINE MemoryContext
78 MemoryContextSwitchTo(MemoryContext context)
80 MemoryContext old = CurrentMemoryContext;
82 CurrentMemoryContext = context;
85 #endif /* PG_USE_INLINE || MCXT_INCLUDE_DEFINITIONS */
88 * These are like standard strdup() except the copied string is
89 * allocated in a context, not with malloc().
91 extern char *MemoryContextStrdup(MemoryContext context, const char *string);
92 #endif /* !FRONTEND */
94 extern char *pstrdup(const char *in);
95 extern char *pnstrdup(const char *in, Size len);
96 extern void *palloc(Size size);
97 extern void *palloc0(Size size);
98 extern void pfree(void *pointer);
99 extern void *repalloc(void *pointer, Size size);
101 #endif /* PALLOC_H */