void Cache::swap_index(int i, int j) { if(i==j) return; if(head[i].len) lru_delete(&head[i]); if(head[j].len) lru_delete(&head[j]); swap(head[i].data,head[j].data); swap(head[i].len,head[j].len); if(head[i].len) lru_insert(&head[i]); if(head[j].len) lru_insert(&head[j]); if(i>j) swap(i,j); for(head_t *h = lru_head.next; h!=&lru_head; h=h->next) { if(h->len > i) { if(h->len > j) swap(h->data[i],h->data[j]); else { // give up lru_delete(h); free(h->data); size += h->len; h->data = 0; h->len = 0; } } } }
END_TEST START_TEST(eject_one) { lru_t *lru; int ejected = 0; lru = lru_create(); lru_insert(lru, "one", 3, "one", 3, NULL); lru_insert(lru, "two", 3, "two", 3, NULL); lru_insert(lru, "three", 5, "three", 5, NULL); ejected = lru_eject_by_size(lru, 3, NULL, NULL); dprintf("ejected %d\n", ejected); fail_unless(ejected > 0); }
END_TEST START_TEST(touch) { lru_t *lru; lru_item_t * one; lru = lru_create(); one = lru_insert(lru, "one", 3, "one", 3, NULL); lru_insert(lru, "two", 3, "two", 3, NULL); lru_insert(lru, "three", 5, "three", 5, NULL); lru_touch(lru, one); fail_unless(one == (lru_item_t*)lru->list->head->data); lru_destroy(lru); }
int Cache::get_data(const int index, Qfloat **data, int len) { head_t *h = &head[index]; if(h->len) lru_delete(h); int more = len - h->len; if(more > 0) { // free old space while(size < more) { head_t *old = lru_head.next; lru_delete(old); free(old->data); size += old->len; old->data = 0; old->len = 0; } // allocate new space h->data = (Qfloat *)realloc(h->data,sizeof(Qfloat)*len); size -= more; swap(h->len,len); } lru_insert(h); *data = h->data; return len; }
END_TEST START_TEST(eject_multiple) { lru_t *lru; int ejected = 0; lru_item_t *three; mark_point(); lru = lru_create(); mark_point(); lru_insert(lru, "one", 3, "one", 3, NULL); lru_insert(lru, "two", 3, "two", 3, NULL); three = lru_insert(lru, "three", 5, "three", 5, NULL); ejected = lru_eject_by_size(lru, 12, NULL, NULL); dprintf("test ejected %d\n", ejected); fail_unless((lru_item_t*)lru->list->head->data == three); fail_unless(ejected == 12); lru_destroy(lru); }
void cache_insert(struct file_data *data,unsigned long hv) { cache* temp = (cache *)malloc(sizeof(cache)); temp->data = data; temp->counter=0; temp->hash_value=hv; temp->next = cache_array[hv]; cache_array[hv]=temp; cache_size= cache_size + (temp->data->file_size); lru_insert(temp); return; }
Qfloat* sCache::get_data(int idx, int len, int& numRet) { shead_t *h = &head[idx]; h->refcount ++; if(h->len > 0) lru_delete(h); if(len > h->max_len) { int more = len + CACHE_DELTA - h->max_len; h->max_len = h->max_len + more; int count = 0; // free old space while(maxItem < more) //requested space exceeds logical limit { count++; shead_t *old = lru_head.next; lru_delete(old); free(old->data); old->data = NULL; maxItem += old->max_len; old->len = 0; old->max_len = 0; } // allocate new space h->data = (Qfloat *)realloc(h->data,sizeof(Qfloat)*h->max_len); if (h->data == NULL)//requested space cannot be provided by physical limit { while(h->data == NULL && lru_head.next != &lru_head) { shead_t *old = lru_head.next; lru_delete(old); if (old->refcount <= 0) { free(old->data); old->data = NULL; maxItem += old->max_len; old->len = 0; old->max_len = 0; h->data = (Qfloat *)calloc(h->max_len,sizeof(Qfloat)); } else { old->refcount --; lru_insert(old); } } h->len = 0; if (h->data == NULL) { printf ("sCache cannot allocate memory!\n"); return NULL; } } maxItem -= more; } lru_insert(h); numRet = h->len; h->len = len; return h->data; }