Veil
veil_shmem.h
Go to the documentation of this file.
1 /**
2  * @file veil_shmem.h
3  * \code
4  * Author: Marc Munro
5  * Copyright (c) 2005 - 2018 Marc Munro
6  * License: BSD
7  *
8  * \endcode
9  * @brief
10  * Define the basic veil shared memory structures
11  *
12  */
13 
14 #ifndef VEIL_DATATYPES
15 /** Prevent multiple definitions of the contents of this file.
16  */
17 #define VEIL_DATATYPES 1
18 
19 #include "utils/hsearch.h"
20 #include "storage/lwlock.h"
21 
22 /**
23  * Chunks od shared memory are allocated in multiples of this size.
24  */
25 #define CHUNK_SIZE 8192
26 
27 /**
28  * Limits the total amount of memory available for veil shared
29  * variables.
30  */
31 #define MAX_ALLOWED_SHMEM CHUNK_SIZE * 100
32 
33 
34 /**
35  * Chunks provide a linked list of dynamically allocated shared memory
36  * segments, with the most recently allocated chunk at the tail.
37  * Shmalloc allocates space from this list of chunks, creating new
38  * chunks as needed up to MAX_ALLOWED_SHMEM.
39  */
40 typedef struct MemChunk {
41  struct MemChunk *next_chunk; /**< Pointer to next allocated chunk */
42  size_t next; /**< Offset, within this chunk, of 1st
43  * free byte */
44  size_t limit; /**< Offset, of 1st byte beyond chunk */
45  void *memory[0]; /**< The rest of the chunk, from which
46  * memory is allocated */
47 } MemChunk;
48 
49 
50 /**
51  * MemContexts are large single chunks of shared memory from which
52  * smaller allocations may be made
53  */
54 typedef struct MemContext {
55  Oid db_id; /**< Identifier for the database for
56  * which this context was created,
57  * or by which it has been taken
58  * over. */
59  LWLock *lwlock; /**< The LWLock associated with this
60  * memory context */
61  size_t next; /**< Offset of 1st free byte */
62  size_t limit; /**< Offset, of 1st byte beyond this
63  * struct */
64  LWLockPadded *lwlock_tranche; /**< A tranche of lwlocks (only used in
65  * the zeroth MemContext. */
66  int lwlock_idx; /**< Index into the above. */
67  struct ShmemCtl *memctl; /**< Pointer to shared memory control
68  * structure. */
69  void *memory[0]; /**< The rest of the chunk, from which
70  * memory is allocated */
71 } MemContext;
72 
73 
74 
75 /**
76  * The key length for veil hash types.
77  */
78 #define HASH_KEYLEN 60
79 
80 
81 /**
82  * Describes the type of an Object record or one of its subtypes.
83  */
84 typedef enum {
85  OBJ_UNDEFINED = 0,
86  OBJ_SHMEMCTL,
87  OBJ_INT4,
88  OBJ_RANGE,
89  OBJ_BITMAP,
90  OBJ_BITMAP_ARRAY,
91  OBJ_BITMAP_HASH,
92  OBJ_BITMAP_REF,
93  OBJ_INT4_ARRAY
94 } ObjType;
95 
96 /**
97  * General purpose object-type. All veil variables are effectively
98  * sub-types of this.
99  */
100 typedef struct Object {
101  ObjType type;
102 } Object;
103 
104 /**
105  * The ShmemCtl structure is the first object allocated from the first
106  * chunk of shared memory in context 0. This object describes and
107  * manages shared memory allocated by shmalloc()
108  */
109 typedef struct ShmemCtl {
110  ObjType type; /**< This must have the value OBJ_SHMEMCTL */
111  bool initialised; /**< Set to true once struct is setup */
112  LWLockId veil_lwlock; /** dynamically allocated LWLock */
113  int current_context; /**< Index of the current context (0
114  * or 1) */
115  size_t total_allocated[2]; /**< Total shared memory allocated in
116  * chunks in each context */
117  bool switching; /**< Whether a context-switch is in
118  * progress */
119  MemContext *context[2]; /**< Array (pair) of contexts */
120  TransactionId xid[2]; /**< The transaction id of the
121  * transaction that initialised each
122  * context: this is used to determine
123  * whether there are transactions
124  * still runnning that may be using an
125  * earlier context. */
126 } ShmemCtl;
127 
128 /**
129  * Subtype of Object for storing simple int4 values. These values are
130  * allowed to be null.
131  */
132 typedef struct Int4Var {
133  ObjType type; /**< This must have the value OBJ_INT4 */
134  bool isnull; /**< if true, the value is null */
135  int32 value; /**< the integer value of the variable */
136 } Int4Var;
137 
138 /**
139  * Subtype of Object for storing range values. A range has an upper and
140  * lower bound, both stored as int4s. Nulls are not allowed.
141  */
142 typedef struct Range {
143  ObjType type; /**< This must have the value OBJ_RANGE */
144  int32 min;
145  int32 max;
146 } Range;
147 
148 /**
149  * Subtype of Object for storing bitmaps. A bitmap is stored as an
150  * array of int4 values. See veil_bitmap.c for more information. Note
151  * that the size of a Bitmap structure is determined dynamically at run
152  * time as the size of the array is only known then.
153  */
154 typedef struct Bitmap {
155  ObjType type; /**< This must have the value OBJ_BITMAP */
156  int32 bitzero; /**< The index of the lowest bit the bitmap can
157  * store */
158  int32 bitmax; /**< The index of the highest bit the bitmap can
159  * store */
160  uint32 bitset[0]; /**< Element zero of the array of int4 values
161  * comprising the bitmap. */
162 } Bitmap;
163 
164 /**
165  * Subtype of Object for storing bitmap refs. A bitmapref is like a
166  * bitmap but instead of containing a bitmap it contains a reference to
167  * one. This reference may be set during a transaction and then
168  * referenced only from within the setting transaction.
169  */
170 typedef struct BitmapRef {
171  ObjType type; /**< This must have the value OBJ_BITMAP_REF */
172  TransactionId xid; /**< The xid for which this variable is
173  * valid */
174  Bitmap *bitmap;
175 } BitmapRef;
176 
177 /**
178  * Subtype of Object for storing bitmap arrays. A bitmap array is
179  * simply an array of pointers to dynamically allocated Bitmaps. Note
180  * that the size of a Bitmap structure is determined dynamically at run
181  * time as the size of the array is only known then.
182  */
183 typedef struct BitmapArray { // subtype of Object
184  ObjType type; /**< This must have the value OBJ_BITMAP_ARRAY */
185  int32 bitzero; /**< The index of the lowest bit each bitmap can
186  * store */
187  int32 bitmax; /**< The index of the highest bit each bitmap can
188  * store */
189  int32 arrayzero; /**< The index of array element zero: the
190  * index of the lowest numbered bitmap in the
191  * array */
192  int32 arraymax; /**< The index of the lowest numbered bitmap in
193  * the array */
194  Bitmap *bitmap[0]; /** Element zero of the array of Bitmap pointers
195  * comprising the array. */
196 } BitmapArray;
197 
198 /**
199  * Subtype of Object for storing bitmap hashes. A bitmap hash is a hash
200  * of dynamically allocated bitmaps, keyed by strings. Note that these
201  * cannot be created as shared variables.
202  */
203 typedef struct BitmapHash {
204  ObjType type; /**< This must have the value OBJ_BITMAP_HASH */
205  int32 bitzero; /**< The index of the lowest bit each bitmap can
206  * store */
207  int32 bitmax; /**< The index of the highest bit each bitmap can
208  * store */
209  HTAB *hash; /**< Pointer to the (Postgresql dynahash) hash
210  * table */
211 } BitmapHash;
212 
213 
214 /**
215  * Subtype of Object for storing arrays of integers.
216  */
217 typedef struct Int4Array {
218  ObjType type; /**< This must have the value OBJ_INT4_ARRAY */
219  int32 arrayzero; /**< The index of array element zero: the
220  * index of the lowest numbered bitmap in the
221  * array */
222  int32 arraymax; /**< The index of the lowest numbered bitmap in
223  * the array */
224  int32 array[0]; /** Element zero of the array of integers */
225 } Int4Array;
226 
227 
228 /**
229  * A Veil variable. These may be session or shared variables, and may
230  * contain any Veil variable type. They are created and accessed by
231  * vl_lookup_shared_variable() and vl_lookup_variable(), and are stored
232  * in either the shared hash or one of the session hashes. See
233  * veil_shmem.c and veil_variables.c for more details.
234  */
235 typedef struct VarEntry {
236  char key[HASH_KEYLEN]; /**< String containing variable name */
237  bool shared; /**< Whether this is a shared variable */
238  Object *obj; /**< Pointer to the contents of the variable */
239 } VarEntry;
240 
241 
242 /**
243  * Describes a veil shared or session variable. This matches the SQL
244  * veil_variable_t which is defined as:
245 \verbatim
246 create type veil_variable_t as (
247  name text,
248  type text,
249  shared bool,
250 );
251 \endverbatim
252  */
253 typedef struct veil_variable_t {
254  char *name; /**< The name of the variable */
255  char *type; /**< The type of the variable (eg "Bitmap") */
256  bool shared; /**< Whether this is a shared variable (as
257  opposed to a session variable) */
259 
260 
261 #endif
262 
size_t next
Offset of 1st free byte.
Definition: veil_shmem.h:61
LWLockPadded * lwlock_tranche
A tranche of lwlocks (only used in the zeroth MemContext.
Definition: veil_shmem.h:64
struct Bitmap Bitmap
Subtype of Object for storing bitmaps.
Subtype of Object for storing arrays of integers.
Subtype of Object for storing bitmap refs.
Subtype of Object for storing bitmaps.
Subtype of Object for storing simple int4 values.
Describes a veil shared or session variable.
Chunks provide a linked list of dynamically allocated shared memory segments, with the most recently ...
Oid db_id
Identifier for the database for which this context was created, or by which it has been taken over...
Definition: veil_shmem.h:55
struct ShmemCtl ShmemCtl
The ShmemCtl structure is the first object allocated from the first chunk of shared memory in context...
struct MemContext MemContext
MemContexts are large single chunks of shared memory from which smaller allocations may be made...
struct Int4Array Int4Array
Subtype of Object for storing arrays of integers.
void * memory[0]
The rest of the chunk, from which memory is allocated.
#define HASH_KEYLEN
The key length for veil hash types.
Definition: veil_shmem.h:78
size_t limit
Offset, of 1st byte beyond chunk.
struct MemChunk MemChunk
Chunks provide a linked list of dynamically allocated shared memory segments, with the most recently ...
MemContexts are large single chunks of shared memory from which smaller allocations may be made...
Definition: veil_shmem.h:54
struct veil_variable_t veil_variable_t
Describes a veil shared or session variable.
struct Int4Var Int4Var
Subtype of Object for storing simple int4 values.
int lwlock_idx
Index into the above.
Definition: veil_shmem.h:66
General purpose object-type.
Subtype of Object for storing bitmap hashes.
struct VarEntry VarEntry
A Veil variable.
ObjType
Describes the type of an Object record or one of its subtypes.
struct ShmemCtl * memctl
Pointer to shared memory control structure.
Definition: veil_shmem.h:67
A Veil variable.
ObjType
Describes the type of an Object record or one of its subtypes.
Definition: veil_shmem.h:84
size_t next
Offset, within this chunk, of 1st free byte.
size_t limit
Offset, of 1st byte beyond this struct.
Definition: veil_shmem.h:62
Subtype of Object for storing range values.
struct Object Object
General purpose object-type.
struct MemChunk * next_chunk
Pointer to next allocated chunk.
Subtype of Object for storing bitmap arrays.
The ShmemCtl structure is the first object allocated from the first chunk of shared memory in context...
struct BitmapRef BitmapRef
Subtype of Object for storing bitmap refs.
struct BitmapArray BitmapArray
Subtype of Object for storing bitmap arrays.
LWLock * lwlock
The LWLock associated with this memory context.
Definition: veil_shmem.h:59
struct BitmapHash BitmapHash
Subtype of Object for storing bitmap hashes.
struct Range Range
Subtype of Object for storing range values.