/* Find the least recently block (to evict soon) */ cache_block* find_LRU(address_t addr, student_cache_t *cache) { int index = decode_index(addr,cache); cache_LRU *lru; cache_block *block; /* Find the LRU block from the LRUs array */ lru = cache->LRUs + index; block = get_block_from_way(index,cache->ways + lru->way_index); /* Set as used */ set_used(cache,index,lru->way_index); return block; }
/* Find an invalid block to replace */ cache_block* find_invalid(address_t addr, student_cache_t *cache) { int i; int index = decode_index(addr,cache); cache_block *block; /* Loop through the cache line and return a block if it's invalid */ for(i=0; i<cache->ways_size; i++) { block = get_block_from_way(index,cache->ways+i); if(!block->valid) { set_used(cache,index,i); return block; } } /* Nothing found so return NULL */ return NULL; }
/* Look for a matching tag in the cache */ cache_block* find_block(address_t addr, student_cache_t *cache, int write) { int i; int index = decode_index(addr,cache); int tag = decode_tag(addr,cache); cache_block *block; /* Loop through the cache line */ for(i=0; i<cache->ways_size; i++) { block = get_block_from_way(index,cache->ways+i); if(block->valid && block->tag == tag) { /* Found a valid match */ if(!write || cache->WP==WBWA) { /* Only set used if a read or a WBWA write */ set_used(cache,index,i); } return block; } } /* No match so return NULL */ return NULL; }
lzma_vli find_file_index(void **bdatap) { if (!gIndex) decode_index(); // find the last block lzma_index_iter iter; lzma_index_iter_init(&iter, gIndex); lzma_vli loc = lzma_index_uncompressed_size(gIndex) - 1; if (lzma_index_iter_locate(&iter, loc)) die("Can't locate file index block"); void *bdata = decode_block_start(iter.block.compressed_file_offset); gFileIndexBuf = malloc(gFIBSize); gStream.avail_out = gFIBSize; gStream.avail_in = 0; // Check if this is really an index read_file_index_data(); lzma_vli ret = iter.block.compressed_file_offset; if (xle64dec(gFileIndexBuf + gFIBPos) != PIXZ_INDEX_MAGIC) ret = 0; gFIBPos += sizeof(uint64_t); if (bdatap && ret) { *bdatap = bdata; } else { // Just looking, don't keep things around if (bdatap) *bdatap = NULL; free(bdata); free(gFileIndexBuf); gLastFile = gFileIndex = NULL; lzma_end(&gStream); } return ret; }
void pixz_read(bool verify, size_t nspecs, char **specs) { decode_index(); if (verify) gFileIndexOffset = read_file_index(0); wanted_files(nspecs, specs); set_block_sizes(); #if DEBUG for (wanted_t *w = gWantedFiles; w; w = w->next) debug("want: %s", w->name); #endif pipeline_create(block_create, block_free, read_thread, decode_thread); if (verify && gFileIndexOffset) { gArWanted = gWantedFiles; wanted_t *w = gWantedFiles, *wlast = NULL; bool lastmulti = false; off_t lastoff = 0; struct archive *ar = archive_read_new(); archive_read_support_compression_none(ar); archive_read_support_format_tar(ar); archive_read_open(ar, NULL, tar_ok, tar_read, tar_ok); struct archive_entry *entry; while (true) { int aerr = archive_read_next_header(ar, &entry); if (aerr == ARCHIVE_EOF) { break; } else if (aerr != ARCHIVE_OK && aerr != ARCHIVE_WARN) { fprintf(stderr, "%s\n", archive_error_string(ar)); die("Error reading archive entry"); } off_t off = archive_read_header_position(ar); const char *path = archive_entry_pathname(entry); if (!lastmulti) { if (wlast && wlast->size != off - lastoff) die("Index and archive show differing sizes for %s: %d vs %d", wlast->name, wlast->size, off - lastoff); lastoff = off; } lastmulti = is_multi_header(path); if (lastmulti) continue; if (!w) die("File %s missing in index", path); if (strcmp(path, w->name) != 0) die("Index and archive differ as to next file: %s vs %s", w->name, path); wlast = w; w = w->next; } if (w && w->name) die("File %s missing in archive", w->name); tar_write_last(); // write whatever's left } else { pipeline_item_t *pi; while ((pi = pipeline_merged())) { io_block_t *ib = (io_block_t*)(pi->data); fwrite(ib->output, ib->outsize, 1, gOutFile); queue_push(gPipelineStartQ, PIPELINE_ITEM, pi); } } pipeline_destroy(); wanted_free(gWantedFiles); }
/* 0 - non equal 1 - equal */ static int syntax_strcmp(const char *line,int len,int pos,char *text,char *whole_left,char *whole_right,int line_start,int recursive) { if(!*text) return 0; int c=syntax_get_byte(line,len,pos-1); if(line_start) if(c!='\n') return 0; if(whole_left) if(whole_left[c&0xff]) return 0; char *p=text; int old_pos=pos,index; while(*p) { switch(*p) { case '\001': p++; while(true) { if(recursive) { while(true) { int new_pos=syntax_strcmp(line,len,pos,text,whole_left,whole_right,line_start,recursive); if(new_pos) pos=new_pos; else break; } } c=syntax_get_byte(line,len,pos); if(c==*p) break; if(c=='\n') return 0; pos++; } break; case '\002': p++; while(true) { if(recursive) { while(true) { int new_pos=syntax_strcmp(line,len,pos,text,whole_left,whole_right,line_start,recursive); if(new_pos) pos=new_pos; else break; } } c=syntax_get_byte(line,len,pos); if(c==*p) break; if((c=='\n')||(c=='\t')||(c==' ')) { if(!*p) break; return 0; } pos++; } break; case '\003': p++; index=*((int *)p); index=decode_index(index); p++; p++; p++; for(unsigned int i=0;i<classes[index].min;i++,pos++) { c=syntax_get_byte(line,len,pos); if(!classes[index].Class[c&0xff]) return 0; } for(unsigned int i=classes[index].min;i<classes[index].max;i++,pos++) { c=syntax_get_byte(line,len,pos); if(!classes[index].Class[c&0xff]) goto syntax_strcmp_cc_ok; } c=syntax_get_byte(line,len,pos); if(classes[index].Class[c&0xff]) return 0; syntax_strcmp_cc_ok: pos--; break; default: if(*p!=syntax_get_byte(line,len,pos)) return 0; } p++,pos++; } if(whole_right) if(whole_right[syntax_get_byte(line,len,pos)]) return 0; if(pos==old_pos) return 0; return pos; }
void pixz_read(bool verify, size_t nspecs, char **specs) { if (decode_index()) { if (verify) gFileIndexOffset = read_file_index(); wanted_files(nspecs, specs); gExplicitFiles = nspecs; } #if DEBUG for (wanted_t *w = gWantedFiles; w; w = w->next) debug("want: %s", w->name); #endif pipeline_create(block_create, block_free, gIndex ? read_thread : read_thread_noindex, decode_thread); if (verify && gFileIndexOffset) { gArWanted = gWantedFiles; wanted_t *w = gWantedFiles, *wlast = NULL; bool lastmulti = false; off_t lastoff = 0; struct archive *ar = archive_read_new(); archive_read_support_compression_none(ar); archive_read_support_format_tar(ar); archive_read_open(ar, NULL, tar_ok, tar_read, tar_ok); struct archive_entry *entry; while (true) { int aerr = archive_read_next_header(ar, &entry); if (aerr == ARCHIVE_EOF) { break; } else if (aerr != ARCHIVE_OK && aerr != ARCHIVE_WARN) { fprintf(stderr, "%s\n", archive_error_string(ar)); die("Error reading archive entry"); } off_t off = archive_read_header_position(ar); const char *path = archive_entry_pathname(entry); if (!lastmulti) { if (wlast && wlast->size != off - lastoff) die("Index and archive show differing sizes for %s: %d vs %d", wlast->name, wlast->size, off - lastoff); lastoff = off; } lastmulti = is_multi_header(path); if (lastmulti) continue; if (!w) die("File %s missing in index", path); if (strcmp(path, w->name) != 0) die("Index and archive differ as to next file: %s vs %s", w->name, path); wlast = w; w = w->next; } archive_read_finish(ar); if (w && w->name) die("File %s missing in archive", w->name); tar_write_last(); // write whatever's left } if (!gExplicitFiles) { /* Heuristics for detecting pixz file index: * - Input must be streaming (otherwise read_thread does this) * - Data must look tar-like * - Must have all sized blocks, followed by unsized file index */ bool start = !gIndex && verify, tar = false, all_sized = true, skipping = false; pipeline_item_t *pi; while ((pi = pipeline_merged())) { io_block_t *ib = (io_block_t*)(pi->data); if (skipping && ib->btype != BLOCK_CONTINUATION) { fprintf(stderr, "Warning: File index heuristic failed, use -t flag.\n"); skipping = false; } if (!skipping && tar && !start && all_sized && ib->btype == BLOCK_UNSIZED && taste_file_index(ib)) skipping = true; if (start) { tar = taste_tar(ib); start = false; } if (ib->btype == BLOCK_UNSIZED) all_sized = false; if (!skipping) fwrite(ib->output, ib->outsize, 1, gOutFile); queue_push(gPipelineStartQ, PIPELINE_ITEM, pi); } } pipeline_destroy(); wanted_free(gWantedFiles); }