Veil
veil_datatypes.h
Go to the documentation of this file.
1 /**
2  * @file veil_datatypes.h
3  * \code
4  * Author: Marc Munro
5  * Copyright (c) 2005 - 2011 Marc Munro
6  * License: BSD
7  *
8  * \endcode
9  * @brief
10  * Define all Veil public datatypes
11  *
12  */
13 
14 #ifndef VEIL_DATATYPES
15 /**
16  * Prevent this header from being included multiple times.
17  */
18 #define VEIL_DATATYPES 1
19 
20 #ifndef VEIL_DEBUG
21 /**
22  * Enables various debugging constructs, such as canaries, in code and
23  * data structures. If such debugging is required, define VEIL_DEBUG in
24  * the make invocation, eg: "make VEIL_DEBUG=1".
25  */
26 // TODO: REMOVE THE FOLLOWING DEFINITION
27 #define VEIL_DEBUG 1
28 #endif
29 
30 #if VEIL_DEBUG == 1
31 /**
32  * Value to be set in sacrificial "canary" fields. If this value is not
33  * as expected, the canary has been killed by something inappropriately
34  * stomping on memory.
35  */
36 #define DBG_CANARY 0xca96ca96
37 
38 /**
39  * Defines a canary element in a data structure.
40  */
41 #define DBG_CANARY_ENTRY int32 canary;
42 
43 /**
44  * Field to record the size of an array so that its canary element can
45  * be found.
46  */
47 #define DBG_ELEMS_ENTRY int32 dbgelems;
48 
49 /**
50  * Code to records the size of an array so that its canary element can
51  * be found.
52  */
53 #define DBG_SET_ELEMS(x,y) (x).dbgelems = y
54 
55 /**
56  * Code to initialise a canary.
57  */
58 #define DBG_SET_CANARY(x) (x).canary = DBG_CANARY
59 
60 /**
61  * Code to test for a canary having been overwritten.
62  */
63 #define DBG_TEST_CANARY(x) if ((x).canary != DBG_CANARY) {\
64  elog(ERROR, "canary fault"); }
65 
66 /**
67  * Base size for an array containing a canary. This is zero if
68  * VEIL_DEBUG is not defined, when this is defined arrays will be one
69  * element longer to allow a canary to be placed at the end of the array.
70  */
71 #define EMPTY 1
72 
73 /**
74  * Set a trailing canary in an array.
75  */
76 #define DBG_SET_TRAILER(x, y) (x).y[(x).dbgelems] = DBG_CANARY;
77 /**
78  * Set a trailing canary in an array (using a void pointer).
79  */
80 #define DBG_SET_TRAILERP(x, y) (x).y[(x).dbgelems] = (void *) DBG_CANARY;
81 
82 /**
83  * Test the trailing canary in an array.
84  */
85 #define DBG_TEST_TRAILER(x, y) \
86  if ((uintptr_t) (x).y[(x).dbgelems] != DBG_CANARY) { \
87  elog(ERROR, "trailing canary fault"); }
88 
89 /**
90  * Check whether an array index is in bounds.
91  */
92 #define DBG_CHECK_INDEX(x,i) if (i >= (x).dbgelems) {\
93  elog(ERROR, "Element index out of range %d", i); }
94 
95 #else
96 #define DBG_CANARY_ENTRY
97 #define DBG_ELEMS_ENTRY
98 #define DBG_SET_ELEMS(x,y)
99 #define DBG_SET_CANARY(x)
100 #define DBG_TEST_CANARY(x)
101 #define EMPTY 0
102 #define DBG_SET_TRAILER(x,Y)
103 #define DBG_TEST_TRAILER(x,Y)
104 #define DBG_CHECK_INDEX(x,i)
105 #endif
106 
107 #include "utils/hsearch.h"
108 #include "storage/lwlock.h"
109 
110 /**
111  * Chunks provide a linked list of dynamically allocated shared memory
112  * segments, with the most recently allocated chunk at the tail.
113  * Shmalloc allocates space from this list of chunks, creating new
114  * chunks as needed up to MAX_ALLOWED_SHMEM.
115  */
116 typedef struct MemChunk {
117  struct MemChunk *next_chunk; /**< Pointer to next allocated chunk */
118  size_t next; /**< Offset, within this chunk, of 1st
119  * free byte */
120  size_t limit; /**< Offset, of 1st byte beyond chunk */
121  void *memory[0]; /**< The rest of the chunk, from which
122  * memory is allocated */
123 } MemChunk;
124 
125 
126 
127 /**
128  * The key length for veil hash types.
129  */
130 #define HASH_KEYLEN 60
131 
132 
133 /**
134  * Describes the type of an Object record or one of its subtypes.
135  */
136 typedef enum {
137  OBJ_UNDEFINED = 0,
138  OBJ_SHMEMCTL,
139  OBJ_INT4,
140  OBJ_RANGE,
141  OBJ_BITMAP,
142  OBJ_BITMAP_ARRAY,
143  OBJ_BITMAP_HASH,
144  OBJ_BITMAP_REF,
145  OBJ_INT4_ARRAY
146 } ObjType;
147 
148 /**
149  * General purpose object-type. All veil variables are effectively
150  * sub-types of this.
151  */
152 typedef struct Object {
153  ObjType type; /**< Identifies the type of the object. */
154 } Object;
155 
156 /**
157  * The ShmemCtl structure is the first object allocated from the first
158  * chunk of shared memory in context 0. This object describes and
159  * manages shared memory allocated by shmalloc()
160  */
161 typedef struct ShmemCtl {
162  ObjType type; /**< This must have the value OBJ_SHMEMCTL */
163  bool initialised; /**< Set to true once struct is setup */
164  LWLockId veil_lwlock; /**< dynamically allocated LWLock */
165  int current_context; /**< Index of the current context (0
166  * or 1) */
167  size_t total_allocated[2]; /**< Total shared memory allocated in
168  * chunks in each context */
169  bool switching; /**< Whether a context-switch is in
170  * progress */
171  MemChunk *context[2]; /**< The first chunks of each context */
172  TransactionId xid[2]; /**< The transaction id of the
173  * transaction that initialised each
174  * context: this is used to determine
175  * whether there are transactions
176  * still runnning that may be using an
177  * earlier context. */
178 } ShmemCtl;
179 
180 /**
181  * Subtype of Object for storing simple int4 values. These values are
182  * allowed to be null.
183  */
184 typedef struct Int4Var {
185  ObjType type; /**< This must have the value OBJ_INT4 */
186  bool isnull; /**< if true, the value is null */
187  int32 value; /**< the integer value of the variable */
188 } Int4Var;
189 
190 /**
191  * Subtype of Object for storing range values. A range has an upper and
192  * lower bound, both stored as int4s. Nulls are not allowed.
193  */
194 typedef struct Range {
195  ObjType type; /**< This must have the value OBJ_RANGE */
196  int32 min; /**< Lower limit for range. */
197  int32 max; /**< Upper limit for range. */
198 } Range;
199 
200 
201 /* Bitmaps will be based on 64-bit integers if pointer types are 64-bit,
202  * unless FORCE_32_BIT is defined.
203  */
204 #ifdef FORCE_32_BIT
205 #undef USE_64_BIT
206 #else
207 #if (SIZEOF_VOID_P == 8)
208 #define USE_64_BIT 1
209 #else
210 #undef USE_64_BIT
211 #endif
212 #endif
213 
214 /**
215  * Gives the bitmask index for the bitzero value of a Bitmap. This is
216  * part of the "normalisation" process for bitmap ranges. This process
217  * allows unlike bitmaps to be more easily compared by forcing bitmap
218  * indexes to be normalised around 32 or 64 bit word boundaries. Eg, 2
219  * bitmaps with domains 1 to 50 and 3 to 55, will have identical bit
220  * patterns for bits 3 to 50.
221  *
222  * @param x The bitzero value of a bitmap
223  *
224  * @return The bitmask index representing x.
225  */
226 #ifdef USE_64_BIT
227 #define BITZERO(x) (x & 0xffffffffffffffc0)
228 #else
229 #define BITZERO(x) (x & 0xffffffe0)
230 #endif
231 
232 /**
233  * Gives the bitmask index for the bitmax value of a bitmap. See
234  * BITZERO() for more information.
235  *
236  * @param x The bitmax value of a bitmap
237  *
238  * @return The bitmask index representing x.
239  */
240 #ifdef USE_64_BIT
241 #define BITMAX(x) (x | 0x3f)
242 #else
243 #define BITMAX(x) (x | 0x1f)
244 #endif
245 
246 /**
247  * Gives the index of a bit within the array of 32-bit words that
248  * comprise the bitmap.
249  *
250  * @param x The bit in question
251  *
252  * @return The array index of the bit.
253  */
254 #ifdef USE_64_BIT
255 #define BITSET_ELEM(x) (x >> 6)
256 #else
257 #define BITSET_ELEM(x) (x >> 5)
258 #endif
259 
260 /**
261  * Gives the index into ::bitmasks for the bit specified in x.
262  *
263  * @param x The bit in question
264  *
265  * @return The bitmask index
266  */
267 #ifdef USE_64_BIT
268 #define BITSET_BIT(x) (x & 0x3f)
269 #else
270 #define BITSET_BIT(x) (x & 0x1f)
271 #endif
272 
273 /**
274  * Gives the number of array elements in a ::Bitmap that runs from
275  * element min to element max.
276  *
277  * @param min
278  * @param max
279  *
280  * @return The number of elements in the bitmap.
281  */
282 #ifdef USE_64_BIT
283 #define ARRAYELEMS(min,max) (((max - BITZERO(min)) >> 6) + 1)
284 #else
285 #define ARRAYELEMS(min,max) (((max - BITZERO(min)) >> 5) + 1)
286 #endif
287 
288 
289 /**
290  * Return the smaller of a or b. Note that expressions a and b may be
291  * evaluated more than once.
292  *
293  * @param a
294  * @param b
295  *
296  * @return The smaller value of a or b.
297  */
298 #define MIN(a,b) ((a < b)? a: b)
299 
300 #ifdef USE_64_BIT
301 typedef uint64 bm_int;
302 #else
303 typedef uint32 bm_int;
304 #endif
305 
306 /**
307  * Subtype of Object for storing bitmaps. A bitmap is stored as an
308  * array of int4 values. See veil_bitmap.c for more information. Note
309  * that the size of a Bitmap structure is determined dynamically at run
310  * time as the size of the array is only known then.
311  */
312 typedef struct Bitmap {
313  ObjType type; /**< This must have the value OBJ_BITMAP */
314  DBG_CANARY_ENTRY /**< Debugging entry */
315  DBG_ELEMS_ENTRY /**< Debugging entry */
316  int32 bitzero; /**< The index of the lowest bit the bitmap can
317  * store */
318  int32 bitmax; /**< The index of the highest bit the bitmap can
319  * store */
320  bm_int bitset[EMPTY]; /**< Element zero of the array of int4 values
321  * comprising the bitmap. */
322 } Bitmap;
323 
324 /**
325  * Subtype of Object for storing bitmap refs. A bitmapref is like a
326  * bitmap but instead of containing a bitmap it contains a reference to
327  * one. This reference may be set during a transaction and then
328  * referenced only from within the setting transaction.
329  */
330 typedef struct BitmapRef {
331  ObjType type; /**< This must have the value OBJ_BITMAP_REF */
332  TransactionId xid; /**< The xid for which this variable is
333  * valid */
334  Bitmap *bitmap; /**< Pointer to the referenced bitmap */
335 } BitmapRef;
336 
337 /**
338  * Subtype of Object for storing bitmap arrays. A bitmap array is
339  * simply an array of pointers to dynamically allocated Bitmaps. Note
340  * that the size of a Bitmap structure is determined dynamically at run
341  * time as the size of the array is only known then.
342  */
343 typedef struct BitmapArray { // subtype of Object
344  ObjType type; /**< This must have the value OBJ_BITMAP_ARRAY */
345  DBG_CANARY_ENTRY /**< Debugging entry */
346  DBG_ELEMS_ENTRY /**< Debugging entry */
347  int32 bitzero; /**< The index of the lowest bit each bitmap can
348  * store */
349  int32 bitmax; /**< The index of the highest bit each bitmap can
350  * store */
351  int32 arrayzero; /**< The index of array element zero: the
352  * index of the lowest numbered bitmap in the
353  * array */
354  int32 arraymax; /**< The index of the lowest numbered bitmap in
355  * the array */
356  Bitmap *bitmap[EMPTY]; /**< Element zero of the array of Bitmap pointers
357  * comprising the array. */
358 } BitmapArray;
359 
360 /**
361  * Subtype of Object for storing bitmap hashes. A bitmap hash is a hash
362  * of dynamically allocated bitmaps, keyed by strings. Note that these
363  * cannot be created as shared variables.
364  */
365 typedef struct BitmapHash {
366  ObjType type; /**< This must have the value OBJ_BITMAP_HASH */
367  int32 bitzero; /**< The index of the lowest bit each bitmap can
368  * store */
369  int32 bitmax; /**< The index of the highest bit each bitmap can
370  * store */
371  HTAB *hash; /**< Pointer to the (Postgresql dynahash) hash
372  * table */
373 } BitmapHash;
374 
375 
376 /**
377  * Subtype of Object for storing arrays of integers.
378  */
379 typedef struct Int4Array {
380  ObjType type; /**< This must have the value OBJ_INT4_ARRAY */
381  int32 arrayzero; /**< The index of array element zero: the
382  * index of the lowest numbered bitmap in the
383  * array */
384  int32 arraymax; /**< The index of the lowest numbered bitmap in
385  * the array */
386  int32 array[0]; /**< Element zero of the array of integers */
387 } Int4Array;
388 
389 
390 /**
391  * A Veil variable. These may be session or shared variables, and may
392  * contain any Veil variable type. They are created and accessed by
393  * vl_lookup_shared_variable() and vl_lookup_variable(), and are stored
394  * in either the shared hash or one of the session hashes. See
395  * veil_shmem.c and veil_variables.c for more details.
396  */
397 typedef struct VarEntry {
398  char key[HASH_KEYLEN]; /**< String containing variable name */
399  bool shared; /**< Whether this is a shared variable */
400  Object *obj; /**< Pointer to the contents of the variable */
401 } VarEntry;
402 
403 
404 /**
405  * Describes a veil shared or session variable. This matches the SQL
406  * veil_variable_t which is defined as:
407 \verbatim
408 create type veil_variable_t as (
409  name text,
410  type text,
411  shared bool,
412 );
413 \endverbatim
414  */
415 typedef struct veil_variable_t {
416  char *name; /**< The name of the variable */
417  char *type; /**< The type of the variable (eg "Bitmap") */
418  bool shared; /**< Whether this is a shared variable (as
419  opposed to a session variable) */
421 
422 
423 #endif
424 
struct ShmemCtl ShmemCtl
The ShmemCtl structure is the first object allocated from the first chunk of shared memory in context...
struct Int4Array Int4Array
Subtype of Object for storing arrays of integers.
TransactionId xid
The xid for which this variable is valid.
Subtype of Object for storing arrays of integers.
Subtype of Object for storing bitmap refs.
struct MemChunk MemChunk
Chunks provide a linked list of dynamically allocated shared memory segments, with the most recently ...
Subtype of Object for storing bitmaps.
int32 value
the integer value of the variable
int32 bitzero
The index of the lowest bit each bitmap can store.
Bitmap * bitmap
Pointer to the referenced bitmap.
int32 bitmax
The index of the highest bit each bitmap can store.
Subtype of Object for storing simple int4 values.
ObjType type
This must have the value OBJ_BITMAP.
Describes a veil shared or session variable.
bool shared
Whether this is a shared variable (as opposed to a session variable)
Chunks provide a linked list of dynamically allocated shared memory segments, with the most recently ...
bool switching
Whether a context-switch is in progress.
bool initialised
Set to true once struct is setup.
ObjType type
This must have the value OBJ_BITMAP_ARRAY.
char * name
The name of the variable.
struct Bitmap Bitmap
Subtype of Object for storing bitmaps.
int current_context
Index of the current context (0 or 1)
ObjType type
This must have the value OBJ_RANGE.
int32 bitmax
The index of the highest bit the bitmap can store.
void * memory[0]
The rest of the chunk, from which memory is allocated.
ObjType type
This must have the value OBJ_INT4_ARRAY.
size_t limit
Offset, of 1st byte beyond chunk.
#define HASH_KEYLEN
The key length for veil hash types.
struct veil_variable_t veil_variable_t
Describes a veil shared or session variable.
int32 arrayzero
The index of array element zero: the index of the lowest numbered bitmap in the array.
int32 arraymax
The index of the lowest numbered bitmap in the array.
#define DBG_ELEMS_ENTRY
Field to record the size of an array so that its canary element can be found.
struct BitmapHash BitmapHash
Subtype of Object for storing bitmap hashes.
bool shared
Whether this is a shared variable.
General purpose object-type.
#define DBG_CANARY_ENTRY
Defines a canary element in a data structure.
struct Object Object
General purpose object-type.
int32 bitzero
The index of the lowest bit the bitmap can store.
struct BitmapArray BitmapArray
Subtype of Object for storing bitmap arrays.
Subtype of Object for storing bitmap hashes.
char * type
The type of the variable (eg "Bitmap")
ObjType
Describes the type of an Object record or one of its subtypes.
#define EMPTY
Base size for an array containing a canary.
HTAB * hash
Pointer to the (Postgresql dynahash) hash table.
A Veil variable.
struct Range Range
Subtype of Object for storing range values.
size_t next
Offset, within this chunk, of 1st free byte.
ObjType type
This must have the value OBJ_BITMAP_REF.
int32 arrayzero
The index of array element zero: the index of the lowest numbered bitmap in the array.
Subtype of Object for storing range values.
int32 max
Upper limit for range.
ObjType type
This must have the value OBJ_SHMEMCTL.
int32 bitzero
The index of the lowest bit each bitmap can store.
ObjType type
Identifies the type of the object.
LWLockId veil_lwlock
dynamically allocated LWLock
int32 bitmax
The index of the highest bit each bitmap can store.
struct MemChunk * next_chunk
Pointer to next allocated chunk.
int32 arraymax
The index of the lowest numbered bitmap in the array.
struct Int4Var Int4Var
Subtype of Object for storing simple int4 values.
ObjType type
This must have the value OBJ_BITMAP_HASH.
Object * obj
Pointer to the contents of the variable.
Subtype of Object for storing bitmap arrays.
The ShmemCtl structure is the first object allocated from the first chunk of shared memory in context...
ObjType type
This must have the value OBJ_INT4.
int32 min
Lower limit for range.
bool isnull
if true, the value is null
struct VarEntry VarEntry
A Veil variable.
struct BitmapRef BitmapRef
Subtype of Object for storing bitmap refs.