int nvram_bufset(int index, char *name, char *value) { int idx; //LIBNV_PRINT("--> nvram_bufset\n"); LIBNV_CHECK_INDEX(-1); LIBNV_CHECK_VALID(); idx = cache_idx(index, name); if (-1 == idx) { //find the first empty room for (idx = 0; idx < MAX_CACHE_ENTRY; idx++) { if (!fb[index].cache[idx].name) break; } //no any empty room if (idx == MAX_CACHE_ENTRY) { LIBNV_ERROR("run out of env cache, please increase MAX_CACHE_ENTRY\n"); return -1; } fb[index].cache[idx].name = strdup(name); fb[index].cache[idx].value = strdup(value); } else { //abandon the previous value FREE(fb[index].cache[idx].value); fb[index].cache[idx].value = strdup(value); } LIBNV_PRINT("bufset %d '%s'->'%s'\n", index, name, value); fb[index].dirty = 1; return 0; }
char const *nvram_get(int index, char *name) { int idx; static char const *ret; RANV_PRINT("--> nvram_get %d %s\n", index, name); RANV_CHECK_INDEX(NULL); down(&nvram_sem); RANV_CHECK_VALID(); counter++; idx = cache_idx(index, name); if (-1 != idx) { if (fb[index].cache[idx].value) { //duplicate the value in case caller modify it //ret = strdup(fb[index].cache[idx].value); ret = fb[index].cache[idx].value; up(&nvram_sem); return ret; } } //printk("nvram_get: not found\n"); //no default value set? //btw, we don't return NULL anymore! up(&nvram_sem); return NULL; }
int cache_write(struct block_device *dev, uint8_t *buf, size_t buf_size, uint32_t starting_block) { struct cache_dev *cd = (struct cache_dev *)dev; // Only support whole block writes if(buf_size % cd->bd.block_size) { errno = EFAULT; return -1; } // Write through to the underlying device buf_size = cd->parent->write(cd->parent, buf, buf_size, starting_block); // How many blocks? int block_count = buf_size / cd->bd.block_size; // Update the cache if required for(int i = 0; i < block_count; i++) { int idx = cache_idx(cd, starting_block + i); if(cd->cached_blocks[idx] == starting_block + i) qmemcpy((void *)(cd->cache_start + idx * cd->bd.block_size), &buf[i * cd->bd.block_size], cd->bd.block_size); } return buf_size; }
char const *nvram_bufget(int index, char *name) { int idx; static char const *ret; //LIBNV_PRINT("--> nvram_bufget %d\n", index); LIBNV_CHECK_INDEX(""); LIBNV_CHECK_VALID(); idx = cache_idx(index, name); if (-1 != idx) { if (fb[index].cache[idx].value) { //duplicate the value in case caller modify it //ret = strdup(fb[index].cache[idx].value); ret = fb[index].cache[idx].value; LIBNV_PRINT("bufget %d '%s'->'%s'\n", index, name, ret); return ret; } } //no default value set? //btw, we don't return NULL anymore! LIBNV_PRINT("bufget %d '%s'->''(empty) Warning!\n", index, name); return ""; }
static page_descr_t *page_cache_get_descr (void *addr) { page_descr_t *main_descr, *cache_head, *cache_descr; main_descr = main_descr_get(); cache_head = cache_head_get(cache_idx(addr)); for (cache_descr = cache_head->next; cache_descr != cache_head; cache_descr = cache_descr->next) { if (cache_descr->addr == addr) { return cache_descr; } } MEMOPS_PRINTF("%s: cannot get cache page descr\n", __func__); return NULL; }
int nvram_set(int index, char *name, char *value) { int idx; RANV_PRINT("--> nvram_set %d %s=%s\n", index, name, value); RANV_CHECK_INDEX(-1); down(&nvram_sem); RANV_CHECK_VALID(); counter++; idx = cache_idx(index, name); if (-1 == idx) { //find the first empty room for (idx = 0; idx < MAX_CACHE_ENTRY; idx++) { if (!fb[index].cache[idx].name) { break; } } //no any empty room if (idx == MAX_CACHE_ENTRY) { RANV_ERROR("run out of env cache, please increase MAX_CACHE_ENTRY\n"); up(&nvram_sem); return -1; } fb[index].cache[idx].name = kstrdup(name, GFP_KERNEL); fb[index].cache[idx].value = kstrdup(value, GFP_KERNEL); } else { //abandon the previous value kfree(fb[index].cache[idx].value); fb[index].cache[idx].value = kstrdup(value, GFP_KERNEL); } fb[index].dirty = 1; up(&nvram_sem); return 0; }
static int page_cache_add_page (page_descr_t *page_descr, int pool_idx) { page_descr_t *main_descr, *cache_head, *cache_descr; main_descr = main_descr_get(); cache_head = cache_head_get(cache_idx(page_descr->addr)); cache_descr = page_descr_get(); if (cache_descr == NULL) { MEMOPS_PRINTF("%s: cannot get cache page\n", __func__); return -1; } cache_descr->nb = pool_idx | (unsigned long)page_descr; cache_descr->prev = cache_head; cache_descr->next = cache_head->next; cache_descr->addr = page_descr->addr; cache_head->next->prev = cache_descr; cache_head->next = cache_descr; return 0; }
/* * 主要查询函数 */ void consult() { char word[MAX_WORD+1]; char *data=NULL; long idx[MAX_KEYS]; unsigned int offset, length; cache_idx(idx); while(1){ printf("INPUT A WORD OR PHRASE: "); if(get_input(word, MAX_WORD)){ fseek(pidx,locate_idx(word,idx),SEEK_SET); if(search_word(word,&offset,&length)) { data=get_data(offset,length); display_data(data,length); free(data); data=NULL; }else fprintf(stderr,"SORRY, '%s' CANNOT BE FOUND!\n", word); printf("\n----------------------------------------\n\n"); }else break; } }
int cache_read(struct block_device *dev, uint8_t *buf, size_t buf_size, uint32_t starting_block) { struct cache_dev *cd = (struct cache_dev *)dev; #ifdef DEBUG_CACHE printf("CACHE: request for %i bytes starting at block %i\n", buf_size, starting_block); #endif // Is this a multiblock request? Pass through to the parent if(buf_size > cd->bd.block_size) { #ifdef DEBUG_CACHE printf("CACHE: request for buf_size %i - passing through to parent\n", buf_size); #endif return cd->parent->read(cd->parent, buf, buf_size, starting_block); } // Else do we cache this block? int idx = cache_idx(cd, starting_block); void *cache_buf = (void *)(cd->cache_start + cd->bd.block_size * idx); // If not, load it up to the cache if(cd->cached_blocks[idx] != starting_block) { // If write-back, we need to evict a current cache entry and flush it // back to disk // TODO // Load up the new block #ifdef DEBUG_CACHE printf("CACHE: not in cache - requesting from parent\n"); #endif int bytes_read = cd->parent->read(cd->parent, (uint8_t *)cache_buf, buf_size, starting_block); // Only cache if we loaded the entire block if(bytes_read == (int)cd->bd.block_size) { // Store this cache entry cd->cached_blocks[idx] = starting_block; #ifdef DEBUG_CACHE printf("CACHE: storing to slot %i\n", idx); #endif } else { // Invalidate the cache line cd->cached_blocks[idx] = 0xffffffff; } buf_size = bytes_read; } else { #ifdef DEBUG_CACHE printf("CACHE: fetching from cache slot %i\n", idx); #endif } // Copy the cached data to the calling process qmemcpy(buf, cache_buf, buf_size); return buf_size; }