palloc.h 2.7 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * palloc.h
4
 *	  POSTGRES memory allocator definitions.
5
 *
6 7 8
 * 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
B
Bruce Momjian 已提交
9
 * everywhere.	Keep it lean!
10
 *
B
Bruce Momjian 已提交
11
 * Memory allocation occurs within "contexts".	Every chunk obtained from
12 13 14 15 16 17 18 19
 * 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.
 *
20
 *
B
Bruce Momjian 已提交
21
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
22
 * Portions Copyright (c) 1994, Regents of the University of California
23
 *
24
 * $Id: palloc.h,v 1.23 2002/11/13 00:37:06 momjian Exp $
25 26 27
 *
 *-------------------------------------------------------------------------
 */
28
#ifndef PALLOC_H
29 30
#define PALLOC_H

31
/*
B
Bruce Momjian 已提交
32
 * Type MemoryContextData is declared in nodes/memnodes.h.	Most users
33 34 35 36 37 38 39
 * of memory allocation should just treat it as an abstract type, so we
 * do not provide the struct contents here.
 */
typedef struct MemoryContextData *MemoryContext;

/*
 * CurrentMemoryContext is the default allocation context for palloc().
B
Bruce Momjian 已提交
40
 * We declare it here so that palloc() can be a macro.	Avoid accessing it
41 42 43 44 45 46 47 48
 * 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);
49
extern void *MemoryContextAllocPalloc0(MemoryContext context, Size size);
50

B
Bruce Momjian 已提交
51
#define palloc(sz)	MemoryContextAlloc(CurrentMemoryContext, (sz))
52

53 54 55 56 57
/* We assume palloc() is already int-aligned */
#define palloc0(sz)	\
	( MemSetTest(0, (sz)) ? \
		MemoryContextAllocPalloc0(CurrentMemoryContext, (sz)) : \
		memset(palloc(sz), 0, (sz)))
58

59
extern void pfree(void *pointer);
60

61
extern void *repalloc(void *pointer, Size size);
62

63 64 65 66 67 68 69 70 71 72
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))

73
#endif   /* PALLOC_H */