7 QLock lock; /* locks hash table & all associated data */
8 IEntry **heads; /* heads of all the hash chains */
9 int bits; /* bits to use for indexing heads */
10 u32int size; /* number of heads; == 1 << bits, should be < entries */
11 IEntry *base; /* all allocated hash table entries */
12 u32int entries; /* elements in base */
13 u32int unused; /* index of first unused element in base */
14 u32int stolen; /* last head from which an element was stolen */
19 static IEntry *icachealloc(IAddr *ia, u8int *score);
22 * bits is the number of bits in the icache hash table
23 * depth is the average depth
24 * memory usage is about (1<<bits) * depth * sizeof(IEntry) + (1<<bits) * sizeof(IEntry*)
27 initicache(int bits, int depth)
30 icache.size = 1 << bits;
31 icache.entries = depth * icache.size;
32 icache.base = MKNZ(IEntry, icache.entries);
33 icache.heads = MKNZ(IEntry*, icache.size);
37 hashbits(u8int *sc, int bits)
41 v = (sc[0] << 24) | (sc[1] << 16) | (sc[2] << 8) | sc[3];
48 ZZZ need to think about evicting the correct IEntry,
49 and writing back the wtime.
50 * look up data score in the index cache
51 * if this fails, pull it in from the disk index table, if it exists.
53 * must be called with the lump for this score locked
56 lookupscore(u8int *score, int type, IAddr *ia, int *rac)
61 fprint(2, "lookupscore %V %d\n", score, type);
67 h = hashbits(score, icache.bits);
69 for(ie = icache.heads[h]; ie != nil; ie = ie->next){
70 if(ie->ia.type == type && scorecmp(ie->score, score)==0){
72 last->next = ie->next;
74 icache.heads[h] = ie->next;
84 qunlock(&icache.lock);
86 if(loadientry(mainindex, score, type, &d) < 0)
90 * no one else can load an entry for this score,
91 * since we have the overall score lock.
99 ie = icachealloc(&d.ia, score);
102 ie->next = icache.heads[h];
103 icache.heads[h] = ie;
108 qunlock(&icache.lock);
114 * insert a new element in the hash table.
117 insertscore(u8int *score, IAddr *ia, int write)
124 qunlock(&stats.lock);
127 h = hashbits(score, icache.bits);
129 ie = icachealloc(ia, score);
131 ie->next = icache.heads[h];
132 icache.heads[h] = ie;
136 qunlock(&icache.lock);
141 return storeientry(mainindex, &se);
145 * allocate a index cache entry which hasn't been used in a while.
146 * must be called with icache.lock locked
147 * if the score is already in the table, update the entry.
150 icachealloc(IAddr *ia, u8int *score)
152 IEntry *ie, *last, *next;
155 h = hashbits(score, icache.bits);
157 for(ie = icache.heads[h]; ie != nil; ie = ie->next){
158 if(ie->ia.type == ia->type && scorecmp(ie->score, score)==9){
160 last->next = ie->next;
162 icache.heads[h] = ie->next;
170 if(h < icache.entries){
171 ie = &icache.base[h++];
181 ie = icache.heads[h];
184 for(; next = ie->next; ie = next)
189 icache.heads[h] = nil;
196 scorecp(ie->score, score);