]> granicus.if.org Git - postgresql/blob - src/include/utils/palloc.h
Clean up jsonb code.
[postgresql] / src / include / utils / palloc.h
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-2014, PostgreSQL Global Development Group
22  * Portions Copyright (c) 1994, Regents of the University of California
23  *
24  * src/include/utils/palloc.h
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  * Avoid accessing it directly!  Instead, use MemoryContextSwitchTo()
41  * 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 extern void *palloc(Size size);
53 extern void *palloc0(Size size);
54 extern void *repalloc(void *pointer, Size size);
55 extern void pfree(void *pointer);
56
57 /*
58  * The result of palloc() is always word-aligned, so we can skip testing
59  * alignment of the pointer when deciding which MemSet variant to use.
60  * Note that this variant does not offer any advantage, and should not be
61  * used, unless its "sz" argument is a compile-time constant; therefore, the
62  * issue that it evaluates the argument multiple times isn't a problem in
63  * practice.
64  */
65 #define palloc0fast(sz) \
66         ( MemSetTest(0, sz) ? \
67                 MemoryContextAllocZeroAligned(CurrentMemoryContext, sz) : \
68                 MemoryContextAllocZero(CurrentMemoryContext, sz) )
69
70 /* Higher-limit allocators. */
71 extern void *MemoryContextAllocHuge(MemoryContext context, Size size);
72 extern void *repalloc_huge(void *pointer, Size size);
73
74 /*
75  * MemoryContextSwitchTo can't be a macro in standard C compilers.
76  * But we can make it an inline function if the compiler supports it.
77  * See STATIC_IF_INLINE in c.h.
78  *
79  * Although this header file is nominally backend-only, certain frontend
80  * programs like pg_controldata include it via postgres.h.  For some compilers
81  * it's necessary to hide the inline definition of MemoryContextSwitchTo in
82  * this scenario; hence the #ifndef FRONTEND.
83  */
84
85 #ifndef FRONTEND
86 #ifndef PG_USE_INLINE
87 extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
88 #endif   /* !PG_USE_INLINE */
89 #if defined(PG_USE_INLINE) || defined(MCXT_INCLUDE_DEFINITIONS)
90 STATIC_IF_INLINE MemoryContext
91 MemoryContextSwitchTo(MemoryContext context)
92 {
93         MemoryContext old = CurrentMemoryContext;
94
95         CurrentMemoryContext = context;
96         return old;
97 }
98 #endif   /* PG_USE_INLINE || MCXT_INCLUDE_DEFINITIONS */
99 #endif   /* FRONTEND */
100
101 /*
102  * These are like standard strdup() except the copied string is
103  * allocated in a context, not with malloc().
104  */
105 extern char *MemoryContextStrdup(MemoryContext context, const char *string);
106 extern char *pstrdup(const char *in);
107 extern char *pnstrdup(const char *in, Size len);
108
109 /* sprintf into a palloc'd buffer --- these are in psprintf.c */
110 extern char *
111 psprintf(const char *fmt,...)
112 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
113 extern size_t
114 pvsnprintf(char *buf, size_t len, const char *fmt, va_list args)
115 __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 0)));
116
117 #endif   /* PALLOC_H */