codekingpro/portable-devtools
115k
1/*-------------------------------------------------------------------------2 *3 * palloc.h4 * POSTGRES memory allocator definitions.5 *6 * This file contains the basic memory allocation interface that is7 * needed by almost every backend module. It is included directly by8 * postgres.h, so the definitions here are automatically available9 * everywhere. Keep it lean!10 *11 * Memory allocation occurs within "contexts". Every chunk obtained from12 * palloc()/MemoryContextAlloc() is allocated within a specific context.13 * The entire contents of a context can be freed easily and quickly by14 * resetting or deleting the context --- this is both faster and less15 * prone to memory-leakage bugs than releasing chunks individually.16 * We organize contexts into context trees to allow fine-grain control17 * over chunk lifetime while preserving the certainty that we will free18 * everything that should be freed. See utils/mmgr/README for more info.19 *20 *21 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group22 * Portions Copyright (c) 1994, Regents of the University of California23 *24 * src/include/utils/palloc.h25 *26 *-------------------------------------------------------------------------27 */28#ifndef PALLOC_H29#define PALLOC_H30 31/*32 * Type MemoryContextData is declared in nodes/memnodes.h. Most users33 * of memory allocation should just treat it as an abstract type, so we34 * do not provide the struct contents here.35 */36typedef struct MemoryContextData *MemoryContext;37 38/*39 * A memory context can have callback functions registered on it. Any such40 * function will be called once just before the context is next reset or41 * deleted. The MemoryContextCallback struct describing such a callback42 * typically would be allocated within the context itself, thereby avoiding43 * any need to manage it explicitly (the reset/delete action will free it).44 */45typedef void (*MemoryContextCallbackFunction) (void *arg);46 47typedef struct MemoryContextCallback48{49 MemoryContextCallbackFunction func; /* function to call */50 void *arg; /* argument to pass it */51 struct MemoryContextCallback *next; /* next in list of callbacks */52} MemoryContextCallback;53 54/*55 * CurrentMemoryContext is the default allocation context for palloc().56 * Avoid accessing it directly! Instead, use MemoryContextSwitchTo()57 * to change the setting.58 */59extern PGDLLIMPORT MemoryContext CurrentMemoryContext;60 61/*62 * Flags for MemoryContextAllocExtended.63 */64#define MCXT_ALLOC_HUGE 0x01 /* allow huge allocation (> 1 GB) */65#define MCXT_ALLOC_NO_OOM 0x02 /* no failure if out-of-memory */66#define MCXT_ALLOC_ZERO 0x04 /* zero allocated memory */67 68/*69 * Fundamental memory-allocation operations (more are in utils/memutils.h)70 */71extern void *MemoryContextAlloc(MemoryContext context, Size size);72extern void *MemoryContextAllocZero(MemoryContext context, Size size);73extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);74extern void *MemoryContextAllocExtended(MemoryContext context,75 Size size, int flags);76extern void *MemoryContextAllocAligned(MemoryContext context,77 Size size, Size alignto, int flags);78 79extern void *palloc(Size size);80extern void *palloc0(Size size);81extern void *palloc_extended(Size size, int flags);82extern void *palloc_aligned(Size size, Size alignto, int flags);83extern pg_nodiscard void *repalloc(void *pointer, Size size);84extern pg_nodiscard void *repalloc_extended(void *pointer,85 Size size, int flags);86extern pg_nodiscard void *repalloc0(void *pointer, Size oldsize, Size size);87extern void pfree(void *pointer);88 89/*90 * Variants with easier notation and more type safety91 */92 93/*94 * Allocate space for one object of type "type"95 */96#define palloc_object(type) ((type *) palloc(sizeof(type)))97#define palloc0_object(type) ((type *) palloc0(sizeof(type)))98 99/*100 * Allocate space for "count" objects of type "type"101 */102#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count)))103#define palloc0_array(type, count) ((type *) palloc0(sizeof(type) * (count)))104 105/*106 * Change size of allocation pointed to by "pointer" to have space for "count"107 * objects of type "type"108 */109#define repalloc_array(pointer, type, count) ((type *) repalloc(pointer, sizeof(type) * (count)))110#define repalloc0_array(pointer, type, oldcount, count) ((type *) repalloc0(pointer, sizeof(type) * (oldcount), sizeof(type) * (count)))111 112/*113 * The result of palloc() is always word-aligned, so we can skip testing114 * alignment of the pointer when deciding which MemSet variant to use.115 * Note that this variant does not offer any advantage, and should not be116 * used, unless its "sz" argument is a compile-time constant; therefore, the117 * issue that it evaluates the argument multiple times isn't a problem in118 * practice.119 */120#define palloc0fast(sz) \121 ( MemSetTest(0, sz) ? \122 MemoryContextAllocZeroAligned(CurrentMemoryContext, sz) : \123 MemoryContextAllocZero(CurrentMemoryContext, sz) )124 125/* Higher-limit allocators. */126extern void *MemoryContextAllocHuge(MemoryContext context, Size size);127extern pg_nodiscard void *repalloc_huge(void *pointer, Size size);128 129/*130 * Although this header file is nominally backend-only, certain frontend131 * programs like pg_controldata include it via postgres.h. For some compilers132 * it's necessary to hide the inline definition of MemoryContextSwitchTo in133 * this scenario; hence the #ifndef FRONTEND.134 */135 136#ifndef FRONTEND137static inline MemoryContext138MemoryContextSwitchTo(MemoryContext context)139{140 MemoryContext old = CurrentMemoryContext;141 142 CurrentMemoryContext = context;143 return old;144}145#endif /* FRONTEND */146 147/* Registration of memory context reset/delete callbacks */148extern void MemoryContextRegisterResetCallback(MemoryContext context,149 MemoryContextCallback *cb);150 151/*152 * These are like standard strdup() except the copied string is153 * allocated in a context, not with malloc().154 */155extern char *MemoryContextStrdup(MemoryContext context, const char *string);156extern char *pstrdup(const char *in);157extern char *pnstrdup(const char *in, Size len);158 159extern char *pchomp(const char *in);160 161/* sprintf into a palloc'd buffer --- these are in psprintf.c */162extern char *psprintf(const char *fmt,...) pg_attribute_printf(1, 2);163extern size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3, 0);164 165#endif /* PALLOC_H */166 