-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.c
252 lines (204 loc) · 4.84 KB
/
cache.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// speed up access into bit array
//
// Note: benchmarks show this speeds up encoding by 33% for
// a tradeoff in complexity
//
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <search.h>
#include "config.h"
#include "cache.h"
//#define CP_TRACE
#define CE_MAP 1024
// easy macros
#define IS_END(ce) ( (char *)ce == (char *)&ctx->ce_q )
#define IS_FIRST(ce) ( (char *)ce->q.q_back == (char *)&ctx->ce_q )
#define IS_LAST(ce) ( (char *)ce->q.q_forw == (char *)&ctx->ce_q )
// a queue of CE's
// create an initial ctx
CTX *cache_ctx_new ( void )
{
CTX *ctx = (CTX *)malloc(sizeof(CTX));
assert(ctx!=NULL);
ctx->ce_q.q_forw = &ctx->ce_q;
ctx->ce_q.q_back = &ctx->ce_q;
return ctx;
}
// init ces
void cache_init_ces ( CTX *ctx, int bit_max )
{
CE *ce;
int offset = 0;
// free anybody on ce_q
while ( (unsigned char *)ctx->ce_q.q_forw != (unsigned char *)&ctx->ce_q ) {
ce = (CE *)ctx->ce_q.q_forw;
remque(ce);
free(ce);
}
// init queue
ctx->ce_q.q_forw = &ctx->ce_q;
ctx->ce_q.q_back = &ctx->ce_q;
// create a CE for each block of data presented
while ( 1 ) {
if ( offset > bit_max ) {
break;
} else {
ce = (CE *)malloc ( sizeof(CE) );
assert(ce!=NULL);
ce->bit_offset = offset;
ce->off = offset / 4;
ce->bit = 0;
insque ( ce, ctx->ce_q.q_back );
}
// onward
offset += CE_MAP;
}
}
// return TRUE if ce in range
static int ce_in_range ( CTX *ctx, CE *ce , int bit_offset )
{
CE *nxt;
if ( IS_LAST(ce) ) return 1;
if ( IS_FIRST(ce) ) {
nxt = (CE *)ce->q.q_forw;
if ( bit_offset < nxt->bit_offset ) return 1;
return 0;
}
// neither first nor last
nxt = (CE *)ce->q.q_forw;
if ( bit_offset >= ce->bit_offset &&
bit_offset < nxt->bit_offset ) return 1;
return 0;
}
#if defined(CP_TRACE)
static void show_ce( CE *ce)
{
printf("%s: entry, ce = %p\n",
__FUNCTION__,ce);
printf(" bit_offset = %d\n",ce->bit_offset);
printf(" off = %d\n",ce->off);
printf(" bit = %d\n",ce->bit);
}
#endif // CP_TRACE
static int do_local_match ( CTX *ctx, CE *ce, unsigned char *array, int bit_offset, int rbit_flag )
{
int ret;
int off;
int bit;
off = ce->off;
bit = ce->bit;
bit_offset -= ce->bit_offset;
// walk forward until we find our guy
while ( 1 ) {
int bit_mask;
int dibit_mask;
bit_mask = 1<<(bit*2);
dibit_mask = 1<<(bit*2 + 1);
if ( !(array[off]&dibit_mask) ) {
// count
if ( bit_offset == 0 ) {
// found
if ( rbit_flag ) {
ret = off * 4 + bit;
} else {
ret = array[off]&bit_mask ? 1 : 0;
}
array[off] |= dibit_mask;
#if defined(CP_TRACE)
printf("%s: found bit, array[%d] = 0x%02x, bit_mask = 0x%02x, ret = %d\n",
__FUNCTION__,
off,array[off]&0xff,bit_mask,ret);
#endif
break;
}
bit_offset -= 1;
}
bit += 1;
if ( bit > 3 ) {
bit = 0;
off += 1;
}
} // while
// decrement by 1 everyone after this CE
ce = (CE *)ce->q.q_forw;
while ( !IS_END(ce) ) {
CE *nxt = (CE *)ce->q.q_forw;
if ( ce->bit_offset ) {
ce->bit_offset -= 1;
if ( ce->bit_offset < 0 ) {
remque(ce);
free(ce);
}
}
// onward
ce = nxt;
}
return ret;
}
// find idx and bit for a given bit_offset
// return value
int cache_find_dibit ( CTX *ctx, unsigned char *array, int bit_offset )
{
CE *ce;
int ret;
#if defined(CP_TRACE)
printf("%s: entry, bit_offset = %d (0x%08x)\n",
__FUNCTION__,
bit_offset, bit_offset);
#endif
ce = (CE *)ctx->ce_q.q_forw;
while ( !IS_END(ce) ) {
CE *nxt = (CE *)ce->q.q_forw; // save next ce as current one may goa
if ( ce_in_range(ctx,ce,bit_offset) ) {
// match the bits
ret = do_local_match ( ctx, ce, array, bit_offset, 0 );
// done
return ret;
}
ce = nxt;
} // while
#if defined(CP_TRACE)
printf("%s: Fatal Internal Error, after while\n",__FUNCTION__);
exit(0);
#endif
return 0;
}
#if defined(USE_RBIT_TEST)
// find the bit number in rbit file
//
// Note: This returns a dibit offset, NOT the bit offset
//
int cache_find_rbit ( CTX *ctx, unsigned char *array, int bit_offset )
{
CE *ce;
int ret;
#if defined(CP_TRACE)
printf("%s: entry, bit_offset = %d\n",
__FUNCTION__,
bit_offset);
#endif
ce = (CE *)ctx->ce_q.q_forw;
while ( !IS_END(ce) ) {
CE *nxt = (CE *)ce->q.q_forw; // save next ce as current one may goa
if ( ce_in_range(ctx,ce,bit_offset) ) {
// match the bits
ret = do_local_match ( ctx, ce, array, bit_offset, 1 );
// done
return ret;
}
ce = nxt;
} // while
#if defined(CP_TRACE)
printf("%s: Fatal Internal Error, after while\n",__FUNCTION__);
exit(0);
#endif
return 0;
}
#endif