int find_file(const char *filename) { Buffer *bp; char *s; for (bp = head_bp; bp != NULL; bp = bp->next) if (bp->filename != NULL && !strcmp(bp->filename, filename)) { switch_to_buffer(bp); return TRUE; } s = make_buffer_name(filename); if (strlen(s) < 1) { free(s); return FALSE; } if (!is_regular_file(filename)) { minibuf_error("%s is not a regular file", filename); waitkey(WAITKEY_DEFAULT); return FALSE; } bp = create_buffer(s); free(s); bp->filename = zstrdup(filename); switch_to_buffer(bp); read_from_disk(filename); thisflag |= FLAG_NEED_RESYNC; return TRUE; }
static bool disk_ctrl_writeb(void *_disk_state, uint32_t addr, uint8_t val){ disk_state* disk_state = (struct disk_state*) _disk_state; uint32_t offset = addr - DISK_MEM_BASE; if(offset >= DISK_MEM_BORDER){ return false; } /* Adress is in range [0xD000; 0xD400] */ switch (offset) { case DISK_BNR: disk_state->bnr &= ~(0xFF << 24); disk_state->bnr |= val << 24; return true; case DISK_BNR_1: disk_state->bnr &= ~(0xFF << 16); disk_state->bnr |= val << 16; return true; case DISK_BNR_2: disk_state->bnr &= ~(0xFF << 8); disk_state->bnr |= val << 8; return true; case DISK_BNR_3: disk_state->bnr &= ~(0xFF << 0); disk_state->bnr |= val << 0; return true; case DISK_ERR_REG: disk_state->err_reg = val; return true; case DISK_READ_WRITE: disk_state->read_write = val; if(val == 0) { if(!read_from_disk(_disk_state)){ disk_state->err_reg = 1; return false; } return true; } else if(val == 1) { if(!write_to_disk(_disk_state)){ disk_state->err_reg = 1; return false; } return true; } else { return true; } default: /* Check if adress is in range [0xD200, 0xD400] */ if(offset < DISK_MEM_BUF){ return false; } offset = offset - DISK_MEM_BUF; disk_state->buffer[offset] = val; return true; } }
/* * Pin given files of size num_files in the file_cache file_cache *fct * @params file_cache type file_cache, list of file names, number of files * @return void */ void file_cache_pin_files(file_cache *fct, const char **files, int num_files) { // for each file in files // if file exists in cache then increment pin count // else try to find a spot to fill the cache entry with file size_t idx; for (idx = 0; idx < num_files; idx++) { // check if file exists in cache, lock down to prevent duplicate additions pthread_mutex_lock(&(fct->mutex)); file *loc = find_in_cache(fct, files[idx]); // if file already present, then inc #pins if (NULL != loc) { (loc->pins)++; pthread_mutex_unlock(&(fct->mutex)); continue; } // else find a spot to fit the file int spot = findspot_in_cache(fct); // no spot found so continue to see if any other files have existing // pins that can be increased, else dont do anything if (spot == -1) { pthread_mutex_unlock(&(fct->mutex)); continue; } // previously something exists, need to write to disk if dirty and clear if (NULL != fct->file_struct[spot]) { if (true == fct->file_struct[spot]->dirty) { write_to_disk(fct->file_struct[spot]); } // clean the entry in cache free(fct->file_struct[idx]->content); fct->file_struct[idx]->content = NULL; // decrement actual cache size fct->actual_size--; // free that entry free(fct->file_struct[idx]); fct->file_struct[idx] = NULL; } // Yep! this can be improved by simply overwriting the old memory // allocate space fct->file_struct[idx] = malloc(sizeof(file)); // initialize name, pins, dirty flag and read contents fct->file_struct[idx]->file_name = files[idx]; fct->file_struct[idx]->pins = 1; fct->file_struct[idx]->dirty = false; // read contents read_from_disk(fct->file_struct[idx]); // increment cache size fct->actual_size++; pthread_mutex_unlock(&(fct->mutex)); } }
paddr_t swap_in(off_t offset){ //Check for empty page paddr_t pa; pa = user_page_alloc(); int result = read_from_disk(PADDR_TO_KVADDR(pa),offset) ; if (result) { panic("\n swap failed \n") ; } return pa; }
int read_extend(uint32_t extend_no, void *buf) { size_t len = get_extend_size(); int fd = get_disk_fd(); off64_t offset = (uint32_t)extend_no * len; //log_dbg("%u", extend_no); if (read_from_disk(fd, buf, offset, len)) return -1; return 0; }
Model::Model(const cpmf::ModelParams &model_params, const std::shared_ptr<Matrix> R) : params_(model_params), num_users_(R->num_users), num_items_(R->num_items), num_blocks_(R->blocks.size()), P(new float[num_users_ * params_.dim]), Q(new float[num_items_ * params_.dim]) { if (params_.read_model) { read_from_disk(); } else { fill_with_random_value(P, num_users_ * params_.dim); fill_with_random_value(Q, num_items_ * params_.dim); } }
parameter_cache(const device &device) : m_dirty(false), m_device_name(device.name()) { #ifdef BOOST_COMPUTE_USE_OFFLINE_CACHE // get offline cache file name (e.g. /home/user/.boost_compute/tune/device.json) m_file_name = make_file_name(); // load parameters from offline cache file (if it exists) if(boost::filesystem::exists(m_file_name)){ read_from_disk(); } #endif // BOOST_COMPUTE_USE_OFFLINE_CACHE }
int init_super(const char *dev_name) { int fd; if ((vbfs_superblock_disk = Valloc(VBFS_SUPER_SIZE)) == NULL) goto err; memset(vbfs_superblock_disk, 0, VBFS_SUPER_SIZE); fd = open(dev_name, O_RDWR | O_DIRECT | O_LARGEFILE); if (fd < 0) { fprintf(stderr, "open %s error, %s\n", dev_name, strerror(errno)); goto err; } init_vbfs_ctx(fd); if (read_from_disk(fd, vbfs_superblock_disk, VBFS_SUPER_OFFSET, VBFS_SUPER_SIZE)) goto err; if (load_super()) goto err; vbfs_ctx.super.s_mount_time = time(NULL); vbfs_ctx.super.s_state = MOUNT_DIRTY; vbfs_ctx.super.super_vbfs_dirty = SUPER_DIRTY; vbfs_ctx.super.inode_offset = vbfs_ctx.super.extend_bitmap_offset + vbfs_ctx.super.extend_bitmap_count; vbfs_ctx.super.inode_extends = ((__u64)vbfs_ctx.super.s_inode_count * INODE_SIZE) / vbfs_ctx.super.s_extend_size; /* bad extend init */ if (init_bad_extend()) goto err; /* calculate free extend */ /* */ return 0; err: return -1; }
std::string cache_read() { PROFILE_FUNC(); std::string data; PROFILE_START(action_find); // Starts new action which will be inner to ACTION_READ bool found = find_record(); PROFILE_STOP(action_find); if (!found) { PROFILE_BLOCK(load_from_disk); data = read_from_disk(); put_into_cache(data); return data; // Here all action guards are destructed and actions are correctly finished } data = load_from_cache(); return data; }
int main(void) { int option, i = 0; FILE *fp1; point *start =NULL; // root node do // do while loop { //Display user menu printf("\n\n0 - quit\n"); printf("1 - ADD A Record\n"); printf("2 - Remove A Record\n"); printf("3 - Display All Records\n"); printf("4 - Save\n"); printf("5 - Load Saved Data\n"); printf("Enter Option: "); fflush(stdout); scanf("%2d", &option); getchar(); //Process option switch (option) // switch statement to decide next action { case 1: start = add_point(start); break; case 2: display_all(start); start = remove_point(start); break; case 3: display_all(start); break; case 4: write_to_disk(fp1, start); break; case 5: start = NULL; start = read_from_disk(fp1,start); printf("\nSAVED DATA:\n"); break; } }while (option != 0); return 0; }
Page * Pager::fetch_page(Address addr) { Page * res = index.get(addr); if (res){ //cache hit //remove from list res->prev->next = res->next; res->next->prev = res->prev; //insert to head res->next = list_head->next; res->prev = list_head; res->prev->next = res; res->next->prev = res; return res; }else{ //cache miss if (size == max_size){ //replace Address tmp = evict(list_head->prev); index.erase(tmp); --size; } ++size; Page * res = read_from_disk(addr); //insert to head res->next = list_head->next; res->prev = list_head; res->prev->next = res; res->next->prev = res; index.put(addr, res); return res; } }
void process_input(char *input, struct Graph *g){ int i = 0; char name_a[50]; char name_b[50]; char type[5]; sscanf(input, "%s %s %s", type, name_a, name_b); if(!strcmp(type,"new")){ add_new_node(name_a, g); } else if(!strcmp(type,"link")){ add_new_link(name_a, name_b, g); } else if(!strcmp(type,"out")){ print_outgoing_nodes(name_a, g); } else if(!strcmp(type,"in")){ print_incoming_nodes(name_a, g); } else if(!strcmp(type,"wn")){ write_data_prompt(name_a, g); } else if(!strcmp(type,"rn")){ read_data(name_a, g); } else if(!strcmp(type, "all")){ print_graph(g); } else if(!strcmp(type,"wl")){ write_link_data_prompt(name_a, name_b, g); } else if(!strcmp(type,"rl")){ read_link_data(name_a, name_b, g); } else if(!strcmp(type, "path")){ // printf("Hey"); test_for_path(name_a, name_b, g); } else if(!strcmp(type, "save")){ save_to_disk(g, name_a); } else if(!strcmp(type, "load")){ read_from_disk(name_a, g); } else if(!strcmp(type, "getl")){ get_links(g); // all links (node pairs) with X data } else if(!strcmp(type, "getn")){ get_nodes(g); // all nodes with X data } else if(!strcmp(type, "add")){ add_nodes(name_a, name_b, g); } else if(!strcmp(type, "div")){ divide_nodes(name_a, name_b, g); } else if(!strcmp(type, "sub")){ subtract_nodes(name_a, name_b, g); } else if(!strcmp(type, "mult")){ multiply_nodes(name_a, name_b, g); } else if(!strcmp(type, "cmd")){ run_command(name_a, g); } else if(!strcmp(type, "id")){ long long_val; long_val = strtol(name_a, NULL, 10); int id = (int) long_val; struct Node *node; node = get_node_by_id(id, g); if(!strcmp(node->name, "NULL")){ printf("{ \"error\": \"No node with id %d\" }\n", id); free(node->name); free(node); } else { printf("{ \"id: %d, \"name\": \"%s\", \"data\": \"%s\" }\n", id, node->name, node->data); } } }
/******************************************************************************** *Function: search_chunk *Description: Analyze the given chunk by running each defined search spec on it *Return: TRUE/FALSE **********************************************************************************/ int search_chunk(f_state *s, unsigned char *buf, f_info *i, u_int64_t chunk_size, u_int64_t f_offset) { u_int64_t c_offset = 0; //u_int64_t foundat_off = 0; //u_int64_t buf_off = 0; unsigned char *foundat = buf; unsigned char *current_pos = NULL; unsigned char *header_pos = NULL; unsigned char *newbuf = NULL; unsigned char *ind_ptr = NULL; u_int64_t current_buflen = chunk_size; int tryBS[3] = { 4096, 1024, 512 }; unsigned char *extractbuf = NULL; u_int64_t file_size = 0; s_spec *needle = NULL; int j = 0; int bs = 0; int rem = 0; int x = 0; int found_ind = FALSE; off_t saveme; //char comment[32]; for (j = 0; j < s->num_builtin; j++) { needle = &search_spec[j]; foundat = buf; /*reset the buffer for the next search spec*/ #ifdef DEBUG printf(" SEARCHING FOR %s's\n", needle->suffix); #endif bs = 0; current_buflen = chunk_size; while (foundat) { needle->written = FALSE; found_ind = FALSE; memset(needle->comment, 0, COMMENT_LENGTH - 1); if (chunk_size <= (foundat - buf)) { #ifdef DEBUG printf("avoided seg fault in search_chunk()\n"); #endif foundat = NULL; break; } current_buflen = chunk_size - (foundat - buf); //if((foundat-buf)< 1 ) break; #ifdef DEBUG //foundat_off=foundat; //buf_off=buf; //printf("current buf:=%llu (foundat-buf)=%llu \n", current_buflen, (u_int64_t) (foundat_off - buf_off)); #endif if (signal_caught == SIGTERM || signal_caught == SIGINT) { user_interrupt(s, i); printf("Cleaning up.\n"); signal_caught = 0; } if (get_mode(s, mode_quick)) /*RUN QUICK SEARCH*/ { #ifdef DEBUG //printf("quick mode is on\n"); #endif /*Check if we are not on a block head, adjust if so*/ rem = (foundat - buf) % s->block_size; if (rem != 0) { foundat += (s->block_size - rem); } if (memwildcardcmp(needle->header, foundat, needle->header_len, needle->case_sen ) != 0) { /*No match, jump to the next block*/ if (current_buflen > s->block_size) { foundat += s->block_size; continue; } else /*We are out of buffer lets go to the next search spec*/ { foundat = NULL; break; } } header_pos = foundat; } else /**********RUN STANDARD SEARCH********************/ { foundat = bm_search(needle->header, needle->header_len, foundat, current_buflen, //How much to search through needle->header_bm_table, needle->case_sen, //casesensative SEARCHTYPE_FORWARD); header_pos = foundat; } if (foundat != NULL && foundat >= 0) /*We got something, run the appropriate heuristic to find the EOF*/ { current_buflen = chunk_size - (foundat - buf); if (get_mode(s, mode_ind_blk)) { #ifdef DEBUG printf("ind blk detection on\n"); #endif //dumpInd(foundat+12*1024,1024); for (x = 0; x < 3; x++) { bs = tryBS[x]; if (ind_block(foundat, current_buflen, bs)) { if (get_mode(s, mode_verbose)) { sprintf(needle->comment, " (IND BLK bs:=%d)", bs); } //dumpInd(foundat+12*bs,bs); #ifdef DEBUG printf("performing mem move\n"); #endif if(current_buflen > 13 * bs)//Make sure we have enough buffer { if (!memmove(foundat + 12 * bs, foundat + 13 * bs, current_buflen - 13 * bs)) break; found_ind = TRUE; #ifdef DEBUG printf("performing mem move complete\n"); #endif ind_ptr = foundat + 12 * bs; current_buflen -= bs; chunk_size -= bs; break; } } } } c_offset = (foundat - buf); current_pos = foundat; /*Now lets analyze the file and see if we can determine its size*/ // printf("c_offset=%llu %x %x %llx\n", c_offset,foundat,buf,c_offset); foundat = extract_file(s, c_offset, foundat, current_buflen, needle, f_offset); #ifdef DEBUG if (foundat == NULL) { printf("Foundat == NULL!!!\n"); } #endif if (get_mode(s, mode_write_all)) { if (needle->written == FALSE) { /*write every header we find*/ if (current_buflen >= needle->max_len) { file_size = needle->max_len; } else { file_size = current_buflen; } sprintf(needle->comment, " (Header dump)"); extractbuf = (unsigned char *)malloc(file_size * sizeof(char)); memcpy(extractbuf, header_pos, file_size); write_to_disk(s, needle, file_size, extractbuf, c_offset + f_offset); free(extractbuf); } } else if (!foundat) /*Should we search further?*/ { /*We couldn't determine where the file ends, now lets check to see * if we should try again */ if (current_buflen < needle->max_len) /*We need to bridge the gap*/ { #ifdef DEBUG printf(" Bridge the gap\n"); #endif saveme = ftello(i->handle); /*grow the buffer and try to extract again*/ newbuf = read_from_disk(c_offset + f_offset, i, needle->max_len); if (newbuf == NULL) break; current_pos = extract_file(s, c_offset, newbuf, needle->max_len, needle, f_offset); /*Lets put the fp back*/ fseeko(i->handle, saveme, SEEK_SET); free(newbuf); } else { foundat = header_pos; /*reset the foundat pointer to the location of the last header*/ foundat += needle->header_len + 1; /*jump past the header*/ } } } if (found_ind) { /*Put the ind blk back in, re-arrange the buffer so that the future blks names come out correct*/ #ifdef DEBUG printf("Replacing the ind block\n"); #endif /*This is slow, should we do this??????*/ if (!memmove(ind_ptr + 1 * bs, ind_ptr, current_buflen - 13 * bs)) break; memset(ind_ptr, 0, bs - 1); chunk_size += bs; memset(needle->comment, 0, COMMENT_LENGTH - 1); } } //end while } return TRUE; }