OSDN Git Service

Change Copyright from PostgreSQL, Inc to PostgreSQL Global Development Group.
[pg-rex/syncrep.git] / src / include / utils / palloc.h
1 /*-------------------------------------------------------------------------
2  *
3  * palloc.h
4  *        POSTGRES memory allocator definitions.
5  *
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!
10  *
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.
19  *
20  *
21  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
22  * Portions Copyright (c) 1994, Regents of the University of California
23  *
24  * $Id: palloc.h,v 1.14 2001/01/24 19:43:28 momjian Exp $
25  *
26  *-------------------------------------------------------------------------
27  */
28 #ifndef PALLOC_H
29 #define PALLOC_H
30
31 /*
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.
35  */
36 typedef struct MemoryContextData *MemoryContext;
37
38 /*
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.
42  */
43 extern DLLIMPORT MemoryContext CurrentMemoryContext;
44
45 /*
46  * Fundamental memory-allocation operations (more are in utils/memutils.h)
47  */
48 extern void *MemoryContextAlloc(MemoryContext context, Size size);
49
50 #define palloc(sz)  MemoryContextAlloc(CurrentMemoryContext, (sz))
51
52 extern void pfree(void *pointer);
53
54 extern void *repalloc(void *pointer, Size size);
55
56 extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
57
58 /*
59  * These are like standard strdup() except the copied string is
60  * allocated in a context, not with malloc().
61  */
62 extern char *MemoryContextStrdup(MemoryContext context, const char *string);
63
64 #define pstrdup(str)  MemoryContextStrdup(CurrentMemoryContext, (str))
65
66
67 /* ----------------
68  * Alignment macros: align a length or address appropriately for a given type.
69  *
70  * There used to be some incredibly crufty platform-dependent hackery here,
71  * but now we rely on the configure script to get the info for us. Much nicer.
72  *
73  * NOTE: TYPEALIGN will not work if ALIGNVAL is not a power of 2.
74  * That case seems extremely unlikely to occur in practice, however.
75  * ----------------
76  */
77
78 #define TYPEALIGN(ALIGNVAL,LEN) (((long)(LEN) + (ALIGNVAL-1)) & ~(ALIGNVAL-1))
79
80 #define SHORTALIGN(LEN)                 TYPEALIGN(ALIGNOF_SHORT, (LEN))
81 #define INTALIGN(LEN)                   TYPEALIGN(ALIGNOF_INT, (LEN))
82 #define LONGALIGN(LEN)                  TYPEALIGN(ALIGNOF_LONG, (LEN))
83 #define DOUBLEALIGN(LEN)                TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
84 #define MAXALIGN(LEN)                   TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
85
86
87 #endif   /* PALLOC_H */