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