mirror of
https://github.com/postgres/postgres.git
synced 2025-07-08 11:42:09 +03:00
First phase of memory management rewrite (see backend/utils/mmgr/README
for details). It doesn't really do that much yet, since there are no short-term memory contexts in the executor, but the infrastructure is in place and long-term contexts are handled reasonably. A few long- standing bugs have been fixed, such as 'VACUUM; anything' in a single query string crashing. Also, out-of-memory is now considered a recoverable ERROR, not FATAL. Eliminate a large amount of crufty, now-dead code in and around memory management. Fix problem with holding off SIGTRAP, SIGSEGV, etc in postmaster and backend startup.
This commit is contained in:
@ -3,36 +3,85 @@
|
||||
* palloc.h
|
||||
* POSTGRES memory allocator definitions.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*
|
||||
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $Id: palloc.h,v 1.12 2000/01/26 05:58:38 momjian Exp $
|
||||
* $Id: palloc.h,v 1.13 2000/06/28 03:33:33 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
#ifndef PALLOC_H
|
||||
#define PALLOC_H
|
||||
|
||||
#ifdef PALLOC_IS_MALLOC
|
||||
|
||||
#define palloc(s) malloc(s)
|
||||
#define pfree(p) free(p)
|
||||
#define repalloc(p,s) realloc((p),(s))
|
||||
|
||||
#else /* ! PALLOC_IS_MALLOC */
|
||||
|
||||
/* ----------
|
||||
* In the case we use memory contexts, use macro's for palloc() etc.
|
||||
* ----------
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
#define palloc(s) ((void *)MemoryContextAlloc(CurrentMemoryContext,(Size)(s)))
|
||||
#define pfree(p) MemoryContextFree(CurrentMemoryContext,(Pointer)(p))
|
||||
#define repalloc(p,s) ((void *)MemoryContextRealloc(CurrentMemoryContext,(Pointer)(p),(Size)(s)))
|
||||
typedef struct MemoryContextData *MemoryContext;
|
||||
|
||||
#endif /* PALLOC_IS_MALLOC */
|
||||
/*
|
||||
* CurrentMemoryContext is the default allocation context for palloc().
|
||||
* We declare it here so that palloc() can be a macro. Avoid accessing it
|
||||
* directly! Instead, use MemoryContextSwitchTo() to change the setting.
|
||||
*/
|
||||
extern DLLIMPORT MemoryContext CurrentMemoryContext;
|
||||
|
||||
/*
|
||||
* Fundamental memory-allocation operations (more are in utils/memutils.h)
|
||||
*/
|
||||
extern void *MemoryContextAlloc(MemoryContext context, Size size);
|
||||
|
||||
#define palloc(sz) MemoryContextAlloc(CurrentMemoryContext, (sz))
|
||||
|
||||
extern void pfree(void *pointer);
|
||||
|
||||
extern void *repalloc(void *pointer, Size size);
|
||||
|
||||
extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
|
||||
|
||||
/*
|
||||
* 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);
|
||||
|
||||
#define pstrdup(str) MemoryContextStrdup(CurrentMemoryContext, (str))
|
||||
|
||||
|
||||
/* ----------------
|
||||
* Alignment macros: align a length or address appropriately for a given type.
|
||||
*
|
||||
* There used to be some incredibly crufty platform-dependent hackery here,
|
||||
* but now we rely on the configure script to get the info for us. Much nicer.
|
||||
*
|
||||
* NOTE: TYPEALIGN will not work if ALIGNVAL is not a power of 2.
|
||||
* That case seems extremely unlikely to occur in practice, however.
|
||||
* ----------------
|
||||
*/
|
||||
|
||||
#define TYPEALIGN(ALIGNVAL,LEN) (((long)(LEN) + (ALIGNVAL-1)) & ~(ALIGNVAL-1))
|
||||
|
||||
#define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
|
||||
#define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
|
||||
#define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN))
|
||||
#define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
|
||||
#define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
|
||||
|
||||
/* like strdup except uses palloc */
|
||||
extern char *pstrdup(const char *pointer);
|
||||
|
||||
#endif /* PALLOC_H */
|
||||
|
Reference in New Issue
Block a user