1 /* 2 ** 2008 February 16 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** This file implements an object that represents a fixed-length 13 ** bitmap. Bits are numbered starting with 1. 14 ** 15 ** A bitmap is used to record which pages of a database file have been 16 ** journalled during a transaction, or which pages have the "dont-write" 17 ** property. Usually only a few pages are meet either condition. 18 ** So the bitmap is usually sparse and has low cardinality. 19 ** But sometimes (for example when during a DROP of a large table) most 20 ** or all of the pages in a database can get journalled. In those cases, 21 ** the bitmap becomes dense with high cardinality. The algorithm needs 22 ** to handle both cases well. 23 ** 24 ** The size of the bitmap is fixed when the object is created. 25 ** 26 ** All bits are clear when the bitmap is created. Individual bits 27 ** may be set or cleared one at a time. 28 ** 29 ** Test operations are about 100 times more common that set operations. 30 ** Clear operations are exceedingly rare. There are usually between 31 ** 5 and 500 set operations per Bitvec object, though the number of sets can 32 ** sometimes grow into tens of thousands or larger. The size of the 33 ** Bitvec object is the number of pages in the database file at the 34 ** start of a transaction, and is thus usually less than a few thousand, 35 ** but can be as large as 2 billion for a really big database. 36 ** 37 ** @(#) $Id: bitvec.c,v 1.8 2008/11/11 15:48:48 drh Exp $ 38 */ 39 #include "sqliteInt.h" 40 41 #define BITVEC_SZ 512 42 /* Round the union size down to the nearest pointer boundary, since that's how 43 ** it will be aligned within the Bitvec struct. */ 44 #define BITVEC_USIZE (((BITVEC_SZ-12)/sizeof(Bitvec*))*sizeof(Bitvec*)) 45 #define BITVEC_NCHAR BITVEC_USIZE 46 #define BITVEC_NBIT (BITVEC_NCHAR*8) 47 #define BITVEC_NINT (BITVEC_USIZE/4) 48 #define BITVEC_MXHASH (BITVEC_NINT/2) 49 #define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *)) 50 51 #define BITVEC_HASH(X) (((X)*37)%BITVEC_NINT) 52 53 /* 54 ** A bitmap is an instance of the following structure. 55 ** 56 ** This bitmap records the existance of zero or more bits 57 ** with values between 1 and iSize, inclusive. 58 ** 59 ** There are three possible representations of the bitmap. 60 ** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight 61 ** bitmap. The least significant bit is bit 1. 62 ** 63 ** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is 64 ** a hash table that will hold up to BITVEC_MXHASH distinct values. 65 ** 66 ** Otherwise, the value i is redirected into one of BITVEC_NPTR 67 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap 68 ** handles up to iDivisor separate values of i. apSub[0] holds 69 ** values between 1 and iDivisor. apSub[1] holds values between 70 ** iDivisor+1 and 2*iDivisor. apSub[N] holds values between 71 ** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized 72 ** to hold deal with values between 1 and iDivisor. 73 */ 74 struct Bitvec { 75 u32 iSize; /* Maximum bit index */ 76 u32 nSet; /* Number of bits that are set */ 77 u32 iDivisor; /* Number of bits handled by each apSub[] entry */ 78 union { 79 u8 aBitmap[BITVEC_NCHAR]; /* Bitmap representation */ 80 u32 aHash[BITVEC_NINT]; /* Hash table representation */ 81 Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */ 82 } u; 83 }; 84 85 /* 86 ** Create a new bitmap object able to handle bits between 0 and iSize, 87 ** inclusive. Return a pointer to the new object. Return NULL if 88 ** malloc fails. 89 */ 90 Bitvec *sqlite3BitvecCreate(u32 iSize){ 91 Bitvec *p; 92 assert( sizeof(*p)==BITVEC_SZ ); 93 p = sqlite3MallocZero( sizeof(*p) ); 94 if( p ){ 95 p->iSize = iSize; 96 } 97 return p; 98 } 99 100 /* 101 ** Check to see if the i-th bit is set. Return true or false. 102 ** If p is NULL (if the bitmap has not been created) or if 103 ** i is out of range, then return false. 104 */ 105 int sqlite3BitvecTest(Bitvec *p, u32 i){ 106 if( p==0 ) return 0; 107 if( i>p->iSize || i==0 ) return 0; 108 if( p->iSize<=BITVEC_NBIT ){ 109 i--; 110 return (p->u.aBitmap[i/8] & (1<<(i&7)))!=0; 111 } 112 if( p->iDivisor>0 ){ 113 u32 bin = (i-1)/p->iDivisor; 114 i = (i-1)%p->iDivisor + 1; 115 return sqlite3BitvecTest(p->u.apSub[bin], i); 116 }else{ 117 u32 h = BITVEC_HASH(i); 118 while( p->u.aHash[h] ){ 119 if( p->u.aHash[h]==i ) return 1; 120 h++; 121 if( h>=BITVEC_NINT ) h = 0; 122 } 123 return 0; 124 } 125 } 126 127 /* 128 ** Set the i-th bit. Return 0 on success and an error code if 129 ** anything goes wrong. 130 ** 131 ** This routine might cause sub-bitmaps to be allocated. Failing 132 ** to get the memory needed to hold the sub-bitmap is the only 133 ** that can go wrong with an insert, assuming p and i are valid. 134 ** 135 ** The calling function must ensure that p is a valid Bitvec object 136 ** and that the value for "i" is within range of the Bitvec object. 137 ** Otherwise the behavior is undefined. 138 */ 139 int sqlite3BitvecSet(Bitvec *p, u32 i){ 140 u32 h; 141 assert( p!=0 ); 142 assert( i>0 ); 143 assert( i<=p->iSize ); 144 if( p->iSize<=BITVEC_NBIT ){ 145 i--; 146 p->u.aBitmap[i/8] |= 1 << (i&7); 147 return SQLITE_OK; 148 } 149 if( p->iDivisor ){ 150 u32 bin = (i-1)/p->iDivisor; 151 i = (i-1)%p->iDivisor + 1; 152 if( p->u.apSub[bin]==0 ){ 153 sqlite3BeginBenignMalloc(); 154 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor ); 155 sqlite3EndBenignMalloc(); 156 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM; 157 } 158 return sqlite3BitvecSet(p->u.apSub[bin], i); 159 } 160 h = BITVEC_HASH(i); 161 while( p->u.aHash[h] ){ 162 if( p->u.aHash[h]==i ) return SQLITE_OK; 163 h++; 164 if( h==BITVEC_NINT ) h = 0; 165 } 166 p->nSet++; 167 if( p->nSet>=BITVEC_MXHASH ){ 168 unsigned int j; 169 int rc; 170 u32 aiValues[BITVEC_NINT]; 171 memcpy(aiValues, p->u.aHash, sizeof(aiValues)); 172 memset(p->u.apSub, 0, sizeof(p->u.apSub[0])*BITVEC_NPTR); 173 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR; 174 rc = sqlite3BitvecSet(p, i); 175 for(j=0; j<BITVEC_NINT; j++){ 176 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]); 177 } 178 return rc; 179 } 180 p->u.aHash[h] = i; 181 return SQLITE_OK; 182 } 183 184 /* 185 ** Clear the i-th bit. Return 0 on success and an error code if 186 ** anything goes wrong. 187 */ 188 void sqlite3BitvecClear(Bitvec *p, u32 i){ 189 assert( p!=0 ); 190 assert( i>0 ); 191 if( p->iSize<=BITVEC_NBIT ){ 192 i--; 193 p->u.aBitmap[i/8] &= ~(1 << (i&7)); 194 }else if( p->iDivisor ){ 195 u32 bin = (i-1)/p->iDivisor; 196 i = (i-1)%p->iDivisor + 1; 197 if( p->u.apSub[bin] ){ 198 sqlite3BitvecClear(p->u.apSub[bin], i); 199 } 200 }else{ 201 unsigned int j; 202 u32 aiValues[BITVEC_NINT]; 203 memcpy(aiValues, p->u.aHash, sizeof(aiValues)); 204 memset(p->u.aHash, 0, sizeof(p->u.aHash[0])*BITVEC_NINT); 205 p->nSet = 0; 206 for(j=0; j<BITVEC_NINT; j++){ 207 if( aiValues[j] && aiValues[j]!=i ){ 208 sqlite3BitvecSet(p, aiValues[j]); 209 } 210 } 211 } 212 } 213 214 /* 215 ** Destroy a bitmap object. Reclaim all memory used. 216 */ 217 void sqlite3BitvecDestroy(Bitvec *p){ 218 if( p==0 ) return; 219 if( p->iDivisor ){ 220 unsigned int i; 221 for(i=0; i<BITVEC_NPTR; i++){ 222 sqlite3BitvecDestroy(p->u.apSub[i]); 223 } 224 } 225 sqlite3_free(p); 226 } 227 228 #ifndef SQLITE_OMIT_BUILTIN_TEST 229 /* 230 ** Let V[] be an array of unsigned characters sufficient to hold 231 ** up to N bits. Let I be an integer between 0 and N. 0<=I<N. 232 ** Then the following macros can be used to set, clear, or test 233 ** individual bits within V. 234 */ 235 #define SETBIT(V,I) V[I>>3] |= (1<<(I&7)) 236 #define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7)) 237 #define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0 238 239 /* 240 ** This routine runs an extensive test of the Bitvec code. 241 ** 242 ** The input is an array of integers that acts as a program 243 ** to test the Bitvec. The integers are opcodes followed 244 ** by 0, 1, or 3 operands, depending on the opcode. Another 245 ** opcode follows immediately after the last operand. 246 ** 247 ** There are 6 opcodes numbered from 0 through 5. 0 is the 248 ** "halt" opcode and causes the test to end. 249 ** 250 ** 0 Halt and return the number of errors 251 ** 1 N S X Set N bits beginning with S and incrementing by X 252 ** 2 N S X Clear N bits beginning with S and incrementing by X 253 ** 3 N Set N randomly chosen bits 254 ** 4 N Clear N randomly chosen bits 255 ** 5 N S X Set N bits from S increment X in array only, not in bitvec 256 ** 257 ** The opcodes 1 through 4 perform set and clear operations are performed 258 ** on both a Bitvec object and on a linear array of bits obtained from malloc. 259 ** Opcode 5 works on the linear array only, not on the Bitvec. 260 ** Opcode 5 is used to deliberately induce a fault in order to 261 ** confirm that error detection works. 262 ** 263 ** At the conclusion of the test the linear array is compared 264 ** against the Bitvec object. If there are any differences, 265 ** an error is returned. If they are the same, zero is returned. 266 ** 267 ** If a memory allocation error occurs, return -1. 268 */ 269 int sqlite3BitvecBuiltinTest(int sz, int *aOp){ 270 Bitvec *pBitvec = 0; 271 unsigned char *pV = 0; 272 int rc = -1; 273 int i, nx, pc, op; 274 275 /* Allocate the Bitvec to be tested and a linear array of 276 ** bits to act as the reference */ 277 pBitvec = sqlite3BitvecCreate( sz ); 278 pV = sqlite3_malloc( (sz+7)/8 + 1 ); 279 if( pBitvec==0 || pV==0 ) goto bitvec_end; 280 memset(pV, 0, (sz+7)/8 + 1); 281 282 /* Run the program */ 283 pc = 0; 284 while( (op = aOp[pc])!=0 ){ 285 switch( op ){ 286 case 1: 287 case 2: 288 case 5: { 289 nx = 4; 290 i = aOp[pc+2] - 1; 291 aOp[pc+2] += aOp[pc+3]; 292 break; 293 } 294 case 3: 295 case 4: 296 default: { 297 nx = 2; 298 sqlite3_randomness(sizeof(i), &i); 299 break; 300 } 301 } 302 if( (--aOp[pc+1]) > 0 ) nx = 0; 303 pc += nx; 304 i = (i & 0x7fffffff)%sz; 305 if( (op & 1)!=0 ){ 306 SETBIT(pV, (i+1)); 307 if( op!=5 ){ 308 if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end; 309 } 310 }else{ 311 CLEARBIT(pV, (i+1)); 312 sqlite3BitvecClear(pBitvec, i+1); 313 } 314 } 315 316 /* Test to make sure the linear array exactly matches the 317 ** Bitvec object. Start with the assumption that they do 318 ** match (rc==0). Change rc to non-zero if a discrepancy 319 ** is found. 320 */ 321 rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1) 322 + sqlite3BitvecTest(pBitvec, 0); 323 for(i=1; i<=sz; i++){ 324 if( (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){ 325 rc = i; 326 break; 327 } 328 } 329 330 /* Free allocated structure */ 331 bitvec_end: 332 sqlite3_free(pV); 333 sqlite3BitvecDestroy(pBitvec); 334 return rc; 335 } 336 #endif /* SQLITE_OMIT_BUILTIN_TEST */ 337