1 /*-------------------------------------------------------------------------
4 * this file contains general memory alignment, allocation
5 * and manipulation stuff that used to be spread out
6 * between the following files:
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)
13 * limit.h max bits/byte, etc.
16 * Copyright (c) 1994, Regents of the University of California
18 * $Id: memutils.h,v 1.7 1997/08/19 21:40:43 momjian Exp $
21 * some of the information in this file will be moved to
22 * other files, (like MaxHeapTupleSize and MaxAttributeSize).
24 *-------------------------------------------------------------------------
31 /*****************************************************************************
32 * align.h - alignment macros *
33 ****************************************************************************
34 [TRH] Let the compiler decide what alignment it uses instead of
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
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.}}
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))
57 * SHORTALIGN(LEN) - length (or address) aligned for shorts
59 #define SHORTALIGN(LEN)\
60 (((long)(LEN) + (sizeof (short) - 1)) & ~(sizeof (short) - 1))
62 #define INTALIGN(LEN)\
63 (((long)(LEN) + (sizeof (int) - 1)) & ~(sizeof (int) -1))
66 * LONGALIGN(LEN) - length (or address) aligned for longs
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
76 #define LONGALIGN(LEN)\
77 (((long)(LEN) + (sizeof (int) - 1)) & ~(sizeof (int) -1))
79 #define LONGALIGN(LEN)\
80 (((long)(LEN) + (sizeof (long) - 1)) & ~(sizeof (long) -1))
83 #define DOUBLEALIGN(LEN)\
84 (((long)(LEN) + (sizeof (double) - 1)) & ~(sizeof (double) -1))
86 #define MAXALIGN(LEN)\
87 (((long)(LEN) + (sizeof (double) - 1)) & ~(sizeof (double) -1))
89 /*****************************************************************************
90 * oset.h -- Fixed format ordered set definitions. *
91 *****************************************************************************/
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.
100 typedef struct OrderedElemData OrderedElemData;
101 typedef OrderedElemData* OrderedElem;
103 typedef struct OrderedSetData OrderedSetData;
104 typedef OrderedSetData* OrderedSet;
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 */
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! */
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);
128 /*****************************************************************************
129 * aset.h -- Allocation set definitions. *
130 *****************************************************************************/
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.
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.
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.
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.
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.
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.
167 * Allocation sets are not automatically reset on a system reset.
168 * Higher level code is responsible for cleaning up.
170 * There may other modes in the future.
175 * Aligned pointer which may be a member of an allocation set.
177 typedef Pointer AllocPointer;
181 * Mode of allocation for an allocation set.
184 * See above for a description of the various nodes.
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 */
193 #define DefaultAllocMode DynamicAllocMode
199 typedef struct AllocSetData {
200 OrderedSetData setData;
201 /* Note: this will change in the future to support other modes */
204 typedef AllocSetData *AllocSet;
207 * AllocPointerIsValid --
208 * True iff pointer is valid allocation pointer.
210 #define AllocPointerIsValid(pointer) PointerIsValid(pointer)
214 * True iff set is valid allocation set.
216 #define AllocSetIsValid(set) PointerIsValid(set)
218 extern void AllocSetInit(AllocSet set, AllocMode mode, Size limit);
220 extern void AllocSetReset(AllocSet set);
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,
228 extern void AllocSetDump(AllocSet set);
230 /*****************************************************************************
231 * clib.h -- Standard C library definitions *
232 *****************************************************************************/
235 * This file is OPERATING SYSTEM dependent!!!
240 * LibCCopyLength is only used within this file. -cim 6/12/90
243 typedef int LibCCopyLength;
247 * Copies fixed length block of memory to another.
249 #define MemoryCopy(toBuffer, fromBuffer, length)\
250 memcpy(toBuffer, fromBuffer, length)
252 /*****************************************************************************
253 * limit.h -- POSTGRES limit definitions. *
254 *****************************************************************************/
256 #define MaxBitsPerByte 8
258 typedef uint32 AttributeSize; /* XXX should be defined elsewhere */
260 #define MaxHeapTupleSize 0x7fffffff
261 #define MaxAttributeSize 0x7fffffff
263 #define MaxIndexAttributeNumber 7
266 #endif /* MEMUTILS_H */