-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbitvec.c
222 lines (209 loc) · 5.26 KB
/
bitvec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Symisc unQLite: An Embeddable NoSQL (Post Modern) Database Engine.
* Copyright (C) 2012-2013, Symisc Systems http://unqlite.org/
* Version 1.1.6
* For information on licensing, redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES
* please contact Symisc Systems via:
* or visit:
* http://unqlite.org/licensing.html
*/
/* $SymiscID: bitvec.c v1.0 Win7 2013-02-27 15:16 stable <[email protected]> $ */
#ifndef UNQLITE_AMALGAMATION
#include "unqliteInt.h"
#endif
/** This file implements an object that represents a dynmaic
** bitmap.
**
** A bitmap is used to record which pages of a database file have been
** journalled during a transaction, or which pages have the "dont-write"
** property. Usually only a few pages are meet either condition.
** So the bitmap is usually sparse and has low cardinality.
*/
/*
* Actually, this is not a bitmap but a simple hashtable where page
* number (64-bit unsigned integers) are used as the lookup keys.
*/
typedef struct bitvec_rec bitvec_rec;
struct bitvec_rec
{
pgno iPage; /* Page number */
bitvec_rec *pNext,*pNextCol; /* Collison link */
};
struct Bitvec
{
SyMemBackend *pAlloc; /* Memory allocator */
sxu32 nRec; /* Total number of records */
sxu32 nSize; /* Table size */
bitvec_rec **apRec; /* Record table */
bitvec_rec *pList; /* List of records */
};
/*
* Allocate a new bitvec instance.
*/
UNQLITE_PRIVATE Bitvec * unqliteBitvecCreate(SyMemBackend *pAlloc,pgno iSize)
{
bitvec_rec **apNew;
Bitvec *p;
p = (Bitvec *)SyMemBackendAlloc(pAlloc,sizeof(*p) );
if( p == 0 ){
SXUNUSED(iSize); /* cc warning */
return 0;
}
/* Zero the structure */
SyZero(p,sizeof(Bitvec));
/* Allocate a new table */
p->nSize = 64; /* Must be a power of two */
apNew = (bitvec_rec **)SyMemBackendAlloc(pAlloc,p->nSize * sizeof(bitvec_rec *));
if( apNew == 0 ){
SyMemBackendFree(pAlloc,p);
return 0;
}
/* Zero the new table */
SyZero((void *)apNew,p->nSize * sizeof(bitvec_rec *));
/* Fill-in */
p->apRec = apNew;
p->pAlloc = pAlloc;
return p;
}
/*
* Check if the given page number is already installed in the table.
* Return true if installed. False otherwise.
*/
UNQLITE_PRIVATE int unqliteBitvecTest(Bitvec *p,pgno i)
{
bitvec_rec *pRec;
/* Point to the desired bucket */
pRec = p->apRec[i & (p->nSize - 1)];
for(;;){
if( pRec == 0 ){ break; }
if( pRec->iPage == i ){
/* Page found */
return 1;
}
/* Point to the next entry */
pRec = pRec->pNextCol;
if( pRec == 0 ){ break; }
if( pRec->iPage == i ){
/* Page found */
return 1;
}
/* Point to the next entry */
pRec = pRec->pNextCol;
if( pRec == 0 ){ break; }
if( pRec->iPage == i ){
/* Page found */
return 1;
}
/* Point to the next entry */
pRec = pRec->pNextCol;
if( pRec == 0 ){ break; }
if( pRec->iPage == i ){
/* Page found */
return 1;
}
/* Point to the next entry */
pRec = pRec->pNextCol;
}
/* No such entry */
return 0;
}
/*
* Install a given page number in our bitmap (Actually, our hashtable).
*/
UNQLITE_PRIVATE int unqliteBitvecSet(Bitvec *p,pgno i)
{
bitvec_rec *pRec;
sxi32 iBuck;
/* Allocate a new instance */
pRec = (bitvec_rec *)SyMemBackendPoolAlloc(p->pAlloc,sizeof(bitvec_rec));
if( pRec == 0 ){
return UNQLITE_NOMEM;
}
/* Zero the structure */
SyZero(pRec,sizeof(bitvec_rec));
/* Fill-in */
pRec->iPage = i;
iBuck = i & (p->nSize - 1);
pRec->pNextCol = p->apRec[iBuck];
p->apRec[iBuck] = pRec;
pRec->pNext = p->pList;
p->pList = pRec;
p->nRec++;
if( p->nRec >= (p->nSize * 3) && p->nRec < 100000 ){
/* Grow the hashtable */
sxu32 nNewSize = p->nSize << 1;
bitvec_rec *pEntry,**apNew;
sxu32 n;
apNew = (bitvec_rec **)SyMemBackendAlloc(p->pAlloc, nNewSize * sizeof(bitvec_rec *));
if( apNew ){
sxu32 iBucket;
/* Zero the new table */
SyZero((void *)apNew, nNewSize * sizeof(bitvec_rec *));
/* Rehash all entries */
n = 0;
pEntry = p->pList;
for(;;){
/* Loop one */
if( n >= p->nRec ){
break;
}
pEntry->pNextCol = 0;
/* Install in the new bucket */
iBucket = pEntry->iPage & (nNewSize - 1);
pEntry->pNextCol = apNew[iBucket];
apNew[iBucket] = pEntry;
/* Point to the next entry */
pEntry = pEntry->pNext;
n++;
}
/* Release the old table and reflect the change */
SyMemBackendFree(p->pAlloc,(void *)p->apRec);
p->apRec = apNew;
p->nSize = nNewSize;
}
}
return UNQLITE_OK;
}
/*
* Destroy a bitvec instance. Reclaim all memory used.
*/
UNQLITE_PRIVATE void unqliteBitvecDestroy(Bitvec *p)
{
bitvec_rec *pNext,*pRec = p->pList;
SyMemBackend *pAlloc = p->pAlloc;
for(;;){
if( p->nRec < 1 ){
break;
}
pNext = pRec->pNext;
SyMemBackendPoolFree(pAlloc,(void *)pRec);
pRec = pNext;
p->nRec--;
if( p->nRec < 1 ){
break;
}
pNext = pRec->pNext;
SyMemBackendPoolFree(pAlloc,(void *)pRec);
pRec = pNext;
p->nRec--;
if( p->nRec < 1 ){
break;
}
pNext = pRec->pNext;
SyMemBackendPoolFree(pAlloc,(void *)pRec);
pRec = pNext;
p->nRec--;
if( p->nRec < 1 ){
break;
}
pNext = pRec->pNext;
SyMemBackendPoolFree(pAlloc,(void *)pRec);
pRec = pNext;
p->nRec--;
}
SyMemBackendFree(pAlloc,(void *)p->apRec);
SyMemBackendFree(pAlloc,p);
}