1996-08-28 03:59:28 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* palloc.h
|
1996-08-28 03:59:28 +02:00
|
|
|
* POSTGRES memory allocator definitions.
|
|
|
|
*
|
2000-06-28 05:33:33 +02:00
|
|
|
* This file contains the basic memory allocation interface that is
|
|
|
|
* needed by almost every backend module. It is included directly by
|
|
|
|
* postgres.h, so the definitions here are automatically available
|
|
|
|
* everywhere. Keep it lean!
|
|
|
|
*
|
|
|
|
* Memory allocation occurs within "contexts". Every chunk obtained from
|
|
|
|
* palloc()/MemoryContextAlloc() is allocated within a specific context.
|
|
|
|
* The entire contents of a context can be freed easily and quickly by
|
|
|
|
* resetting or deleting the context --- this is both faster and less
|
|
|
|
* prone to memory-leakage bugs than releasing chunks individually.
|
|
|
|
* We organize contexts into context trees to allow fine-grain control
|
|
|
|
* over chunk lifetime while preserving the certainty that we will free
|
|
|
|
* everything that should be freed. See utils/mmgr/README for more info.
|
|
|
|
*
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
2022-01-08 01:04:57 +01:00
|
|
|
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/include/utils/palloc.h
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#ifndef PALLOC_H
|
|
|
|
#define PALLOC_H
|
|
|
|
|
2000-06-28 05:33:33 +02:00
|
|
|
/*
|
|
|
|
* Type MemoryContextData is declared in nodes/memnodes.h. Most users
|
|
|
|
* of memory allocation should just treat it as an abstract type, so we
|
|
|
|
* do not provide the struct contents here.
|
|
|
|
*/
|
|
|
|
typedef struct MemoryContextData *MemoryContext;
|
|
|
|
|
2015-03-01 18:31:32 +01:00
|
|
|
/*
|
|
|
|
* A memory context can have callback functions registered on it. Any such
|
|
|
|
* function will be called once just before the context is next reset or
|
|
|
|
* deleted. The MemoryContextCallback struct describing such a callback
|
|
|
|
* typically would be allocated within the context itself, thereby avoiding
|
|
|
|
* any need to manage it explicitly (the reset/delete action will free it).
|
|
|
|
*/
|
|
|
|
typedef void (*MemoryContextCallbackFunction) (void *arg);
|
|
|
|
|
|
|
|
typedef struct MemoryContextCallback
|
|
|
|
{
|
|
|
|
MemoryContextCallbackFunction func; /* function to call */
|
|
|
|
void *arg; /* argument to pass it */
|
|
|
|
struct MemoryContextCallback *next; /* next in list of callbacks */
|
|
|
|
} MemoryContextCallback;
|
|
|
|
|
2000-06-28 05:33:33 +02:00
|
|
|
/*
|
|
|
|
* CurrentMemoryContext is the default allocation context for palloc().
|
2014-04-26 20:14:28 +02:00
|
|
|
* Avoid accessing it directly! Instead, use MemoryContextSwitchTo()
|
|
|
|
* to change the setting.
|
2000-06-28 05:33:33 +02:00
|
|
|
*/
|
2007-07-25 14:22:54 +02:00
|
|
|
extern PGDLLIMPORT MemoryContext CurrentMemoryContext;
|
2000-06-28 05:33:33 +02:00
|
|
|
|
2015-01-30 18:56:48 +01:00
|
|
|
/*
|
|
|
|
* Flags for MemoryContextAllocExtended.
|
|
|
|
*/
|
|
|
|
#define MCXT_ALLOC_HUGE 0x01 /* allow huge allocation (> 1 GB) */
|
|
|
|
#define MCXT_ALLOC_NO_OOM 0x02 /* no failure if out-of-memory */
|
|
|
|
#define MCXT_ALLOC_ZERO 0x04 /* zero allocated memory */
|
|
|
|
|
2000-06-28 05:33:33 +02:00
|
|
|
/*
|
|
|
|
* Fundamental memory-allocation operations (more are in utils/memutils.h)
|
|
|
|
*/
|
|
|
|
extern void *MemoryContextAlloc(MemoryContext context, Size size);
|
2002-12-16 17:22:46 +01:00
|
|
|
extern void *MemoryContextAllocZero(MemoryContext context, Size size);
|
|
|
|
extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);
|
2015-01-30 18:56:48 +01:00
|
|
|
extern void *MemoryContextAllocExtended(MemoryContext context,
|
|
|
|
Size size, int flags);
|
2000-06-28 05:33:33 +02:00
|
|
|
|
2014-04-26 20:14:28 +02:00
|
|
|
extern void *palloc(Size size);
|
|
|
|
extern void *palloc0(Size size);
|
2015-04-03 10:36:12 +02:00
|
|
|
extern void *palloc_extended(Size size, int flags);
|
2020-10-17 08:38:39 +02:00
|
|
|
extern pg_nodiscard void *repalloc(void *pointer, Size size);
|
2014-04-26 20:14:28 +02:00
|
|
|
extern void pfree(void *pointer);
|
2013-06-27 20:53:57 +02:00
|
|
|
|
2022-09-12 08:31:56 +02:00
|
|
|
/*
|
|
|
|
* Variants with easier notation and more type safety
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate space for one object of type "type"
|
|
|
|
*/
|
|
|
|
#define palloc_object(type) ((type *) palloc(sizeof(type)))
|
|
|
|
#define palloc0_object(type) ((type *) palloc0(sizeof(type)))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate space for "count" objects of type "type"
|
|
|
|
*/
|
|
|
|
#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count)))
|
|
|
|
#define palloc0_array(type, count) ((type *) palloc0(sizeof(type) * (count)))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Change size of allocation pointed to by "pointer" to have space for "count"
|
|
|
|
* objects of type "type"
|
|
|
|
*/
|
|
|
|
#define repalloc_array(pointer, type, count) ((type *) repalloc(pointer, sizeof(type) * (count)))
|
|
|
|
|
2002-12-16 17:22:46 +01:00
|
|
|
/*
|
|
|
|
* The result of palloc() is always word-aligned, so we can skip testing
|
|
|
|
* alignment of the pointer when deciding which MemSet variant to use.
|
|
|
|
* Note that this variant does not offer any advantage, and should not be
|
|
|
|
* used, unless its "sz" argument is a compile-time constant; therefore, the
|
|
|
|
* issue that it evaluates the argument multiple times isn't a problem in
|
|
|
|
* practice.
|
|
|
|
*/
|
|
|
|
#define palloc0fast(sz) \
|
|
|
|
( MemSetTest(0, sz) ? \
|
|
|
|
MemoryContextAllocZeroAligned(CurrentMemoryContext, sz) : \
|
|
|
|
MemoryContextAllocZero(CurrentMemoryContext, sz) )
|
2002-11-10 03:17:25 +01:00
|
|
|
|
2014-04-26 20:14:28 +02:00
|
|
|
/* Higher-limit allocators. */
|
|
|
|
extern void *MemoryContextAllocHuge(MemoryContext context, Size size);
|
2020-10-17 08:38:39 +02:00
|
|
|
extern pg_nodiscard void *repalloc_huge(void *pointer, Size size);
|
2014-04-26 20:14:28 +02:00
|
|
|
|
2005-02-18 22:52:34 +01:00
|
|
|
/*
|
2014-04-28 03:24:19 +02:00
|
|
|
* Although this header file is nominally backend-only, certain frontend
|
|
|
|
* programs like pg_controldata include it via postgres.h. For some compilers
|
|
|
|
* it's necessary to hide the inline definition of MemoryContextSwitchTo in
|
|
|
|
* this scenario; hence the #ifndef FRONTEND.
|
2005-02-18 22:52:34 +01:00
|
|
|
*/
|
|
|
|
|
2014-04-28 03:24:19 +02:00
|
|
|
#ifndef FRONTEND
|
2015-08-05 18:19:52 +02:00
|
|
|
static inline MemoryContext
|
2005-02-18 22:52:34 +01:00
|
|
|
MemoryContextSwitchTo(MemoryContext context)
|
|
|
|
{
|
|
|
|
MemoryContext old = CurrentMemoryContext;
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-02-18 22:52:34 +01:00
|
|
|
CurrentMemoryContext = context;
|
|
|
|
return old;
|
|
|
|
}
|
2014-04-28 03:24:19 +02:00
|
|
|
#endif /* FRONTEND */
|
2005-02-18 22:52:34 +01:00
|
|
|
|
2015-03-01 18:31:32 +01:00
|
|
|
/* Registration of memory context reset/delete callbacks */
|
|
|
|
extern void MemoryContextRegisterResetCallback(MemoryContext context,
|
|
|
|
MemoryContextCallback *cb);
|
|
|
|
|
2000-06-28 05:33:33 +02:00
|
|
|
/*
|
|
|
|
* These are like standard strdup() except the copied string is
|
|
|
|
* allocated in a context, not with malloc().
|
|
|
|
*/
|
|
|
|
extern char *MemoryContextStrdup(MemoryContext context, const char *string);
|
Create libpgcommon, and move pg_malloc et al to it
libpgcommon is a new static library to allow sharing code among the
various frontend programs and backend; this lets us eliminate duplicate
implementations of common routines. We avoid libpgport, because that's
intended as a place for porting issues; per discussion, it seems better
to keep them separate.
The first use case, and the only implemented by this patch, is pg_malloc
and friends, which many frontend programs were already using.
At the same time, we can use this to provide palloc emulation functions
for the frontend; this way, some palloc-using files in the backend can
also be used by the frontend cleanly. To do this, we change palloc() in
the backend to be a function instead of a macro on top of
MemoryContextAlloc(). This was previously believed to cause loss of
performance, but this implementation has been tweaked by Tom and Andres
so that on modern compilers it provides a slight improvement over the
previous one.
This lets us clean up some places that were already with
localized hacks.
Most of the pg_malloc/palloc changes in this patch were authored by
Andres Freund. Zoltán Böszörményi also independently provided a form of
that. libpgcommon infrastructure was authored by Álvaro.
2013-02-12 14:33:40 +01:00
|
|
|
extern char *pstrdup(const char *in);
|
2008-06-28 18:45:22 +02:00
|
|
|
extern char *pnstrdup(const char *in, Size len);
|
2013-10-23 00:42:13 +02:00
|
|
|
|
2017-02-27 14:30:06 +01:00
|
|
|
extern char *pchomp(const char *in);
|
|
|
|
|
2013-10-25 03:43:57 +02:00
|
|
|
/* sprintf into a palloc'd buffer --- these are in psprintf.c */
|
|
|
|
extern char *psprintf(const char *fmt,...) pg_attribute_printf(1, 2);
|
|
|
|
extern size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3, 0);
|
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
#endif /* PALLOC_H */
|