📄 macedon_cache.cc
字号:
//Copyright (c) 2004, Charles Killian, Adolfo Rodriguez, Dejan Kostic, Sooraj Bhat, and Amin Vahdat//All rights reserved.////Redistribution and use in source and binary forms, with or without//modification, are permitted provided that the following conditions are met://// * Redistributions of source code must retain the above copyright// notice, this list of conditions and the following disclaimer.// * Redistributions in binary form must reproduce the above copyright// notice, this list of conditions and the following disclaimer in// the documentation and/or other materials provided with the// distribution.// * Neither the names of Duke University nor The University of// California, San Diego, nor the names of its contributors// may be used to endorse or promote products derived from// this software without specific prior written permission.////THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"//AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE//IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE//FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL//DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,//OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE//USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.#include "macedon_cache.h"#include "macedon.h"mac_cache::mac_cache() : num_entries(0){}mac_cache::~mac_cache(){}void mac_cache::consistency(){ for (int i=0; i<num_entries; i++) { if (wall_clock() > entries[i].time + MAX_CACHE_LIFE) {#ifdef CACHE_TRACE printf("Cache %f: Nuking stale %.8x %.8x %.8x\n",wall_clock(),entries[i].upper_start, entries[i].upper_end, entries[i].lower);#endif if (i != num_entries-1) entries[i] = entries[num_entries-1]; num_entries--; return; } } return;}void mac_cache::insert(int upper_s, int upper_e, int lower) { consistency(); for (int i=0; i<num_entries; i++) { if (entries[i].lower == lower) { if ( entries[i].upper_start != upper_s || entries[i].upper_end != upper_e)#ifdef CACHE_TRACE printf("Cache %f: Updating %.8x %.8x %.8x\n", wall_clock(),upper_s, upper_e, lower);#endif entries[i].upper_start = upper_s; entries[i].upper_end = upper_e; entries[i].time = wall_clock(); return; } } if (num_entries == MAX_CACHE_SIZE) throw_out_oldest(); #ifdef CACHE_TRACE printf("Cache %f: Adding %.8x %.8x %.8x\n", wall_clock(),upper_s, upper_e, lower);#endif entries[num_entries].upper_start = upper_s; entries[num_entries].upper_end = upper_e; entries[num_entries].lower = lower; entries[num_entries].time = wall_clock(); num_entries++; return;}void mac_cache::throw_out_oldest() { if (num_entries != MAX_CACHE_SIZE) return; double oldest_time = entries[0].time; int oldest = 0; for (int i=1; i<num_entries; i++) { if (entries[i].time < oldest_time) { oldest_time = entries[i].time; oldest=i; } }#ifdef CACHE_TRACE printf("Cache %f: Nuking oldest %.8x %.8x %.8x\n",wall_clock(), entries[oldest].upper_start, entries[oldest].upper_end, entries[oldest].lower);#endif num_entries--; entries[oldest] = entries[num_entries];}void mac_cache::remove(int node) { for (int i=0; i<num_entries; i++) { if(entries[i].lower == node) { num_entries--; if(i < num_entries) { entries[i] = entries[num_entries]; } } }}int mac_cache::query(int upper_query){ //return 0; consistency(); for (int i=0; i<num_entries; i++) { if (entries[i].upper_start == upper_query || (entries[i].upper_start != entries[i].upper_end && isinrangeright(upper_query, entries[i].upper_start, entries[i].upper_end))) {#ifdef CACHE_TRACE printf("Cache: Using mapping %x (%x %x) to %x\n", upper_query, entries[i].upper_start, entries[i].upper_end, entries[i].lower);#endif return entries[i].lower; } } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -