| 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 |
#include "global.h" |
| 29 |
|
| 30 |
#define PROFILE 0 |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#define CHUNKBITS (12) |
| 38 |
#define CHUNKSIZE (1<<CHUNKBITS) |
| 39 |
#define CHUNKMASK (CHUNKSIZE-1) |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#define MINBLOCKSIZE (256) |
| 44 |
|
| 45 |
|
| 46 |
#define HASHSIZE (13) |
| 47 |
#define HASHFUNC(start) (((start)>>CHUNKBITS) % HASHSIZE) |
| 48 |
|
| 49 |
|
| 50 |
#define MINIMUM(a,b) (((a) < (b)) ? (a) : (b)) |
| 51 |
#define MAXIMUM(a,b) (((a) > (b)) ? (a) : (b)) |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
typedef struct chunk { |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
u8 start, end, len; |
| 64 |
void *buf; |
| 65 |
|
| 66 |
struct chunk *next, *prev; |
| 67 |
} CHUNK; |
| 68 |
|
| 69 |
typedef struct cache { |
| 70 |
|
| 71 |
CHUNK *hashtab[HASHSIZE]; |
| 72 |
|
| 73 |
void *tempbuf; |
| 74 |
} CACHE; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
static CHUNK * ensure_chunk(SOURCE *s, CACHE *cache, u8 start); |
| 81 |
static CHUNK * get_chunk_alloc(CACHE *cache, u8 start); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
u8 get_buffer(SECTION *section, u8 pos, u8 len, void **buf) |
| 88 |
{ |
| 89 |
SOURCE *s; |
| 90 |
|
| 91 |
|
| 92 |
s = section->source; |
| 93 |
pos += section->pos; |
| 94 |
|
| 95 |
return get_buffer_real(s, pos, len, NULL, buf); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
u8 get_buffer_real(SOURCE *s, u8 pos, u8 len, void *inbuf, void **outbuf) |
| 103 |
{ |
| 104 |
CACHE *cache; |
| 105 |
CHUNK *c; |
| 106 |
u8 end, first_chunk, last_chunk, curr_chunk, got, tocopy; |
| 107 |
void *mybuf; |
| 108 |
|
| 109 |
|
| 110 |
if (len == 0 || (inbuf == NULL && outbuf == NULL)) |
| 111 |
return 0; |
| 112 |
|
| 113 |
|
| 114 |
if (s->size_known && pos >= s->size) { |
| 115 |
|
| 116 |
return 0; |
| 117 |
} |
| 118 |
end = pos + len; |
| 119 |
if (s->size_known && end > s->size) { |
| 120 |
|
| 121 |
end = s->size; |
| 122 |
len = end - pos; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
cache = (CACHE *)s->cache_head; |
| 127 |
if (cache == NULL) { |
| 128 |
|
| 129 |
cache = (CACHE *)malloc(sizeof(CACHE)); |
| 130 |
if (cache == NULL) |
| 131 |
bailout("Out of memory"); |
| 132 |
memset(cache, 0, sizeof(CACHE)); |
| 133 |
s->cache_head = (void *)cache; |
| 134 |
} |
| 135 |
|
| 136 |
if (cache->tempbuf != NULL) { |
| 137 |
free(cache->tempbuf); |
| 138 |
cache->tempbuf = NULL; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
first_chunk = pos & ~CHUNKMASK; |
| 143 |
last_chunk = (end - 1) & ~CHUNKMASK; |
| 144 |
|
| 145 |
if (last_chunk == first_chunk) { |
| 146 |
|
| 147 |
c = ensure_chunk(s, cache, first_chunk); |
| 148 |
|
| 149 |
|
| 150 |
if (pos >= c->end) |
| 151 |
return 0; |
| 152 |
|
| 153 |
|
| 154 |
len = MINIMUM(len, c->end - pos); |
| 155 |
mybuf = c->buf + (pos - c->start); |
| 156 |
if (inbuf) |
| 157 |
memcpy(inbuf, mybuf, len); |
| 158 |
if (outbuf) |
| 159 |
*outbuf = mybuf; |
| 160 |
|
| 161 |
return len; |
| 162 |
|
| 163 |
} else { |
| 164 |
|
| 165 |
|
| 166 |
if (inbuf) { |
| 167 |
mybuf = inbuf; |
| 168 |
} else { |
| 169 |
|
| 170 |
#if PROFILE |
| 171 |
printf("Temporary buffer for request %llu:%llu\n", pos, len); |
| 172 |
#endif |
| 173 |
|
| 174 |
|
| 175 |
cache->tempbuf = malloc(len); |
| 176 |
if (cache->tempbuf == NULL) { |
| 177 |
error("Out of memory, still going"); |
| 178 |
return 0; |
| 179 |
} |
| 180 |
mybuf = cache->tempbuf; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
got = 0; |
| 185 |
for (curr_chunk = first_chunk; curr_chunk <= last_chunk; |
| 186 |
curr_chunk += CHUNKSIZE) { |
| 187 |
|
| 188 |
c = ensure_chunk(s, cache, curr_chunk); |
| 189 |
|
| 190 |
|
| 191 |
if (pos > curr_chunk) { |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
if (c->end > pos) { |
| 202 |
tocopy = c->end - pos; |
| 203 |
memcpy(mybuf, c->buf + (pos & CHUNKMASK), tocopy); |
| 204 |
} else |
| 205 |
tocopy = 0; |
| 206 |
} else { |
| 207 |
|
| 208 |
tocopy = MINIMUM(c->len, len - got); |
| 209 |
if (tocopy) |
| 210 |
memcpy(mybuf + got, c->buf, tocopy); |
| 211 |
} |
| 212 |
got += tocopy; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
if (c->len < CHUNKSIZE) |
| 217 |
break; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
len = MINIMUM(len, got); |
| 222 |
if (outbuf) |
| 223 |
*outbuf = mybuf; |
| 224 |
return len; |
| 225 |
|
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
static CHUNK * ensure_chunk(SOURCE *s, CACHE *cache, u8 start) |
| 230 |
{ |
| 231 |
CHUNK *c; |
| 232 |
u8 pos, rel_start, rel_end; |
| 233 |
u8 toread, result, curr_chunk; |
| 234 |
|
| 235 |
c = get_chunk_alloc(cache, start); |
| 236 |
if (c->len >= CHUNKSIZE || (s->size_known && c->end >= s->size)) { |
| 237 |
|
| 238 |
return c; |
| 239 |
} |
| 240 |
|
| 241 |
if (s->sequential) { |
| 242 |
|
| 243 |
|
| 244 |
if (s->seq_pos < start) { |
| 245 |
|
| 246 |
curr_chunk = s->seq_pos & ~CHUNKMASK; |
| 247 |
while (curr_chunk < start) { |
| 248 |
|
| 249 |
ensure_chunk(s, cache, curr_chunk); |
| 250 |
curr_chunk += CHUNKSIZE; |
| 251 |
if (s->seq_pos < curr_chunk) |
| 252 |
break; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
if (s->size_known && c->end >= s->size) |
| 257 |
return c; |
| 258 |
} |
| 259 |
|
| 260 |
if (s->seq_pos != c->end) |
| 261 |
return c; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
if (s->read_block != NULL) { |
| 266 |
|
| 267 |
|
| 268 |
if (s->blocksize < MINBLOCKSIZE || |
| 269 |
s->blocksize > CHUNKSIZE || |
| 270 |
((s->blocksize & (s->blocksize - 1)) != 0)) { |
| 271 |
bailout("Internal error: Invalid block size %d", s->blocksize); |
| 272 |
} |
| 273 |
|
| 274 |
for (rel_start = 0; rel_start < CHUNKSIZE; rel_start = rel_end) { |
| 275 |
rel_end = rel_start + s->blocksize; |
| 276 |
if (c->len >= rel_end) |
| 277 |
continue; |
| 278 |
pos = c->start + rel_start; |
| 279 |
if (s->size_known && pos >= s->size) |
| 280 |
break; |
| 281 |
|
| 282 |
|
| 283 |
if (s->read_block(s, pos, c->buf + rel_start)) { |
| 284 |
|
| 285 |
c->len = rel_end; |
| 286 |
c->end = c->start + c->len; |
| 287 |
} else { |
| 288 |
|
| 289 |
c->len = rel_start; |
| 290 |
c->end = c->start + c->len; |
| 291 |
|
| 292 |
if (!s->size_known || s->size > c->end) { |
| 293 |
s->size_known = 1; |
| 294 |
s->size = c->end; |
| 295 |
} |
| 296 |
break; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
} else { |
| 301 |
|
| 302 |
|
| 303 |
if (s->size_known && s->size < c->start + CHUNKSIZE) { |
| 304 |
|
| 305 |
toread = s->size - c->end; |
| 306 |
|
| 307 |
} else { |
| 308 |
toread = CHUNKSIZE - c->len; |
| 309 |
} |
| 310 |
result = s->read_bytes(s, c->start + c->len, toread, |
| 311 |
c->buf + c->len); |
| 312 |
if (result > 0) { |
| 313 |
|
| 314 |
c->len += result; |
| 315 |
c->end = c->start + c->len; |
| 316 |
if (s->sequential) |
| 317 |
s->seq_pos += result; |
| 318 |
} |
| 319 |
if (result < toread) { |
| 320 |
|
| 321 |
|
| 322 |
if (!s->size_known || s->size > c->end) { |
| 323 |
s->size_known = 1; |
| 324 |
s->size = c->end; |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
return c; |
| 330 |
} |
| 331 |
|
| 332 |
static CHUNK * get_chunk_alloc(CACHE *cache, u8 start) |
| 333 |
{ |
| 334 |
int hpos; |
| 335 |
CHUNK *chain, *trav, *c; |
| 336 |
|
| 337 |
|
| 338 |
hpos = HASHFUNC(start); |
| 339 |
chain = cache->hashtab[hpos]; |
| 340 |
|
| 341 |
if (chain == NULL) { |
| 342 |
|
| 343 |
c = (CHUNK *)malloc(sizeof(CHUNK)); |
| 344 |
if (c == NULL) |
| 345 |
bailout("Out of memory"); |
| 346 |
c->buf = malloc(CHUNKSIZE); |
| 347 |
if (c->buf == NULL) |
| 348 |
bailout("Out of memory"); |
| 349 |
c->start = start; |
| 350 |
c->end = start; |
| 351 |
c->len = 0; |
| 352 |
|
| 353 |
c->prev = c->next = c; |
| 354 |
cache->hashtab[hpos] = c; |
| 355 |
return c; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
trav = chain; |
| 360 |
do { |
| 361 |
if (trav->start == start) |
| 362 |
return trav; |
| 363 |
trav = trav->next; |
| 364 |
} while (trav != chain); |
| 365 |
|
| 366 |
|
| 367 |
c = (CHUNK *)malloc(sizeof(CHUNK)); |
| 368 |
if (c == NULL) |
| 369 |
bailout("Out of memory"); |
| 370 |
c->buf = malloc(CHUNKSIZE); |
| 371 |
if (c->buf == NULL) |
| 372 |
bailout("Out of memory"); |
| 373 |
c->start = start; |
| 374 |
c->end = start; |
| 375 |
c->len = 0; |
| 376 |
|
| 377 |
c->prev = chain->prev; |
| 378 |
c->next = chain; |
| 379 |
c->prev->next = c; |
| 380 |
c->next->prev = c; |
| 381 |
cache->hashtab[hpos] = c; |
| 382 |
return c; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
void close_source(SOURCE *s) |
| 390 |
{ |
| 391 |
CACHE *cache; |
| 392 |
int hpos; |
| 393 |
CHUNK *chain, *trav, *nexttrav; |
| 394 |
|
| 395 |
|
| 396 |
cache = (CACHE *)s->cache_head; |
| 397 |
if (cache != NULL) { |
| 398 |
#if PROFILE |
| 399 |
printf("Cache profile:\n"); |
| 400 |
#endif |
| 401 |
if (cache->tempbuf != NULL) |
| 402 |
free(cache->tempbuf); |
| 403 |
for (hpos = 0; hpos < HASHSIZE; hpos++) { |
| 404 |
#if PROFILE |
| 405 |
printf(" hash position %d:", hpos); |
| 406 |
#endif |
| 407 |
chain = cache->hashtab[hpos]; |
| 408 |
if (chain != NULL) { |
| 409 |
trav = chain; |
| 410 |
do { |
| 411 |
#if PROFILE |
| 412 |
printf(" %lluK", trav->start >> 10); |
| 413 |
if (trav->len != CHUNKSIZE) |
| 414 |
printf(":%llu", trav->len); |
| 415 |
#endif |
| 416 |
nexttrav = trav->next; |
| 417 |
free(trav->buf); |
| 418 |
free(trav); |
| 419 |
trav = nexttrav; |
| 420 |
} while (trav != chain); |
| 421 |
#if PROFILE |
| 422 |
printf("\n"); |
| 423 |
#endif |
| 424 |
} |
| 425 |
#if PROFILE |
| 426 |
else |
| 427 |
printf(" empty\n"); |
| 428 |
#endif |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
if (s->close != NULL) |
| 434 |
(*s->close)(s); |
| 435 |
|
| 436 |
|
| 437 |
free(s); |
| 438 |
} |
| 439 |
|
| 440 |
|