]> granicus.if.org Git - postgresql/blob - src/include/utils/memutils.h
979b9f798443e13ff3d876f8dde26361bf73f4bb
[postgresql] / src / include / utils / memutils.h
1 /*-------------------------------------------------------------------------
2  *
3  * memutils.h--
4  *    this file contains general memory alignment, allocation
5  *    and manipulation stuff that used to be spread out
6  *    between the following files:
7  *
8  *      align.h                         alignment macros
9  *      aset.h                          memory allocation set stuff
10  *      oset.h                            (used by aset.h)
11  *      (bit.h                          bit array type / extern)
12  *      clib.h                          mem routines
13  *      limit.h                         max bits/byte, etc.
14  *
15  *
16  * Copyright (c) 1994, Regents of the University of California
17  *
18  * $Id: memutils.h,v 1.7 1997/08/19 21:40:43 momjian Exp $
19  *
20  * NOTES
21  *    some of the information in this file will be moved to
22  *    other files, (like MaxHeapTupleSize and MaxAttributeSize).
23  *
24  *-------------------------------------------------------------------------
25  */
26 #ifndef MEMUTILS_H
27 #define MEMUTILS_H
28
29
30 #if 0
31 /*****************************************************************************
32  *      align.h         - alignment macros                                   *
33  ****************************************************************************
34  [TRH] Let the compiler decide what alignment it uses instead of  
35 tending
36    we know better.
37    GCC (at least v2.5.8 and up) has an __alignof__ keyword.
38    However, we cannot use it here since on some architectures it reports
39    just a _recommended_ alignment instead of the actual alignment used in
40    padding structures (or at least, this is how I understand gcc's  
41 s...)
42    So define a macro that gives us the _actual_ alignment inside a struct.
43    {{note: assumes that alignment size is always a power of 2.}}
44  */
45 #define _ALIGNSIZE(TYPE)        offsetof(struct { char __c; TYPE __t;}, __t)
46 #define _ALIGN(TYPE, LEN) \
47         (((long)(LEN) + (_ALIGNSIZE(TYPE) - 1)) & ~(_ALIGNSIZE(TYPE) - 1))
48 #define SHORTALIGN(LEN)         _ALIGN(short, (LEN))
49 #define INTALIGN(LEN)           _ALIGN(int, (LEN))
50 #define LONGALIGN(LEN)          _ALIGN(long, (LEN))
51 #define DOUBLEALIGN(LEN)        _ALIGN(double, (LEN))
52 #define MAXALIGN(LEN)           _ALIGN(double, (LEN))
53
54 #endif /* 0 */
55
56 /*
57  *      SHORTALIGN(LEN) - length (or address) aligned for shorts
58  */
59 #define SHORTALIGN(LEN)\
60         (((long)(LEN) + (sizeof (short) - 1)) & ~(sizeof (short) - 1))
61
62 #define INTALIGN(LEN)\
63         (((long)(LEN) + (sizeof (int) - 1)) & ~(sizeof (int) -1))
64
65 /*
66  *      LONGALIGN(LEN)  - length (or address) aligned for longs
67  */
68 #if defined(sun) && ! defined(sparc)
69 #define LONGALIGN(LEN)  SHORTALIGN(LEN)
70 #elif defined (alpha) || defined(linuxalpha)
71      /* even though "long alignment" should really be on 8-byte boundaries
72       * for linuxalpha, we want the strictest alignment to be on 4-byte (int) 
73       * boundaries, because otherwise things break when they try to use the
74       * FormData_pg_* structures.  --djm 12/12/96
75       */
76 #define LONGALIGN(LEN)\
77         (((long)(LEN) + (sizeof (int) - 1)) & ~(sizeof (int) -1))
78 #else
79 #define LONGALIGN(LEN)\
80         (((long)(LEN) + (sizeof (long) - 1)) & ~(sizeof (long) -1))
81 #endif
82
83 #define DOUBLEALIGN(LEN)\
84         (((long)(LEN) + (sizeof (double) - 1)) & ~(sizeof (double) -1))
85
86 #define MAXALIGN(LEN)\
87         (((long)(LEN) + (sizeof (double) - 1)) & ~(sizeof (double) -1))
88
89 /*****************************************************************************
90  *    oset.h --         Fixed format ordered set definitions.                *
91  *****************************************************************************/
92 /* Note:
93  *      Fixed format ordered sets are <EXPLAIN>.
94  *      XXX This is a preliminary version.  Work is needed to explain
95  *      XXX semantics of the external definitions.  Otherwise, the
96  *      XXX functional interface should not change.
97  *
98  */
99
100 typedef struct OrderedElemData OrderedElemData;
101 typedef OrderedElemData* OrderedElem;
102
103 typedef struct OrderedSetData OrderedSetData;
104 typedef OrderedSetData* OrderedSet;
105
106 struct OrderedElemData {
107     OrderedElem next;   /* Next elem or &this->set->dummy       */
108     OrderedElem prev;   /* Previous elem or &this->set->head    */
109     OrderedSet  set;    /* Parent set                           */
110 };
111
112 struct OrderedSetData {
113     OrderedElem head;   /* First elem or &this->dummy           */
114     OrderedElem dummy;  /* (hack) Terminator == NULL            */
115     OrderedElem tail;   /* Last elem or &this->head             */
116     Offset      offset; /* Offset from struct base to elem      */
117     /* this could be signed short int! */
118 };
119
120 extern void OrderedSetInit(OrderedSet set, Offset offset);
121 extern bool OrderedSetContains(OrderedSet set, OrderedElem elem);
122 extern Pointer OrderedSetGetHead(OrderedSet set);
123 extern Pointer OrderedElemGetPredecessor(OrderedElem elem);
124 extern Pointer OrderedElemGetSuccessor(OrderedElem elem);
125 extern void  OrderedElemPop(OrderedElem elem);
126 extern void OrderedElemPushInto(OrderedElem elem, OrderedSet Set);
127
128 /*****************************************************************************
129  *    aset.h --         Allocation set definitions.                          *
130  *****************************************************************************/
131 /*
132  * Description:
133  *      An allocation set is a set containing allocated elements.  When
134  *      an allocation is requested for a set, memory is allocated and a
135  *      pointer is returned.  Subsequently, this memory may be freed or
136  *      reallocated.  In addition, an allocation set may be reset which
137  *      will cause all allocated memory to be freed.
138  *
139  *      Allocations may occur in four different modes.  The mode of
140  *      allocation does not affect the behavior of allocations except in
141  *      terms of performance.  The allocation mode is set at the time of
142  *      set initialization.  Once the mode is chosen, it cannot be changed
143  *      unless the set is reinitialized.
144  *
145  *      "Dynamic" mode forces all allocations to occur in a heap.  This
146  *      is a good mode to use when small memory segments are allocated
147  *      and freed very frequently.  This is a good choice when allocation
148  *      characteristics are unknown.  This is the default mode.
149  *
150  *      "Static" mode attemts to allocate space as efficiently as possible
151  *      without regard to freeing memory.  This mode should be chosen only
152  *      when it is known that many allocations will occur but that very
153  *      little of the allocated memory will be explicitly freed.
154  *
155  *      "Tunable" mode is a hybrid of dynamic and static modes.  The
156  *      tunable mode will use static mode allocation except when the
157  *      allocation request exceeds a size limit supplied at the time of set
158  *      initialization.  "Big" objects are allocated using dynamic mode.
159  *
160  *      "Bounded" mode attempts to allocate space efficiently given a limit
161  *      on space consumed by the allocation set.  This restriction can be
162  *      considered a "soft" restriction, because memory segments will
163  *      continue to be returned after the limit is exceeded.  The limit is
164  *      specified at the time of set initialization like for tunable mode.
165  *
166  * Note:
167  *      Allocation sets are not automatically reset on a system reset.
168  *      Higher level code is responsible for cleaning up.
169  *
170  *      There may other modes in the future.
171  */
172
173 /*
174  * AllocPointer --
175  *      Aligned pointer which may be a member of an allocation set.
176  */
177 typedef Pointer AllocPointer;
178
179 /*
180  * AllocMode --
181  *      Mode of allocation for an allocation set.
182  *
183  * Note:
184  *      See above for a description of the various nodes.
185  */
186 typedef enum AllocMode {
187     DynamicAllocMode,   /* always dynamically allocate */
188     StaticAllocMode,    /* always "statically" allocate */
189     TunableAllocMode,   /* allocations are "tuned" */
190     BoundedAllocMode    /* allocations bounded to fixed usage */
191 } AllocMode;
192
193 #define DefaultAllocMode        DynamicAllocMode
194
195 /*
196  * AllocSet --
197  *      Allocation set.
198  */
199 typedef struct AllocSetData {
200     OrderedSetData      setData;
201         /* Note: this will change in the future to support other modes */
202 } AllocSetData;
203
204 typedef AllocSetData *AllocSet;
205
206 /*
207  * AllocPointerIsValid --
208  *      True iff pointer is valid allocation pointer.
209  */
210 #define AllocPointerIsValid(pointer) PointerIsValid(pointer)
211
212 /*
213  * AllocSetIsValid --
214  *      True iff set is valid allocation set.
215  */
216 #define AllocSetIsValid(set) PointerIsValid(set)    
217
218 extern void AllocSetInit(AllocSet set, AllocMode mode, Size limit);
219
220 extern void AllocSetReset(AllocSet set);
221
222 extern bool AllocSetContains(AllocSet set, AllocPointer pointer);
223 extern AllocPointer AllocSetAlloc(AllocSet set, Size size);
224 extern void AllocSetFree(AllocSet set, AllocPointer pointer);
225 extern AllocPointer AllocSetRealloc(AllocSet set, AllocPointer pointer, 
226                                     Size size);
227
228 extern void AllocSetDump(AllocSet set);
229
230 /*****************************************************************************
231  *    clib.h --         Standard C library definitions                       *
232  *****************************************************************************/
233 /*
234  * Note:
235  *      This file is OPERATING SYSTEM dependent!!!
236  *
237  */
238
239 /* 
240  *      LibCCopyLength is only used within this file. -cim 6/12/90
241  * 
242  */
243 typedef int     LibCCopyLength;
244
245 /*
246  * MemoryCopy --
247  *      Copies fixed length block of memory to another.
248  */
249 #define MemoryCopy(toBuffer, fromBuffer, length)\
250     memcpy(toBuffer, fromBuffer, length)
251
252 /*****************************************************************************
253  *    limit.h --        POSTGRES limit definitions.                          *
254  *****************************************************************************/
255
256 #define MaxBitsPerByte  8
257
258 typedef uint32  AttributeSize;  /* XXX should be defined elsewhere */
259
260 #define MaxHeapTupleSize        0x7fffffff
261 #define MaxAttributeSize        0x7fffffff
262
263 #define MaxIndexAttributeNumber 7
264
265
266 #endif /* MEMUTILS_H */