int Cache::erase(fim::Image* oi) { /* * erases the image from the image cache * */ /* acca' nun stimm'a'ppazzia' */ if(!oi) { return -1; } if(is_in_cache(oi) ) { usageCounter[oi->getKey()]=0; /* NOTE : the user should call usageCounter.erase(key) after this ! */ lru.erase(oi); imageCache.erase(reverseCache[oi]); reverseCache.erase(oi); // delete imageCache[reverseCache[oi]]; #ifdef FIM_CACHE_DEBUG std::cout << "will erase "<< oi << "\n"; cout << "deleting " << oi->getName() << "\n"; #endif delete oi; // NEW !! setGlobalVariable(FIM_VID_CACHED_IMAGES,cached_elements()); return 0; } return -1; }
int Cache::free(fim::Image* oi) { /* acca' nun stimm'a'ppazzia' */ if(!oi)return -1; if(!is_in_cache(oi)) { #if 0 /* if the image is not already one of ours, it * is probably a cloned one, and the caller * didn't know this. * * in this case we keep it in the cache, * so it could be useful in the future. * */ if( oi->revertToLoaded() )//removes internal scale caches cacheImage( oi ); //FIXME : validity should be checked .. #else delete oi; #endif return 0; } /* * fixme : we should explicitly mark for deletion someday.. * */ //if(need_free())return erase(oi); /* careful here !! */ //if(need_free())free_some_lru(); else return 0; /* no free needed */ }
void make_cache_step(long int address, struct cache_model *cache, struct passed_args *args) { /* A main function to process the lines of the trace file in a sequential way */ age_cache(cache); if (is_in_cache(address, cache, args)) { cache_hits += 1; return; } cache_misses += 1; add_to_cache(address, cache, args); }
bool Cache::freeCachedImage(Image *image) { /* * if the supplied image is cached as a master image of a clone, it is freed and deregistered. * if not, no action is performed. * */ // WARNING : FIXME : DANGER !! if( !image )return false; // if( is_in_cache(image) && usageCounter[image->getKey()]==1 ) if( is_in_clone_cache(image) ) { usageCounter[image->getKey()]--; erase_clone(image); // we _always_ immediately delete clones setGlobalVariable(FIM_VID_CACHE_STATUS,getReport().c_str()); return true; } else if( is_in_cache(image) ) { usageCounter[image->getKey()]--; if( (usageCounter[image->getKey()])==0 && image->getKey().second!=FIM_E_STDIN ) { #if 0 if( need_free() && image->getKey().second!=FIM_E_STDIN ) { cache_key_t key = image->getKey(); this->erase( image ); usageCounter.erase(key); } #else /* doing it here is dangerous : */ if( need_free() ) { Image * lrui = get_lru(true); if(lrui && ( lrui->getKey().second!=FIM_E_STDIN )) { cache_key_t key = lrui->getKey(); this->erase( lrui ); usageCounter.erase(key); } // missing usageCounter.erase().. } #endif } setGlobalVariable(FIM_VID_CACHE_STATUS,getReport().c_str()); return true; } return false; }
int search_from_directory(struct directory *dir, unsigned long entry, struct node *node){ int nb_sons = dir->nb_sons; int i = 0; while(i<nb_sons){ if (is_in_cache(dir->sons_caches[i], entry)){ if (from_node(node, dir->sons_caches[i])){ return 1; } } i++; } return 0; }
int Cache::prefetch(cache_key_t key) { // if(need_free()) // free_some_lru(); if(key.first == FIM_STDIN_IMAGE_NAME) return 0;// just a fix in the case the browser is still lame if(is_in_cache(key)) return 0; if(!loadNewImage(key)) return -1; setGlobalVariable(FIM_VID_CACHED_IMAGES,cached_elements()); setGlobalVariable(FIM_VID_CACHE_STATUS,getReport().c_str()); return 0; // return getCachedImage(key)?0:-1; }
int delete_from_directory(struct directory *dir, struct block *block){ int nb_ways = block->nb_ways; int min_priority = INT_MAX; int i, j; /* At the beginning, all priorities are set to 0 */ for (j=0; j<nb_ways; j++){ block->lines[j]->priority = 0; } for (i=0; i<dir->nb_sons; i++){ for (j=0; j<nb_ways; j++){ if (is_in_cache(dir->sons_caches[i], block->lines[j]->first_case)) block->lines[j]->priority++; } } for (j=0; j<nb_ways; j++){ if (block->lines[j]->priority < min_priority) { min_priority = block->lines[j]->priority; } } return min_priority; }
Image * Cache::useCachedImage(cache_key_t key) { /* * the calling function needs an image, so calls this method. * if we already have the desired image and it is already used, * a clone is built and returned. * * if we have an unused master, we return it. * * then declare this image as used and increase a relative counter. * * a freeImage action will do the converse operation (and delete). * if the image is not already cached, it is loaded, if possible. * * so, if there is no such image, NULL is returned * */ #ifdef FIM_CACHE_DEBUG std::cout << " useCachedImage(\""<<key.first<<","<<key.second<<"\")\n"; #endif Image * image=NULL; if(!is_in_cache(key)) { /* * no Image cached at all for this filename * */ image = loadNewImage(key); if(!image)return NULL; // bad luck! usageCounter[key]=1; setGlobalVariable(FIM_VID_CACHE_STATUS,getReport().c_str()); return image; // usageCounter[key]=0; } else { /* * at least one copy of this filename image is in cache * */ image=getCachedImage(key);// in this way we update the LRU cache :) if(!image) { // critical error #ifdef FIM_CACHE_DEBUG cout << "critical internal cache error!\n"; #endif setGlobalVariable(FIM_VID_CACHE_STATUS,getReport().c_str()); return NULL; } if( used_image( key ) ) { // if the image was already used, cloning occurs // image = image->getClone(); // EVIL !! try { #ifdef FIM_CACHE_DEBUG Image * oi=image; #endif image = new Image(*image); // cloning #ifdef FIM_CACHE_DEBUG std::cout << " cloned image: \"" <<image->getName()<< "\" "<< image << " from \""<<oi->getName() <<"\" " << oi << "\n"; #endif } catch(FimException e) { /* we will survive :P */ image = NULL; /* we make sure no taint remains */ // if( e != FIM_E_NO_IMAGE )throw FIM_E_TRAGIC; /* hope this never occurs :P */ } if(!image)return NULL; //means that cloning failed. clone_pool.insert(image); // we have a clone cloneUsageCounter[image]=1; } lru_touch( key ); // if loading and eventual cloning succeeded, we count the image as used of course usageCounter[key]++; setGlobalVariable(FIM_VID_CACHE_STATUS,getReport().c_str()); return image; //so, it could be a clone.. } }