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-2001, PostgreSQL Global Development Group
22 * Portions Copyright (c) 1994, Regents of the University of California
24 * $Id: palloc.h,v 1.16 2001/03/22 04:01:13 momjian Exp $
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;
39 * CurrentMemoryContext is the default allocation context for palloc().
40 * We declare it here so that palloc() can be a macro. Avoid accessing it
41 * directly! Instead, use MemoryContextSwitchTo() to change the setting.
43 extern DLLIMPORT MemoryContext CurrentMemoryContext;
46 * Fundamental memory-allocation operations (more are in utils/memutils.h)
48 extern void *MemoryContextAlloc(MemoryContext context, Size size);
50 #define palloc(sz) MemoryContextAlloc(CurrentMemoryContext, (sz))
52 extern void pfree(void *pointer);
54 extern void *repalloc(void *pointer, Size size);
56 extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
59 * These are like standard strdup() except the copied string is
60 * allocated in a context, not with malloc().
62 extern char *MemoryContextStrdup(MemoryContext context, const char *string);
64 #define pstrdup(str) MemoryContextStrdup(CurrentMemoryContext, (str))