void brelse(buf *buffer){ //wakeup(); if(CheckStatus(buffer, STAT_LOCKED)){ printf("Wakeup processes wating for any buffer\n"); if(CheckStatus(buffer, STAT_LOCKED | STAT_VALID | STAT_WAITED)){ //wakeup(); printf("Wakeup processes waiting for buffer of blkno %d\n", buffer -> blkno); } //raise_cpu_level(); if(CheckStatus(buffer, STAT_VALID) && !CheckStatus(buffer, STAT_OLD)){ printf("Enqueued to the tail of free list\n"); insert_list(&f_head, buffer, FREETAIL); MakeStatus(buffer, STAT_VALID); } else{ printf("Enqueued to the head of free list\n"); insert_list(&f_head, buffer, FREEHEAD); MakeStatus(buffer, STAT_VALID); } //lower_cpu_level(); } else{ printf("Error! buffer should be locked\n"); } }
int main(void){ int i; mlist *mylist, *temp; insert_list(mylist,2); insert_list(mylist,5); insert_list(mylist,1); insert_list(mylist,3); mylist = head; while(mylist) { mylist = mylist->next; } temp = search_list(mylist,2); __VERIFIER_assert(temp->key==2); delete_list(temp); mylist = head; while(mylist) { mylist = mylist->next; } return 0; }
/** * Allocation * @author Ivan Gualandri * @version 1.0 * @param size Size of the memory to be allocated * @param cur_heap Current heap * @return The start address of the new allocated memory (or NULL if no memory can be allocated) */ void *alloc(unsigned int size, heap_t *cur_heap) { heap_node_t* new_node = NULL; heap_node_t* free_heap_list = cur_heap->free_list; heap_node_t* prev_node; #ifdef DEBUG printf("----\n"); printf("Required Size: %d ", size); #endif prev_node = free_heap_list; /* Look for a free block of memory in the heap's free memory list */ while(free_heap_list) { if(free_heap_list->size >= size) { #ifdef DEBUG printf("Available_pages: %d\n", free_heap_list->size / 4096); #endif /*Se lo spazio disponibile e' maggiore di quello richiesto*/ if(free_heap_list->size > size){ #ifdef DEBUG printf("Node should be splitted\n"); #endif new_node = (heap_node_t*)alloc_node(); new_node->start_address = free_heap_list->start_address; new_node->next = NULL; new_node->size = size; insert_list (new_node, &(cur_heap->used_list)); free_heap_list->size = (free_heap_list->size) - (size); free_heap_list->start_address = free_heap_list->start_address + (size); #ifdef DEBUG printf("New_node -> Size: %d, start_address: %d\n", new_node->size, new_node->start_address); #endif } /*Se lo spazio richiesto e' uguale a quello disponibile*/ else if(free_heap_list->size == size){ if(prev_node == free_heap_list) { kheap->free_list = (heap_node_t*)free_heap_list->next; } else prev_node->next = free_heap_list->next; insert_list (free_heap_list, &(cur_heap->used_list)); return (void *)free_heap_list->start_address; } #ifdef DEBUG printf("free_heap_list -> Actual size: %d, start_address: %d\n", free_heap_list->size, free_heap_list->start_address); printf("----\n"); #endif break; } else { prev_node = free_heap_list; free_heap_list = (heap_node_t*)free_heap_list->next; } } #ifdef DEBUG printf("Prev_node: %d \n", prev_node->start_address); printf("New Address: %d ", new_node->start_address); #endif return (void *)new_node->start_address; }
/** * 配列に単語を追加する * @param char *str 追加する文字列 * @param int doc_index 現在のドキュメントのID * @return int 挿入位置 */ int add_array(char *str, int doc_index) { int i; ARRAY **tmp; // 配列の中を探す for (i = 0; i < n_words; i++) { if (strcmp(words[i]->word, str) == 0) { if (insert_list(&(words[i]->indices), doc_index)) // 単語の出現行番号を保存 words[i]->freq++; // 重複がなければ出現頻度を増やす return i; // ヒットしたIDを返す } } // 単語の格納配列の大きさが足りないとき if (n_words >= n_words_max) { n_words_max *= 2; // 2倍の大きさにする // メモリを再度割り当てる if ((tmp = (ARRAY **)realloc(words, sizeof(ARRAY *) * n_words_max)) == NULL) { fprintf(stderr, "Memory Reallocation Error\n"); exit(1); } else { words = tmp; // 新しい領域を指す } } // 新しく作成 words[n_words] = (ARRAY *)malloc(sizeof(ARRAY)); if (words[n_words] == NULL) { fprintf(stderr, "Memory Allocation Error\n"); exit(1); } words[n_words]->word = (char *)malloc(sizeof(char) * (strlen(str) + 1)); if (words[n_words]->word == NULL) { fprintf(stderr, "Memory Allocation Error\n"); exit(1); } // 文字列のコピー memcpy(words[n_words]->word, str, sizeof(char) * (strlen(str) + 1)); // 各種値の初期化 words[n_words]->freq = 1; words[n_words]->indices.next = NULL; insert_list(&(words[n_words]->indices), doc_index); // 現在の行の番号を追加 words[n_words]->second_words.left = &(words[n_words]->second_words); // 初期化 words[n_words]->second_words.right = &(words[n_words]->second_words); // 初期化 return n_words++; // 追加した位置を返す }
/** * insert_list() - * p points to a list of dict_nodes connected by their left pointers. * l is the length of this list (the last ptr may not be NULL). * It inserts the list into the dictionary. * It does the middle one first, then the left half, then the right. * * Note: I think this insert middle, then left, then right, has * its origins as a lame attempt to hack around the fact that the * resulting binary tree is rather badly unbalanced. This has been * fixed by using the DSW rebalancing algo. Now, that would seem * to render this crazy bisected-insertion algo obsoloete, but .. * oddly enough, it seems to make the DSW balancing go really fast! * Faster than a simple insertion. Go figure. I think this has * something to do with the fact that the dictionaries are in * alphabetical order! This subdivision helps randomize a bit. */ static void insert_list(Dictionary dict, Dict_node * p, int l) { Dict_node * dn, *dn_head, *dn_second_half; int k, i; /* length of first half */ if (l == 0) return; k = (l-1)/2; dn = p; for (i = 0; i < k; i++) { dn = dn->left; } /* dn now points to the middle element */ dn_second_half = dn->left; dn->left = dn->right = NULL; if (contains_underbar(dn->string)) { insert_idiom(dict, dn); } else if (is_idiom_word(dn->string)) { err_ctxt ec; ec.sent = NULL; err_msg(&ec, Warn, "Warning: Word \"%s\" found near line %d.\n" "\tWords ending \".Ix\" (x a number) are reserved for idioms.\n" "\tThis word will be ignored.\n", dn->string, dict->line_number); free_dict_node(dn); } else if ((dn_head = abridged_lookup_list(dict, dn->string)) != NULL) { Dict_node *dnx; err_ctxt ec; ec.sent = NULL; err_msg(&ec, Warn, "Warning: The word \"%s\" " "found near line %d of %s matches the following words:\n", dn->string, dict->line_number, dict->name); for (dnx = dn_head; dnx != NULL; dnx = dnx->right) { fprintf(stderr, "\t%s", dnx->string); } fprintf(stderr, "\n\tThis word will be ignored.\n"); free_lookup_list(dn_head); free_dict_node(dn); } else { dict->root = insert_dict(dict, dict->root, dn); dict->num_entries++; } insert_list(dict, p, k); insert_list(dict, dn_second_half, l-k-1); }
int main(void){ int i; mlist *mylist, *temp; insert_list(mylist,2); insert_list(mylist,5); insert_list(mylist,1); insert_list(mylist,3); temp = search_list(head,2); __VERIFIER_assert(temp->key==1); }
void find_corners() { line_point *p; /*for (int x = 0; x < LEVEL_MAX_DIM; ++x) { for (int y = 0; y < LEVEL_MAX_DIM; ++y) { if (is_solid_tile(x, y)) { if (!is_solid_tile(x-1, y) && !is_solid_tile(x, y-1)) { p = (line_point *) malloc(sizeof(line_point)); p->x = (x) * TILE_DIM; p->y = (y) * TILE_DIM; insert_list(CORNERS, p); } if (!is_solid_tile(x-1, y) && !is_solid_tile(x, y+1)) { p = (line_point *) malloc(sizeof(line_point)); p->x = (x) * TILE_DIM; p->y = (y + 1) * TILE_DIM; insert_list(CORNERS, p); } if (!is_solid_tile(x+1, y) && !is_solid_tile(x, y-1)) { p = (line_point *) malloc(sizeof(line_point)); p->x = (x + 1) * TILE_DIM; p->y = (y) * TILE_DIM; insert_list(CORNERS, p); } if (!is_solid_tile(x+1, y) && !is_solid_tile(x, y+1)) { p = (line_point *) malloc(sizeof(line_point)); p->x = (x + 1) * TILE_DIM; p->y = (y + 1) * TILE_DIM; insert_list(CORNERS, p); } } } }*/ p = (line_point *) malloc(sizeof(line_point)); p->x = (14 + 0.5) * TILE_DIM; p->y = (0 + 0.5) * TILE_DIM; insert_list(CORNERS, p); p = (line_point *) malloc(sizeof(line_point)); p->x = (14 + 0.5) * TILE_DIM; p->y = (9 + 0.5) * TILE_DIM; insert_list(CORNERS, p); /*p = (line_point *) malloc(sizeof(line_point)); p->x = (25 + 0.5) * TILE_DIM; p->y = (0 + 0.5) * TILE_DIM; insert_list(CORNERS, p);*/ p = (line_point *) malloc(sizeof(line_point)); p->x = (25 + 0.5) * TILE_DIM; p->y = (9 + 0.5) * TILE_DIM; insert_list(CORNERS, p); }
Node *insert_triple_child(node_type type, Node *n1, Node *n2, Node *n3) { Node *node = (Node*) malloc(sizeof(Node)); node->type = type; node->next = NULL; n2 = insert_list(n2, n3); n1 = insert_list(n1, n2); node->childs = n1; return node; }
void SourceTreeItem::insert_child( SourceTreeItem * child, SourceTreeItem * where) { SourceTreeItem * my_part = child->next_; child->parent_ = this; my_part->parent_ = parent_; my_part->owner_ = owner_; if (where) { insert_list(child, where->prev_, my_part); insert_list(my_part, child, where); } else { } }
/* * Requires: * "bp" is the address of a newly freed block. * * Effects: * Perform boundary tag coalescing. */ static void * coalesce(void *bp) { size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp))); size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp))) || PREV_BLKP(bp) == bp; size_t size = GET_SIZE(HDRP(bp)); /* Next block is free */ if (prev_alloc && !next_alloc) { size += GET_SIZE(HDRP(NEXT_BLKP(bp))); remove_list(NEXT_BLKP(bp)); PUT(HDRP(bp), PACK(size, 0)); PUT(FTRP(bp), PACK(size, 0)); } /* Previous block is free */ else if (!prev_alloc && next_alloc) { size += GET_SIZE(HDRP(PREV_BLKP(bp))); bp = PREV_BLKP(bp); remove_list(bp); PUT(HDRP(bp), PACK(size, 0)); PUT(FTRP(bp), PACK(size, 0)); } /* Both blocks are free */ else if (!prev_alloc && !next_alloc) { size += GET_SIZE(HDRP(PREV_BLKP(bp))) + GET_SIZE(HDRP(NEXT_BLKP(bp))); remove_list(PREV_BLKP(bp)); remove_list(NEXT_BLKP(bp)); bp = PREV_BLKP(bp); PUT(HDRP(bp), PACK(size, 0)); PUT(FTRP(bp), PACK(size, 0)); } /*insert bp,free block into free list*/ insert_list(bp); return bp; }
/* Appends the element 'elem' to the end of a list. Returns 1 if everything went ok , else it returns -1 */ int append_list(TYPE_ elem, NODE **Head) { NODE *p; reverse_list(Head); insert_list(elem, 0, Head); reverse_list(Head); return 1; }
void putFileinList(list_ref list, FILE *input, char *filename, int after) { char buffer[1024]; int linenr = 1; for (; ; ++linenr) { char *linepos = fgets (buffer, sizeof buffer, input); if (linepos == NULL) break; linepos = strchr (buffer, '\n'); if (linepos == NULL) { fflush (NULL); fprintf (stderr, "%s: %s[%d]: unterminated line\n", Exec_Name, filename, linenr); fflush (NULL); Exit_Status = EXIT_FAILURE; } else { *linepos = '\0'; } linepos = strdup (buffer); assert (linepos != NULL); if(after == 1) { insert_list (list, linepos); } else { insertAfter(list, linepos, last); } } if(after == 1) { printf("%s: %d lines read from %s.\n", Exec_Name, counter(list, last), filename); } else { printf("%s: %d lines read from %s.\n", Exec_Name, linenr-1, filename); } }
hash_map* lat_clonar_hash(lat_vm* vm, hash_map* h) { int c = 0; hash_map* ret = make_hash_map(); list_node* l; for (c = 0; c < 256; ++c) { l = h->buckets[c]; if (l != NULL) { ret->buckets[c] = lat_crear_lista(); if (l != NULL) { list_node* cur; for (cur = l->next; cur != NULL; cur = cur->next) { if (cur->data != NULL) { hash_val* hv = (hash_val*)lat_asignar_memoria(sizeof(hash_val)); //vm->memory_usage += sizeof(hash_val); strncpy(hv->key, ((hash_val*)cur->data)->key, 256); hv->val = lat_clonar_objeto(vm, (lat_objeto*)((hash_val*)cur->data)->val); insert_list(ret->buckets[c], hv); } } } } } return ret; }
heap_t *create_heap(uint32_t start, uint32_t end, uint32_t max, uint8_t supervisor, uint8_t readonly) { heap_t *heap = (heap_t*) kmalloc(sizeof(heap_t)); ASSERT((start % 0x1000 == 0)); ASSERT((end % 0x1000 == 0)); heap->index = place_list((type_t*) start, HEAP_INDEX_SIZE, &header_t_lessthan); start += sizeof(type_t) * HEAP_INDEX_SIZE; if ((start & 0xFFFFF000) != 0) { start &= 0xFFFFF000; start += 0x1000; } heap->start_address = start; heap->end_address = end; heap->max_address = max; heap->supervisor = supervisor; heap->readonly = readonly; header_t *hole = (header_t*) start; hole->size = end - start; hole->magic = HEAP_MAGIC; hole->is_hole = 1; insert_list((type_t) hole, &heap->index); return heap; }
void process(char * file_name) { struct stat info; stat(file_name, &info); if(S_ISDIR(info.st_mode)) print_error("the path is a directory"); char buffer[MAX_LINE_LENGTH + 2]; buffer[MAX_LINE_LENGTH] = '\0'; My402List list; memset(&list, 0, sizeof(My402List)); (void)My402ListInit(&list); FILE * file; if(file_name) { if((file = fopen(file_name, "r")) == NULL) print_error("cannot open file"); } else file = stdin; while(!feof(file)) { if(fgets(buffer, MAX_LINE_LENGTH + 2, file)) insert_list(&list, parse_file(buffer)); } fclose(file); print_list(&list); }
void AddToHash(buf *elem){ int key = elem -> blkno; int hkey = key % 4; AddStatus(elem, STAT_LOCKED); //insert_hash_head(h_head[hkey].hash_bp, elem); insert_list(&h_head[hkey], elem, HASHTAIL); }
int readFile(list_ref list, char* filename, struct options* options) { int lineNum = 0; char buf[1024]; FILE *f = fopen(filename, "r"); if(f == NULL) { eprintf("Error opening %s\n", filename); fclose(f); return -1; } for(;;) { char *linepos = fgets( buf, sizeof buf, f ); linepos = strchr( buf, '\n' ); if( linepos == NULL || buf[0] == '\0' ){ break; }else{ *linepos = '\0'; insert_list(list, buf); lineNum++; } } fclose(f); return lineNum; }
int update_face(face *f, face_list *AFL) { if (member_list(AFL, f)){ return delete_list(AFL, f); } else return insert_list(AFL, f); }
void Query::build_index(People *p, int key, enum parameter_t pm) { //得到某个二级属性节点 List *list = find_list(key, pm); //将查到的people指针插入到二级属性节点后面 insert_list(list, p, pm); }
int merge_right_inter(char *key, t_bind *bind, t_bdb **foot) { int size; t_bind *inter; if ((*foot) && (inter = (*foot)->keybind)) { if (!(bind->key = strdup(key))) fprintf(stderr, "Error allocate memory\n"); else { if (inter->key2) size = strlen(inter->key2) - 1; if (inter->key2 && (inter->key2[size] > 0)) inter->key2[size] = (inter->key2[size] - 1); bind->key2 = NULL; bind->flags = 0; if (!(insert_list(foot, bind))) xfree(bind->key); else return (EXIT_SUCCESS); } } xfree(bind); return (EXIT_FAILURE); }
int main(int argc, char *argv[]) { FILE *fp; int no, x; char name[10], buf[256]; struct record *p; if (argc != 2) { printf("missing file argument.\n"); return 1; } fp = fopen(argv[1], "r"); if (fp == NULL) { printf("can't open %s\n", argv[1]); return 1; } while (fgets(buf, sizeof(buf), fp) != NULL) { sscanf(buf, "%d %s %d", &no, name, &x); insert_list(no, name, x); } fclose(fp); for (p = head; p != NULL; p = p->next) printf("%d %s %d\n", p->no, p->name, p->point); return 0; }
int main(int argc, const char *argv[]) { NODE_T *head = NULL; NODE_T *mp = NULL; int key = 4; while(1) { mp = getNode(); head = insert_list(head, mp); trav_list(head); mp = find_list(head, key); if(mp) { print_node(mp); head = delete_list(head, mp); } else { printf("no age %d\n", key); } } return 0; //sdf }
void set_hash(hash_map *m, char *key, void *val) { hash_val *hv = (hash_val *)lat_asignar_memoria(sizeof(hash_val)); strncpy(hv->key, key, (strlen(key)+1)); hv->val = val; int hk = hash(key); if (m->buckets[hk] == NULL) { m->buckets[hk] = lat_crear_lista(); } else { list_node *c; for (c = m->buckets[hk]; c->next != NULL; c = c->next) { if (c->data != NULL) { if (strcmp(((hash_val *)c->data)->key, key) == 0) { free(c->data); c->data = (void *)hv; return; } } } } insert_list(m->buckets[hk], (void *)hv); }
int main() { PNODE pHead = NULL; int val; pHead = create_list(); traverse_list(pHead); insert_list(pHead, 3, 89); traverse_list(pHead); delete_list(pHead, 4, &val); traverse_list(pHead); //if (is_empty(pHead)) // printf("链表为空\n"); //else // printf("链表不为空\n"); //printf("链表的长度为:%d\n", length_list(pHead)); //sort_list(pHead); //traverse_list(pHead); return 0; }
ttrie_t *insert_ttrie(ttrie_t *p, char *s, uintptr_t val) { if (!p) p = alloc_ttrie(*s); if (*s < p->c) p->lo = insert_ttrie(p->lo, s, val); else if (*s > p->c) p->hi = insert_ttrie(p->hi, s, val); else if (*s) p->eq = insert_ttrie(p->eq, s+1, val); else p->list = insert_list(p->list,val); return p; }
/* [0,1) */ void BUCKETSORT(int A[], int array_size) { int n = array_size; for (int i = 1; i <= n; i++) insert_list(B[n*A[i]], A[i]); for (int i = 0; i <= n - 1; i++) INSERTIONSORT(B[i], B[i].array_size); }
/* Inserts a block in the hash table */ void insert_hash(block_node *b) { int i; i = hash_block(b->block); //pthread_mutex_lock(&hash_mutex); ht[i].active = ht[i].active | (unsigned char)0xFF; /* Uuuu....deep magic...I wonder what this does... */ insert_list(&(ht[i].list), b); //pthread_mutex_unlock(&hash_mutex); }
list_node* lat_clonar_lista(lat_vm* vm, list_node* l) { list_node* ret = lat_crear_lista(); if (l != NULL) { list_node* c; for (c = l->next; c != NULL; c = c->next) { if (c->data != NULL) { insert_list(ret, lat_clonar_objeto(vm, (lat_objeto*)c->data)); } } } return ret; }
int main() { int round,i,tmp; node_ptr root=NULL; scanf("%d",&round); for(i=0;i<round;i++) { scanf("%d",&tmp); root=insert_list(root,tmp); } root=rsort(root); print_list(root); }
int main(int argc, char *argv[]){ LIST L1, L2; int i; L1 = init_list(); L2 = init_list(); for(i=0; i<3; i++){ insert_list(random(10), L1); insert_list(random(10), L2); } print_list(L1); print_list(L2); add_lists(L1, L2); print_list(L2); delet_list(L1); delet_list(L2); }