static int init_bucket_node(BucketNode * const bucket_node, const QuadNode * const parent) { bucket_node->type = NODE_TYPE_BUCKET_NODE; assert(parent->type == NODE_TYPE_QUAD_NODE); bucket_node->parent = (QuadNode *) parent; init_bucket(&bucket_node->bucket); return 0; }
long long create_overflow_bucket(INDEX_FILE *index_file){ FILE *fp; long long free_head; void * bucket; long long n_buckets; bucket = calloc(BSIZE,sizeof(char)); if (bucket==NULL){ printf("Malloc failed"); exit(1); } fp=fopen(index_file->overflow_path,"r+"); if (fp == NULL){ printf("fail to open file\n"); exit(1); } fseek(fp, 3*sizeof(long long), SEEK_SET); fread(&free_head, sizeof(long long),1,fp); fread(&n_buckets, sizeof(long long),1,fp); if (free_head == -1){ init_bucket(bucket); write_bucket(bucket, n_buckets, index_file->overflow_path); n_buckets++; fseek(fp, 4*sizeof(long long), SEEK_SET); fwrite(&n_buckets, sizeof(long long),1,fp); fclose(fp); free(bucket); return n_buckets-1; //allocate new } else { read_bucket(bucket, free_head, index_file->overflow_path); long long next_free = ((long long*)(bucket+4))[0]; fseek(fp, 3*sizeof(long long), SEEK_SET); fwrite(&next_free, sizeof(long long), 1, fp); init_bucket(bucket); write_bucket(bucket, free_head, index_file->overflow_path); fclose(fp); free(bucket); return free_head; } }
void bucket_sort(int *list, int l, int r) { int i, size, max=0; for (i = l; i <= r; i ++) if (list[i]>max) max = list[i]; size = max/10; init_bucket(size); insert_bucket(list, l, r); sort_bucket(list, size); del_bucket(size); }
void add(struct datum * key, struct datum * data, struct hashmap * map){ size_t index = hash(key); if(index < map->index_size){ struct bucket * b = init_bucket(); b->val = data; b->key = key; b->next = map->table[index]; map->table[index] = b; map->size++; } else { fprintf(stderr, "Hash key (%lu) is out of bounds\n", index); exit(EXIT_FAILURE); } }
void init_index(int i, HFILE *hf){ int j; INDEX_FILE indexfile; long long bigzero = 0; long long bignull = -1; FILE *fp1, *fp2; void* bucket; indexfile.next=0; indexfile.s=4; indexfile.n=4; int length = strlen(hf->path)+10; indexfile.main_path = calloc(length,sizeof(char)); indexfile.overflow_path = calloc(length,sizeof(char)); get_index_file_name(hf->path, i+1, indexfile.main_path, indexfile.overflow_path); indexfile.type = hf->schema[i]; indexfile.column_number=i; fp1 = fopen(indexfile.main_path,"w"); if (fp1==NULL){ printf("fail to create file."); exit(1); } fp2 = fopen(indexfile.overflow_path,"w"); if (fp2==NULL){ printf("fail to create file."); exit(1); } bucket = calloc(BSIZE,sizeof(char)); if (bucket==NULL){ printf("Malloc failed"); exit(1); } init_bucket(bucket); fclose(fp1); for(j=0;j<4;j++){ write_bucket(bucket, j, indexfile.main_path); } fwrite(&indexfile.next, sizeof(long long), 1, fp2); fwrite(&indexfile.s, sizeof(long long), 1, fp2); fwrite(&indexfile.n, sizeof(long long), 1, fp2); fwrite(&bignull, sizeof(long long), 1, fp2); fwrite(&bigzero, sizeof(long long), 1, fp2); free(bucket); fclose(fp2); return; }
void rehash(HFILE *hf, int i, INDEX_FILE *index_file){ void *bucket; bucket = calloc(BSIZE,sizeof(char)); if (bucket==NULL){ printf("Malloc failed"); exit(1); } init_bucket(bucket); write_bucket(bucket, index_file->n, index_file->main_path); rehash_keys(hf, i, index_file); index_file->n++; index_file->next++; if (index_file->next == index_file-> s){ index_file->next = 0; index_file->s *= 2; } free(bucket); }
double twenty_lines(){ clear(); int board_x = 15; int board_y = 1; int board_width = 10; int board_height = 20; //board boarders int i; for(i = 0; i < board_height; i++){ mvprintw(i + board_y, board_x, "|"); mvprintw(i + board_y, (board_width * 2) + (board_x) + 1, "|", i); } mvprintw(board_y + board_height, board_x, "+"); for(i = 1; i < (board_width * 2) + 1; i++) mvprintw(board_y + board_height, i + board_x, "-"); mvprintw(board_y + board_height, board_x + (board_width * 2) + 1, "+"); //bucket init_bucket(&bu); //shape new_shape(&s, &bu); //board b = init_board(board_width, board_height); int running = 1; int lines = 0; //drop loop params thread_params tp; tp.b = &b; tp.s = &s; tp.bucket = &bu; tp.hold_cnt = &hold_cnt; tp.drop_speed = &drop_speed; tp.running = &running; tp.lines_cleared = &lines; pthread_t pth; pthread_create(&pth, NULL, drop_loop, (void * ) &tp); struct timeval tval_before, tval_after, tval_result; gettimeofday(&tval_before, NULL); char tmbuf[64]; // game loop while(running > 0 && lines < 10){ timeout(100); draw_board(b, board_x, board_y); draw_hold(h_s, hold_cnt, 1, 1); draw_shape(s, board_x, board_y); draw_bucket(&bu, 45, 1); mvprintw(10, 2, "Lines: %d", lines); gettimeofday(&tval_after, NULL); timersub(&tval_after, &tval_before, &tval_result); strftime(tmbuf, sizeof tmbuf, "%M:%S", localtime(&tval_result.tv_sec)); mvprintw(14, 2, " "); mvprintw(14, 2, "%s.%02d", tmbuf, tval_result.tv_usec/10000); int ch = getch(); if(ch != ERR){ //hold - temporarily cycles trough if(ch == 'x'){ s.type++; if(s.type == 7) s.type = 0; //left }else if(ch == KEY_LEFT && check_move(s, b, -1, 0, 0)){ s.x--; //right }else if(ch == KEY_RIGHT && check_move(s, b, 1, 0, 0)){ s.x++; //rotate }else if(ch == KEY_UP && check_move(s, b, 0, 0, 1)){ s.rotation++; if(s.rotation == 4) s.rotation = 0; //hard drop }else if(ch == ' '){ while(true){ if(!check_move(s, b, 0, 1, 0)) break; s.y++; } //hold }else if(ch == 'z' && hold_cnt < 1){ hold(&s, &h_s, &bu); hold_cnt++; } } refresh(); if(DEBUG_CNT == 20) DEBUG_CNT = 0; } //stop drop loop running = -1; //delay for drop loop to stop usleep(1000000); clear(); return 1; }
void init_palloc() { kprintf("Initializing page frame allocator\n"); // Initialize bucket list int j, k; for (j = 0; j < PALLOC_BUCKET_COUNT; j++) { buckets[j].bucket_tag = j; buckets[j].total_pages = 0; buckets[j].avail_pages = 0; for (k = 0; k < PALLOC_ORDER_COUNT; k++) { buckets[j].buddies[k].value = 0; } spin_init(&buckets[j].lock); } // Calculate total number of nodes - 1 page needs 1 node ulong node_count = hal->paddr_space_end / PAGE_SIZE; ulong node_size = node_count * sizeof(struct palloc_node); kprintf("\tPalloc node size: %d KB, pages: %d\n", node_size / 1024, node_size / PAGE_SIZE); // Allocate and reserve memory nodes = (struct palloc_node *)hal->free_mem_start_addr; reserve_pfndb_mem(hal->free_mem_start_addr, node_size); hal->free_mem_start_addr += node_size; // Initialize all nodes ulong i; for (i = 0; i < node_count; i++) { nodes[i].avail = 0; nodes[i].tag = PALLOC_DUMMY_BUCKET; } // Go through PFN database to construct tags array struct pfndb_entry *entry = get_pfn_entry_by_paddr(0); int cur_tag = entry->tag; int recording = 0; ulong cur_bucket_start = 0; for (i = 0; i < hal->paddr_space_end; i += PAGE_SIZE) { entry = get_pfn_entry_by_paddr(i); // End of a bucket if ( recording && (!entry->usable || entry->inuse || cur_tag != entry->tag) ) { init_bucket(cur_bucket_start, i - cur_bucket_start, cur_tag); recording = 0; } // Start of a bucket else if ( !recording && (entry->usable && !entry->inuse && entry->tag != PALLOC_DUMMY_BUCKET) ) { cur_bucket_start = i; cur_tag = entry->tag; recording = 1; } } // Take care of the last bucket if (recording) { init_bucket(cur_bucket_start, hal->paddr_space_end - cur_bucket_start, cur_tag); } // Print the buddy buddy_print(); }