blob: ede9c81582a69217edf6081a0104984a3c7d1153 [file] [log] [blame]
drhf5e7bb52008-02-18 14:47:33 +00001/*
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**
drhdfe88ec2008-11-03 20:55:06 +000015** 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.
drhf5e7bb52008-02-18 14:47:33 +000019** But sometimes (for example when during a DROP of a large table) most
drhdfe88ec2008-11-03 20:55:06 +000020** 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.
drhf5e7bb52008-02-18 14:47:33 +000023**
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.
drhf5e7bb52008-02-18 14:47:33 +000036*/
37#include "sqliteInt.h"
38
drh1feb7dd2008-11-19 18:30:29 +000039/* Size of the Bitvec structure in bytes. */
drhf6171e92010-08-05 11:56:01 +000040#define BITVEC_SZ 512
drh1feb7dd2008-11-19 18:30:29 +000041
mlcreechdda5b682008-03-14 13:02:08 +000042/* Round the union size down to the nearest pointer boundary, since that's how
43** it will be aligned within the Bitvec struct. */
drh62aaa6c2015-11-21 17:27:42 +000044#define BITVEC_USIZE \
45 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
drh1feb7dd2008-11-19 18:30:29 +000046
47/* Type of the array "element" for the bitmap representation.
48** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
49** Setting this to the "natural word" size of your CPU may improve
50** performance. */
51#define BITVEC_TELEM u8
52/* Size, in bits, of the bitmap element. */
53#define BITVEC_SZELEM 8
54/* Number of elements in a bitmap array. */
55#define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM))
56/* Number of bits in the bitmap array. */
57#define BITVEC_NBIT (BITVEC_NELEM*BITVEC_SZELEM)
58
59/* Number of u32 values in hash table. */
60#define BITVEC_NINT (BITVEC_USIZE/sizeof(u32))
61/* Maximum number of entries in hash table before
62** sub-dividing and re-hashing. */
drhf5e7bb52008-02-18 14:47:33 +000063#define BITVEC_MXHASH (BITVEC_NINT/2)
drh1feb7dd2008-11-19 18:30:29 +000064/* Hashing function for the aHash representation.
65** Empirical testing showed that the *37 multiplier
66** (an arbitrary prime)in the hash function provided
67** no fewer collisions than the no-op *1. */
68#define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
69
mlcreechdda5b682008-03-14 13:02:08 +000070#define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
drhf5e7bb52008-02-18 14:47:33 +000071
drhf5e7bb52008-02-18 14:47:33 +000072
73/*
74** A bitmap is an instance of the following structure.
75**
mistachkin48864df2013-03-21 21:20:32 +000076** This bitmap records the existence of zero or more bits
drhf5e7bb52008-02-18 14:47:33 +000077** with values between 1 and iSize, inclusive.
78**
79** There are three possible representations of the bitmap.
80** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
81** bitmap. The least significant bit is bit 1.
82**
83** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
84** a hash table that will hold up to BITVEC_MXHASH distinct values.
85**
86** Otherwise, the value i is redirected into one of BITVEC_NPTR
87** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
88** handles up to iDivisor separate values of i. apSub[0] holds
89** values between 1 and iDivisor. apSub[1] holds values between
90** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
91** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
92** to hold deal with values between 1 and iDivisor.
93*/
94struct Bitvec {
drh1feb7dd2008-11-19 18:30:29 +000095 u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
drh64f798d2009-04-01 23:49:04 +000096 u32 nSet; /* Number of bits that are set - only valid for aHash
97 ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512,
98 ** this would be 125. */
drh1feb7dd2008-11-19 18:30:29 +000099 u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
100 /* Should >=0 for apSub element. */
101 /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
102 /* For a BITVEC_SZ of 512, this would be 34,359,739. */
drhf5e7bb52008-02-18 14:47:33 +0000103 union {
drh1feb7dd2008-11-19 18:30:29 +0000104 BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */
drhf5e7bb52008-02-18 14:47:33 +0000105 u32 aHash[BITVEC_NINT]; /* Hash table representation */
106 Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */
107 } u;
108};
109
110/*
111** Create a new bitmap object able to handle bits between 0 and iSize,
112** inclusive. Return a pointer to the new object. Return NULL if
113** malloc fails.
114*/
115Bitvec *sqlite3BitvecCreate(u32 iSize){
116 Bitvec *p;
117 assert( sizeof(*p)==BITVEC_SZ );
118 p = sqlite3MallocZero( sizeof(*p) );
119 if( p ){
120 p->iSize = iSize;
121 }
122 return p;
123}
124
125/*
126** Check to see if the i-th bit is set. Return true or false.
127** If p is NULL (if the bitmap has not been created) or if
128** i is out of range, then return false.
129*/
drh82ef8772015-06-29 14:11:50 +0000130int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
131 assert( p!=0 );
drh1feb7dd2008-11-19 18:30:29 +0000132 i--;
drh234a93f2015-06-29 03:28:43 +0000133 if( i>=p->iSize ) return 0;
drh1feb7dd2008-11-19 18:30:29 +0000134 while( p->iDivisor ){
135 u32 bin = i/p->iDivisor;
136 i = i%p->iDivisor;
137 p = p->u.apSub[bin];
138 if (!p) {
139 return 0;
140 }
drhf5e7bb52008-02-18 14:47:33 +0000141 }
drh1feb7dd2008-11-19 18:30:29 +0000142 if( p->iSize<=BITVEC_NBIT ){
143 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
144 } else{
145 u32 h = BITVEC_HASH(i++);
drhf5e7bb52008-02-18 14:47:33 +0000146 while( p->u.aHash[h] ){
147 if( p->u.aHash[h]==i ) return 1;
drh7ee27b02009-07-25 17:33:25 +0000148 h = (h+1) % BITVEC_NINT;
drhf5e7bb52008-02-18 14:47:33 +0000149 }
150 return 0;
151 }
152}
drh82ef8772015-06-29 14:11:50 +0000153int sqlite3BitvecTest(Bitvec *p, u32 i){
154 return p!=0 && sqlite3BitvecTestNotNull(p,i);
155}
drhf5e7bb52008-02-18 14:47:33 +0000156
157/*
158** Set the i-th bit. Return 0 on success and an error code if
159** anything goes wrong.
drhdfe88ec2008-11-03 20:55:06 +0000160**
161** This routine might cause sub-bitmaps to be allocated. Failing
162** to get the memory needed to hold the sub-bitmap is the only
163** that can go wrong with an insert, assuming p and i are valid.
164**
165** The calling function must ensure that p is a valid Bitvec object
166** and that the value for "i" is within range of the Bitvec object.
167** Otherwise the behavior is undefined.
drhf5e7bb52008-02-18 14:47:33 +0000168*/
169int sqlite3BitvecSet(Bitvec *p, u32 i){
170 u32 h;
drh6aac11d2009-07-18 20:01:37 +0000171 if( p==0 ) return SQLITE_OK;
drh3088d592008-03-21 16:45:47 +0000172 assert( i>0 );
dane3c3be82017-05-25 21:02:00 +0000173 assert( i<=p->iSize );
dane3b047b2017-05-26 18:18:51 +0000174 if( i>p->iSize || i==0 ){
dan55b36d52017-05-19 19:57:15 +0000175 sqlite3_log(SQLITE_ERROR,
176 "Bitvec: setting bit %d of bitvec size %d\n", (int)i, (int)p->iSize
177 );
drhcb026712017-12-11 14:02:10 +0000178 abort();
dan55b36d52017-05-19 19:57:15 +0000179 }
drh1feb7dd2008-11-19 18:30:29 +0000180 i--;
181 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
182 u32 bin = i/p->iDivisor;
183 i = i%p->iDivisor;
drhf5e7bb52008-02-18 14:47:33 +0000184 if( p->u.apSub[bin]==0 ){
drhf5e7bb52008-02-18 14:47:33 +0000185 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
mistachkinfad30392016-02-13 23:43:46 +0000186 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
drhf5e7bb52008-02-18 14:47:33 +0000187 }
drh1feb7dd2008-11-19 18:30:29 +0000188 p = p->u.apSub[bin];
drhf5e7bb52008-02-18 14:47:33 +0000189 }
drh1feb7dd2008-11-19 18:30:29 +0000190 if( p->iSize<=BITVEC_NBIT ){
191 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
192 return SQLITE_OK;
193 }
194 h = BITVEC_HASH(i++);
195 /* if there wasn't a hash collision, and this doesn't */
196 /* completely fill the hash, then just add it without */
197 /* worring about sub-dividing and re-hashing. */
198 if( !p->u.aHash[h] ){
199 if (p->nSet<(BITVEC_NINT-1)) {
200 goto bitvec_set_end;
201 } else {
202 goto bitvec_set_rehash;
203 }
204 }
205 /* there was a collision, check to see if it's already */
206 /* in hash, if not, try to find a spot for it */
207 do {
drhf5e7bb52008-02-18 14:47:33 +0000208 if( p->u.aHash[h]==i ) return SQLITE_OK;
209 h++;
drh1feb7dd2008-11-19 18:30:29 +0000210 if( h>=BITVEC_NINT ) h = 0;
211 } while( p->u.aHash[h] );
212 /* we didn't find it in the hash. h points to the first */
213 /* available free spot. check to see if this is going to */
214 /* make our hash too "full". */
215bitvec_set_rehash:
drhf5e7bb52008-02-18 14:47:33 +0000216 if( p->nSet>=BITVEC_MXHASH ){
drh86a7a692008-11-11 15:48:48 +0000217 unsigned int j;
218 int rc;
drhe98c9042009-06-02 21:31:38 +0000219 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
220 if( aiValues==0 ){
mistachkinfad30392016-02-13 23:43:46 +0000221 return SQLITE_NOMEM_BKPT;
drhe98c9042009-06-02 21:31:38 +0000222 }else{
223 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
224 memset(p->u.apSub, 0, sizeof(p->u.apSub));
225 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
226 rc = sqlite3BitvecSet(p, i);
227 for(j=0; j<BITVEC_NINT; j++){
228 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
229 }
230 sqlite3StackFree(0, aiValues);
231 return rc;
drhf5e7bb52008-02-18 14:47:33 +0000232 }
drhf5e7bb52008-02-18 14:47:33 +0000233 }
drh1feb7dd2008-11-19 18:30:29 +0000234bitvec_set_end:
235 p->nSet++;
drhf5e7bb52008-02-18 14:47:33 +0000236 p->u.aHash[h] = i;
237 return SQLITE_OK;
238}
239
240/*
drh1feb7dd2008-11-19 18:30:29 +0000241** Clear the i-th bit.
drhe98c9042009-06-02 21:31:38 +0000242**
243** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
244** that BitvecClear can use to rebuilt its hash table.
drhf5e7bb52008-02-18 14:47:33 +0000245*/
drhe98c9042009-06-02 21:31:38 +0000246void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
drh6aac11d2009-07-18 20:01:37 +0000247 if( p==0 ) return;
drh3088d592008-03-21 16:45:47 +0000248 assert( i>0 );
drh1feb7dd2008-11-19 18:30:29 +0000249 i--;
250 while( p->iDivisor ){
251 u32 bin = i/p->iDivisor;
252 i = i%p->iDivisor;
253 p = p->u.apSub[bin];
254 if (!p) {
255 return;
drhf5e7bb52008-02-18 14:47:33 +0000256 }
drh1feb7dd2008-11-19 18:30:29 +0000257 }
258 if( p->iSize<=BITVEC_NBIT ){
259 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
drhf5e7bb52008-02-18 14:47:33 +0000260 }else{
drh86a7a692008-11-11 15:48:48 +0000261 unsigned int j;
drhe98c9042009-06-02 21:31:38 +0000262 u32 *aiValues = pBuf;
263 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
264 memset(p->u.aHash, 0, sizeof(p->u.aHash));
drhf5e7bb52008-02-18 14:47:33 +0000265 p->nSet = 0;
266 for(j=0; j<BITVEC_NINT; j++){
drh1feb7dd2008-11-19 18:30:29 +0000267 if( aiValues[j] && aiValues[j]!=(i+1) ){
268 u32 h = BITVEC_HASH(aiValues[j]-1);
269 p->nSet++;
270 while( p->u.aHash[h] ){
271 h++;
272 if( h>=BITVEC_NINT ) h = 0;
273 }
274 p->u.aHash[h] = aiValues[j];
drh3088d592008-03-21 16:45:47 +0000275 }
drhf5e7bb52008-02-18 14:47:33 +0000276 }
277 }
278}
279
280/*
281** Destroy a bitmap object. Reclaim all memory used.
282*/
283void sqlite3BitvecDestroy(Bitvec *p){
284 if( p==0 ) return;
285 if( p->iDivisor ){
drh86a7a692008-11-11 15:48:48 +0000286 unsigned int i;
drhf5e7bb52008-02-18 14:47:33 +0000287 for(i=0; i<BITVEC_NPTR; i++){
288 sqlite3BitvecDestroy(p->u.apSub[i]);
289 }
290 }
291 sqlite3_free(p);
292}
drh3088d592008-03-21 16:45:47 +0000293
danielk1977bea2a942009-01-20 17:06:27 +0000294/*
295** Return the value of the iSize parameter specified when Bitvec *p
296** was created.
297*/
298u32 sqlite3BitvecSize(Bitvec *p){
299 return p->iSize;
300}
301
dan43f2b822020-03-09 21:00:13 +0000302static int bitvecCount(Bitvec *p){
303 int nRet = 0;
304 if( p ){
305 if( p->iDivisor ){
306 int i;
307 for(i=0; i<BITVEC_NPTR; i++){
308 nRet += bitvecCount(p->u.apSub[i]);
309 }
310 }else if( p->iSize<=BITVEC_NBIT ){
311 int i;
312 for(i=0; i<BITVEC_NELEM; i++){
313 BITVEC_TELEM x = p->u.aBitmap[i];
314 assert( sizeof(x)==1 );
315 if( x & 0x80 ) nRet++;
316 if( x & 0x40 ) nRet++;
317 if( x & 0x20 ) nRet++;
318 if( x & 0x10 ) nRet++;
319 if( x & 0x08 ) nRet++;
320 if( x & 0x04 ) nRet++;
321 if( x & 0x02 ) nRet++;
322 if( x & 0x01 ) nRet++;
323 }
324 }else{
325 nRet += p->nSet;
326 }
327 }
328 return nRet;
329}
330
331static int bitvecArray(Bitvec *p, u32 *aElem, u32 iOff){
332 int nRet = 0;
333 if( p ){
334 if( p->iDivisor ){
335 int i;
336 for(i=0; i<BITVEC_NPTR; i++){
337 nRet += bitvecArray(p->u.apSub[i], &aElem[nRet], i*p->iDivisor + iOff);
338 }
339 }else if( p->iSize<=BITVEC_NBIT ){
340 int i;
341 for(i=0; i<BITVEC_NELEM; i++){
342 BITVEC_TELEM x = p->u.aBitmap[i];
343 assert( sizeof(x)==1 );
dan76d15522020-03-10 21:14:55 +0000344 if( x & 0x01 ){ aElem[nRet++] = iOff + i*8 + 0; }
345 if( x & 0x02 ){ aElem[nRet++] = iOff + i*8 + 1; }
346 if( x & 0x04 ){ aElem[nRet++] = iOff + i*8 + 2; }
347 if( x & 0x08 ){ aElem[nRet++] = iOff + i*8 + 3; }
348 if( x & 0x10 ){ aElem[nRet++] = iOff + i*8 + 4; }
349 if( x & 0x20 ){ aElem[nRet++] = iOff + i*8 + 5; }
350 if( x & 0x40 ){ aElem[nRet++] = iOff + i*8 + 6; }
351 if( x & 0x80 ){ aElem[nRet++] = iOff + i*8 + 7; }
dan43f2b822020-03-09 21:00:13 +0000352 }
353 }else{
354 int i;
355 for(i=0; i<BITVEC_NINT; i++){
356 if( p->u.aHash[i] ){
357 aElem[nRet++] = p->u.aHash[i]+iOff-1;
358 }
359 }
360 assert( nRet==p->nSet );
361 }
362 }
363 return nRet;
364}
365
366int sqlite3BitvecArray(Bitvec *p, u32 **pa, int *pn){
367 int nElem;
368 u32 *aElem;
369 nElem = bitvecCount(p);
370 aElem = sqlite3MallocZero(nElem*sizeof(u32));
371 if( aElem==0 ) return SQLITE_NOMEM;
372 bitvecArray(p, aElem, 1);
373 *pa = aElem;
374 *pn = nElem;
375 return SQLITE_OK;
376}
377
drhd12602a2016-12-07 15:49:02 +0000378#ifndef SQLITE_UNTESTABLE
drh3088d592008-03-21 16:45:47 +0000379/*
380** Let V[] be an array of unsigned characters sufficient to hold
381** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
382** Then the following macros can be used to set, clear, or test
383** individual bits within V.
384*/
385#define SETBIT(V,I) V[I>>3] |= (1<<(I&7))
386#define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7))
387#define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0
388
389/*
390** This routine runs an extensive test of the Bitvec code.
391**
392** The input is an array of integers that acts as a program
393** to test the Bitvec. The integers are opcodes followed
394** by 0, 1, or 3 operands, depending on the opcode. Another
395** opcode follows immediately after the last operand.
396**
397** There are 6 opcodes numbered from 0 through 5. 0 is the
398** "halt" opcode and causes the test to end.
399**
400** 0 Halt and return the number of errors
401** 1 N S X Set N bits beginning with S and incrementing by X
402** 2 N S X Clear N bits beginning with S and incrementing by X
403** 3 N Set N randomly chosen bits
404** 4 N Clear N randomly chosen bits
405** 5 N S X Set N bits from S increment X in array only, not in bitvec
406**
407** The opcodes 1 through 4 perform set and clear operations are performed
408** on both a Bitvec object and on a linear array of bits obtained from malloc.
409** Opcode 5 works on the linear array only, not on the Bitvec.
410** Opcode 5 is used to deliberately induce a fault in order to
411** confirm that error detection works.
412**
413** At the conclusion of the test the linear array is compared
414** against the Bitvec object. If there are any differences,
415** an error is returned. If they are the same, zero is returned.
416**
417** If a memory allocation error occurs, return -1.
418*/
419int sqlite3BitvecBuiltinTest(int sz, int *aOp){
420 Bitvec *pBitvec = 0;
421 unsigned char *pV = 0;
422 int rc = -1;
423 int i, nx, pc, op;
drhe98c9042009-06-02 21:31:38 +0000424 void *pTmpSpace;
drh3088d592008-03-21 16:45:47 +0000425
426 /* Allocate the Bitvec to be tested and a linear array of
427 ** bits to act as the reference */
428 pBitvec = sqlite3BitvecCreate( sz );
dan6809c962012-07-30 14:53:54 +0000429 pV = sqlite3MallocZero( (sz+7)/8 + 1 );
drhf3cdcdc2015-04-29 16:50:28 +0000430 pTmpSpace = sqlite3_malloc64(BITVEC_SZ);
drhe98c9042009-06-02 21:31:38 +0000431 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end;
drh3088d592008-03-21 16:45:47 +0000432
drh6aac11d2009-07-18 20:01:37 +0000433 /* NULL pBitvec tests */
434 sqlite3BitvecSet(0, 1);
435 sqlite3BitvecClear(0, 1, pTmpSpace);
436
drh3088d592008-03-21 16:45:47 +0000437 /* Run the program */
438 pc = 0;
439 while( (op = aOp[pc])!=0 ){
440 switch( op ){
441 case 1:
442 case 2:
443 case 5: {
444 nx = 4;
445 i = aOp[pc+2] - 1;
446 aOp[pc+2] += aOp[pc+3];
447 break;
448 }
449 case 3:
450 case 4:
451 default: {
452 nx = 2;
453 sqlite3_randomness(sizeof(i), &i);
454 break;
455 }
456 }
457 if( (--aOp[pc+1]) > 0 ) nx = 0;
458 pc += nx;
459 i = (i & 0x7fffffff)%sz;
460 if( (op & 1)!=0 ){
461 SETBIT(pV, (i+1));
462 if( op!=5 ){
463 if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
464 }
465 }else{
466 CLEARBIT(pV, (i+1));
drhe98c9042009-06-02 21:31:38 +0000467 sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
drh3088d592008-03-21 16:45:47 +0000468 }
469 }
470
471 /* Test to make sure the linear array exactly matches the
472 ** Bitvec object. Start with the assumption that they do
473 ** match (rc==0). Change rc to non-zero if a discrepancy
474 ** is found.
475 */
476 rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
drh64f798d2009-04-01 23:49:04 +0000477 + sqlite3BitvecTest(pBitvec, 0)
478 + (sqlite3BitvecSize(pBitvec) - sz);
drh3088d592008-03-21 16:45:47 +0000479 for(i=1; i<=sz; i++){
480 if( (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
481 rc = i;
482 break;
483 }
484 }
485
486 /* Free allocated structure */
487bitvec_end:
drhe98c9042009-06-02 21:31:38 +0000488 sqlite3_free(pTmpSpace);
drh3088d592008-03-21 16:45:47 +0000489 sqlite3_free(pV);
490 sqlite3BitvecDestroy(pBitvec);
491 return rc;
492}
drhd12602a2016-12-07 15:49:02 +0000493#endif /* SQLITE_UNTESTABLE */