00001 /** 00002 * @file veil_shmem.h 00003 * \code 00004 * Author: Marc Munro 00005 * Copyright (c) 2005 - 2011 Marc Munro 00006 * License: BSD 00007 * 00008 * \endcode 00009 * @brief 00010 * Define the basic veil shared memory structures 00011 * 00012 */ 00013 00014 #ifndef VEIL_DATATYPES 00015 /** Prevent multiple definitions of the contents of this file. 00016 */ 00017 #define VEIL_DATATYPES 1 00018 00019 #include "utils/hsearch.h" 00020 #include "storage/lwlock.h" 00021 00022 /** 00023 * Chunks od shared memory are allocated in multiples of this size. 00024 */ 00025 #define CHUNK_SIZE 8192 00026 00027 /** 00028 * Limits the total amount of memory available for veil shared 00029 * variables. 00030 */ 00031 #define MAX_ALLOWED_SHMEM CHUNK_SIZE * 100 00032 00033 00034 /** 00035 * Chunks provide a linked list of dynamically allocated shared memory 00036 * segments, with the most recently allocated chunk at the tail. 00037 * Shmalloc allocates space from this list of chunks, creating new 00038 * chunks as needed up to MAX_ALLOWED_SHMEM. 00039 */ 00040 typedef struct MemChunk { 00041 struct MemChunk *next_chunk; /**< Pointer to next allocated chunk */ 00042 size_t next; /**< Offset, within this chunk, of 1st 00043 * free byte */ 00044 size_t limit; /**< Offset, of 1st byte beyond chunk */ 00045 void *memory[0]; /**< The rest of the chunk, from which 00046 * memory is allocated */ 00047 } MemChunk; 00048 00049 00050 /** 00051 * MemContexts are large single chunks of shared memory from which 00052 * smaller allocations may be made 00053 */ 00054 typedef struct MemContext { 00055 Oid db_id; /**< Identifier for the database for 00056 * which this context was created, 00057 * or by which it has been taken 00058 * over. */ 00059 LWLockId lwlock; /**< The LWLock associated with this 00060 * memory context */ 00061 size_t next; /**< Offset of 1st free byte */ 00062 size_t limit; /**< Offset, of 1st byte beyond this 00063 * struct */ 00064 00065 struct ShmemCtl *memctl; /**< Pointer to shared memory control 00066 * structure. */ 00067 void *memory[0]; /**< The rest of the chunk, from which 00068 * memory is allocated */ 00069 } MemContext; 00070 00071 00072 00073 /** 00074 * The key length for veil hash types. 00075 */ 00076 #define HASH_KEYLEN 60 00077 00078 00079 /** 00080 * Describes the type of an Object record or one of its subtypes. 00081 */ 00082 typedef enum { 00083 OBJ_UNDEFINED = 0, 00084 OBJ_SHMEMCTL, 00085 OBJ_INT4, 00086 OBJ_RANGE, 00087 OBJ_BITMAP, 00088 OBJ_BITMAP_ARRAY, 00089 OBJ_BITMAP_HASH, 00090 OBJ_BITMAP_REF, 00091 OBJ_INT4_ARRAY 00092 } ObjType; 00093 00094 /** 00095 * General purpose object-type. All veil variables are effectively 00096 * sub-types of this. 00097 */ 00098 typedef struct Object { 00099 ObjType type; 00100 } Object; 00101 00102 /** 00103 * The ShmemCtl structure is the first object allocated from the first 00104 * chunk of shared memory in context 0. This object describes and 00105 * manages shared memory allocated by shmalloc() 00106 */ 00107 typedef struct ShmemCtl { 00108 ObjType type; /**< This must have the value OBJ_SHMEMCTL */ 00109 bool initialised; /**< Set to true once struct is setup */ 00110 LWLockId veil_lwlock; /** dynamically allocated LWLock */ 00111 int current_context; /**< Index of the current context (0 00112 * or 1) */ 00113 size_t total_allocated[2]; /**< Total shared memory allocated in 00114 * chunks in each context */ 00115 bool switching; /**< Whether a context-switch is in 00116 * progress */ 00117 MemContext *context[2]; /**< Array (pair) of contexts */ 00118 TransactionId xid[2]; /**< The transaction id of the 00119 * transaction that initialised each 00120 * context: this is used to determine 00121 * whether there are transactions 00122 * still runnning that may be using an 00123 * earlier context. */ 00124 } ShmemCtl; 00125 00126 /** 00127 * Subtype of Object for storing simple int4 values. These values are 00128 * allowed to be null. 00129 */ 00130 typedef struct Int4Var { 00131 ObjType type; /**< This must have the value OBJ_INT4 */ 00132 bool isnull; /**< if true, the value is null */ 00133 int32 value; /**< the integer value of the variable */ 00134 } Int4Var; 00135 00136 /** 00137 * Subtype of Object for storing range values. A range has an upper and 00138 * lower bound, both stored as int4s. Nulls are not allowed. 00139 */ 00140 typedef struct Range { 00141 ObjType type; /**< This must have the value OBJ_RANGE */ 00142 int32 min; 00143 int32 max; 00144 } Range; 00145 00146 /** 00147 * Subtype of Object for storing bitmaps. A bitmap is stored as an 00148 * array of int4 values. See veil_bitmap.c for more information. Note 00149 * that the size of a Bitmap structure is determined dynamically at run 00150 * time as the size of the array is only known then. 00151 */ 00152 typedef struct Bitmap { 00153 ObjType type; /**< This must have the value OBJ_BITMAP */ 00154 int32 bitzero; /**< The index of the lowest bit the bitmap can 00155 * store */ 00156 int32 bitmax; /**< The index of the highest bit the bitmap can 00157 * store */ 00158 uint32 bitset[0]; /**< Element zero of the array of int4 values 00159 * comprising the bitmap. */ 00160 } Bitmap; 00161 00162 /** 00163 * Subtype of Object for storing bitmap refs. A bitmapref is like a 00164 * bitmap but instead of containing a bitmap it contains a reference to 00165 * one. This reference may be set during a transaction and then 00166 * referenced only from within the setting transaction. 00167 */ 00168 typedef struct BitmapRef { 00169 ObjType type; /**< This must have the value OBJ_BITMAP_REF */ 00170 TransactionId xid; /**< The xid for which this variable is 00171 * valid */ 00172 Bitmap *bitmap; 00173 } BitmapRef; 00174 00175 /** 00176 * Subtype of Object for storing bitmap arrays. A bitmap array is 00177 * simply an array of pointers to dynamically allocated Bitmaps. Note 00178 * that the size of a Bitmap structure is determined dynamically at run 00179 * time as the size of the array is only known then. 00180 */ 00181 typedef struct BitmapArray { // subtype of Object 00182 ObjType type; /**< This must have the value OBJ_BITMAP_ARRAY */ 00183 int32 bitzero; /**< The index of the lowest bit each bitmap can 00184 * store */ 00185 int32 bitmax; /**< The index of the highest bit each bitmap can 00186 * store */ 00187 int32 arrayzero; /**< The index of array element zero: the 00188 * index of the lowest numbered bitmap in the 00189 * array */ 00190 int32 arraymax; /**< The index of the lowest numbered bitmap in 00191 * the array */ 00192 Bitmap *bitmap[0]; /** Element zero of the array of Bitmap pointers 00193 * comprising the array. */ 00194 } BitmapArray; 00195 00196 /** 00197 * Subtype of Object for storing bitmap hashes. A bitmap hash is a hash 00198 * of dynamically allocated bitmaps, keyed by strings. Note that these 00199 * cannot be created as shared variables. 00200 */ 00201 typedef struct BitmapHash { 00202 ObjType type; /**< This must have the value OBJ_BITMAP_HASH */ 00203 int32 bitzero; /**< The index of the lowest bit each bitmap can 00204 * store */ 00205 int32 bitmax; /**< The index of the highest bit each bitmap can 00206 * store */ 00207 HTAB *hash; /**< Pointer to the (Postgresql dynahash) hash 00208 * table */ 00209 } BitmapHash; 00210 00211 00212 /** 00213 * Subtype of Object for storing arrays of integers. 00214 */ 00215 typedef struct Int4Array { 00216 ObjType type; /**< This must have the value OBJ_INT4_ARRAY */ 00217 int32 arrayzero; /**< The index of array element zero: the 00218 * index of the lowest numbered bitmap in the 00219 * array */ 00220 int32 arraymax; /**< The index of the lowest numbered bitmap in 00221 * the array */ 00222 int32 array[0]; /** Element zero of the array of integers */ 00223 } Int4Array; 00224 00225 00226 /** 00227 * A Veil variable. These may be session or shared variables, and may 00228 * contain any Veil variable type. They are created and accessed by 00229 * vl_lookup_shared_variable() and vl_lookup_variable(), and are stored 00230 * in either the shared hash or one of the session hashes. See 00231 * veil_shmem.c and veil_variables.c for more details. 00232 */ 00233 typedef struct VarEntry { 00234 char key[HASH_KEYLEN]; /**< String containing variable name */ 00235 bool shared; /**< Whether this is a shared variable */ 00236 Object *obj; /**< Pointer to the contents of the variable */ 00237 } VarEntry; 00238 00239 00240 /** 00241 * Describes a veil shared or session variable. This matches the SQL 00242 * veil_variable_t which is defined as: 00243 \verbatim 00244 create type veil_variable_t as ( 00245 name text, 00246 type text, 00247 shared bool, 00248 ); 00249 \endverbatim 00250 */ 00251 typedef struct veil_variable_t { 00252 char *name; /**< The name of the variable */ 00253 char *type; /**< The type of the variable (eg "Bitmap") */ 00254 bool shared; /**< Whether this is a shared variable (as 00255 opposed to a session variable) */ 00256 } veil_variable_t; 00257 00258 00259 #endif 00260