static int mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf){ struct page *page; // printk(KERN_INFO "mmap_fault()\n"); /* the data is in vma->vm_private_data */ mmap_pointer = (struct mmap_info *)vma->vm_private_data; if (!mmap_pointer->data) { printk("no data\n"); return -1; } /* get the page */ page = virt_to_page(mmap_pointer->data); /* increment the reference count of this page */ get_page(page); vmf->page = page; insert_node(task_pid_nr(current)); return 0; }
/** * Copies @a tree and all its entries. * @param[in] tree Tree being copied. * @return New tree. */ piojo_btree_t* piojo_btree_copy(const piojo_btree_t *tree) { piojo_btree_t *newtree; const void *key; void *data; PIOJO_ASSERT(tree); newtree = piojo_btree_alloc_cb_cmp(tree->cmax, tree->evsize, tree->cmp_cb, tree->eksize, tree->allocator); newtree->ecount = tree->ecount; key = piojo_btree_first(tree, &data); while (key != NULL){ insert_node(key, data, newtree); key = piojo_btree_next(key, tree, &data); } return newtree; }
/* * free - Free a block */ void free(void *bp) { if (bp == 0) return; size_t size = GET_SIZE(HDRP(bp)); if (heap_listp == 0){ mm_init(); } PUT(HDRP(bp), PACK(size, 0)); PUT(FTRP(bp), PACK(size, 0)); insert_node(bp, size); coalesce(bp); line_count++; if (CHECK && CHECK_FREE) { mm_check('f', bp, size); } }
bool get_words_from_file(const char *path, dic_node *head) { FILE *fp = fopen(path, "rb"); char buf[128]; if(fp != NULL) { while(!feof(fp)) { if(fgets(buf, sizeof(buf), fp)) { //printf("%d: %s", i++, buf); insert_node(head, buf); } } fclose(fp); } else return false; return true; }
static void *coalesce(void *ptr) { size_t prev_alloc = GET_ALLOC(HDRP(PREV_BLKP(ptr))); size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(ptr))); size_t size = GET_SIZE(HDRP(ptr)); // Do not coalesce with previous block if the previous block is tagged with Reallocation tag if (GET_TAG(HDRP(PREV_BLKP(ptr)))) prev_alloc = 1; if (prev_alloc && next_alloc) { // Case 1 return ptr; } else if (prev_alloc && !next_alloc) { // Case 2 delete_node(ptr); delete_node(NEXT_BLKP(ptr)); size += GET_SIZE(HDRP(NEXT_BLKP(ptr))); PUT(HDRP(ptr), PACK(size, 0)); PUT(FTRP(ptr), PACK(size, 0)); } else if (!prev_alloc && next_alloc) { // Case 3 delete_node(ptr); delete_node(PREV_BLKP(ptr)); size += GET_SIZE(HDRP(PREV_BLKP(ptr))); PUT(FTRP(ptr), PACK(size, 0)); PUT(HDRP(PREV_BLKP(ptr)), PACK(size, 0)); ptr = PREV_BLKP(ptr); } else { // Case 4 delete_node(ptr); delete_node(PREV_BLKP(ptr)); delete_node(NEXT_BLKP(ptr)); size += GET_SIZE(HDRP(PREV_BLKP(ptr))) + GET_SIZE(HDRP(NEXT_BLKP(ptr))); PUT(HDRP(PREV_BLKP(ptr)), PACK(size, 0)); PUT(FTRP(NEXT_BLKP(ptr)), PACK(size, 0)); ptr = PREV_BLKP(ptr); } insert_node(ptr, size); return ptr; }
int main(int argc, char* argv[]) { char type = 'a'; int nrthr = get_options(argc, argv, &type); pthread_t threads[nrthr]; extern int optind; extern char *optarg; struct listnode *first_node = NULL; struct search_args *args = malloc(sizeof(struct search_args)); args->active_threads = nrthr; args->phrase = argv[argc - 1]; args->type = &type; args->first_node_ptr = &first_node; chdir("/home"); //Add all the paths to the list of search paths. for (int i = optind; i <= argc - 2; i++) { struct listnode *tmp_node = create_node(argv[i]); insert_node(&first_node, tmp_node); } //Create threads. for (int i = 0; i < nrthr - 1; i++) { pthread_create(&threads[i], NULL, thread_function, (void *)args); } //Main thread work. thread_function((void *)args); //Join threads. for (int i = 0; i < nrthr - 1; i++) { pthread_join(threads[i], NULL); } free(args); return 0; }
/** * Replaces or inserts an entry. * If @a data is @b NULL, the value is replaced with @b TRUE (useful for sets). * @param[in] key Entry key. * @param[in] data Entry value. * @param[out] tree Tree being modified. * @return @b TRUE if @a key is new, @b FALSE otherwise. */ bool piojo_btree_set(const void *key, const void *data, piojo_btree_t *tree) { iter_t iter; PIOJO_ASSERT(tree); PIOJO_ASSERT(key); PIOJO_ASSERT(data || tree->evsize == sizeof(bool)); iter = insert_node(key, data, tree); if (iter.bnode == NULL){ PIOJO_ASSERT(tree->ecount < SIZE_MAX); ++tree->ecount; return TRUE; } if (data != NULL){ memcpy(entry_val(iter.eidx, iter.bnode, tree), data, tree->evsize); } return FALSE; }
int main (int argc, char* argv[]) { NODE* start = NULL; NODE *cur, *new_next; DATA_T new_data = 0, data = 0; cur = start; printf( "Enter integers until EOF: " ); while ( scanf( "%f", &data ) != EOF ){ start = insert_node( data, start ); printf( "Enter data: " ); } putchar( '\n' ); originalprint( start ); putchar( '\n' ); free_list( start ); return 0; }
static void *extend_heap(size_t words) { char *bp; size_t size; /* Allocate an even number of words to maintain alignment */ size = (words % 2) ? (words+1) * WSIZE : words * WSIZE; if ((long)(bp = mem_sbrk(size)) == -1){ return NULL; } /* Initialize free block header/footer and the epilogue header */ PUT(HDRP(bp), PACK(size, 0)); /* free block header */ PUT(FTRP(bp), PACK(size, 0)); /* free block footer */ PUT(HDRP(NEXT_BLK(bp)), PACK(0, 1)); /* new epilogue header */ /* Coalesce if the previous block was free */ /*insert block into appropriate list*/ insert_node(bp, size); return coalesce(bp); }
void testRBtree_delete(CuTest *tc){ RB_tree tree; tree.nil = (RB_node*)malloc(sizeof(RB_node)); tree.nil->color = BLACK; tree.nil->key = -10; tree.root = tree.nil; int i; for(i = 63; i > 0; --i){ insert_node(&tree, (short int)i); } for(i = 0; i < 63; i += 2){ delete_node(&tree, (short int)i); } //test printf("\nRed-Black Tree delete test\nInserted nodes 1-63, even numbers from 0 to 62 removed\n(for 0 no error), "); RB_display_keys_in_order(&tree); clear_tree(&tree); free(tree.nil); }
/* * place - Place block of asize bytes at start of free block bp * and split if remainder would be at least minimum block size */ static void place(void *bp, size_t asize) { size_t csize = GET_SIZE(HDRP(bp)); // size of free block size_t remainder = csize - asize; /* Remove block from free list */ delete_node(bp); if ((csize - asize) >= (2*DSIZE)) { PUT(HDRP(bp), PACK(asize, 1)); PUT(FTRP(bp), PACK(asize, 1)); bp = NEXT_BLKP(bp); PUT(HDRP(bp), PACK(remainder, 0)); PUT(FTRP(bp), PACK(remainder, 0)); insert_node(bp, remainder); } else { PUT(HDRP(bp), PACK(csize, 1)); PUT(FTRP(bp), PACK(csize, 1)); } }
int main() { int values[9] = {8, 10, 3, 6, 1, 14, 13, 4, 7}; Node *bst = NULL; printf("\n"); for (int i=0; i<9; i++) { insert_node( &bst, values[i] ); } printf("\n"); print_bst( &bst ); printf("\n\n"); draw_bst( &bst, 0 ); printf("\n"); return 0; }
//malloced here //tn will be free by author before add_to_quizzer //and data will be free by release_qoutinfo int insert_into_ttltree(struct rbtree *rbt,uchar *td,uint ttl) { struct rbnode node = {0}; struct ttlnode *tn = NULL; int len = 0; len = strlen(td) + 1; //printf("ttl is %u\n",ttl); if((tn = malloc(sizeof(struct ttlnode))) == NULL) return -1; if((tn->data = malloc(len)) == NULL) { free(tn); return -1; } tn->dlen = len; tn->exp = ttl; memcpy(tn->data,td,len); node.key = tn; insert_node(rbt,&node); return 0; }
/* * mm_free - Free a block by adding it to the appropriate list and coalescing * it. */ void mm_free(void *ptr) { size_t size = GET_SIZE(HEAD(ptr)); /* Size of block */ /* Unset the reallocation tag on the next block */ UNSET_TAG(HEAD(NEXT(ptr))); /* Adjust the allocation status in boundary tags */ PUT(HEAD(ptr), PACK(size, 0)); PUT(FOOT(ptr), PACK(size, 0)); /* Insert new block into appropriate list */ insert_node(ptr, size); /* Coalesce free block */ coalesce(ptr); /* // Check heap for consistency line_count++; if (CHECK && CHECK_FREE) { mm_check('f', ptr, size); } */ return; }
bool fill_list_array(ldata ar[], int size, list l) { int index; if (l == NULL) { print_error("list should not be uninited!"); return false; } if (!is_empty(l)) { print_error("you should fill a empty list!"); return false; } for (index = size - 1; index >= 0; index--) { insert_node(ar[index], l); } return true; }
/* Elements are in an array. The function builds binary tree */ node_t* binary_search_tree(node_t *root, int keys[], int const size) { int iterator; node_t *new_node = NULL; for(iterator = 0; iterator < size; iterator++) { new_node = (node_t *)malloc( sizeof(node_t) ); /* initialize */ new_node->data = keys[iterator]; new_node->lCount = 0; new_node->left = NULL; new_node->right = NULL; /* insert into BST */ root = insert_node(root, new_node); } return root; }
int main (int argc, char* argv[]) { NODE* start = NULL ; int num = 0 ; NODE* prnt ; printf( "Enter integers to input into the Linked List\n" ) ; while ( scanf( "%d" , &num ) != EOF ) { insert_node( num , start ) ; } prnt = start ; while ( prnt!=NULL ) { printf( "%d\n" , prnt->next ) ; prnt = prnt->next; } free_list(start); return 0; }
void pin_globals() { /* if(is_dynamic_memory_object(reg_accumulator)) */ /* insert_node(GREY, reg_accumulator); */ /* if(is_dynamic_memory_object(reg_next_expression)) */ /* insert_node(GREY, reg_next_expression); */ /* if(is_dynamic_memory_object(reg_current_value_rib)) */ /* insert_node(GREY, reg_current_value_rib); */ /* if(is_dynamic_memory_object(reg_current_env)) */ /* insert_node(GREY, reg_current_env); */ /* if(is_dynamic_memory_object(reg_current_stack)) */ /* insert_node(GREY, reg_current_stack); */ /* if(is_dynamic_memory_object(debug_env)) */ /* insert_node(GREY, debug_env); */ /* if(is_dynamic_memory_object(debug_continuation)) */ /* insert_node(GREY, debug_continuation); */ /* if(is_dynamic_memory_object(debug_execution_stack)) */ /* insert_node(GREY, debug_execution_stack); */ /* if(is_dynamic_memory_object(continuations_map)) */ /* insert_node(GREY, continuations_map); */ if(is_dynamic_memory_object(saved_continuations)) insert_node(GREY, saved_continuations); if(is_dynamic_memory_object(idclo)) insert_node(GREY, idclo); if(is_dynamic_memory_object(most_recent_closure)) insert_node(GREY, most_recent_closure); if(is_dynamic_memory_object(continuations_for_return)) insert_node(GREY, continuations_for_return); if(is_dynamic_memory_object(continuation_to_resume)) insert_node(GREY, continuation_to_resume); if(is_dynamic_memory_object(exception_handlers)) insert_node(GREY, exception_handlers); }
/* * place - Set headers and footers for newly allocated blocks. Split blocks * if enough space is remaining. */ static void place(void *ptr, size_t asize) { size_t ptr_size = GET_SIZE(HEAD(ptr)); size_t remainder = ptr_size - asize; /* Remove block from list */ delete_node(ptr); if (remainder >= MINSIZE) { /* Split block */ PUT(HEAD(ptr), PACK(asize, 1)); /* Block header */ PUT(FOOT(ptr), PACK(asize, 1)); /* Block footer */ PUT_NOTAG(HEAD(NEXT(ptr)), PACK(remainder, 0)); /* Next header */ PUT_NOTAG(FOOT(NEXT(ptr)), PACK(remainder, 0)); /* Next footer */ insert_node(NEXT(ptr), remainder); } else { /* Do not split block */ PUT(HEAD(ptr), PACK(ptr_size, 1)); /* Block header */ PUT(FOOT(ptr), PACK(ptr_size, 1)); /* Block footer */ } return; }
/* * place - Place block of asize bytes at start of free block bp * and split if remainder would be at least minimum block size */ static void place(void *bp, size_t asize){ size_t csize = GET_SIZE(HDRP(bp)); if (IS_VALID(csize - asize)) { /* we want to make sure the new free block satisfy the minimum requirement */ int t = (bp == heap_tailp); delete_node(get_level(GET_SIZE(HDRP(bp))), bp); /* remove the record in the free block list */ SET_SIZE(HDRP(bp), asize); MARK_ALLOC(HDRP(bp)); bp = NEXT_BLKP(bp); PUT(HDRP(bp), PACK3(csize-asize, 2, 0)); PUT(FTRP(bp), PACK3(csize-asize, 2, 0)); insert_node(get_level(GET_SIZE(HDRP(bp))), bp); if (t) { heap_tailp = bp; } }else { delete_node(get_level(GET_SIZE(HDRP(bp))), bp); MARK_ALLOC(HDRP(bp)); FLAG(HDRP(NEXT_BLKP(bp))); } }
int excute_parallel (command_array *cmd_arr, size_t arr_size) { int status; size_t i; node_t n; node_t pid_list; // List of pids to wait for pid_list = initialize_llist (); i = 0; // Go through all the processes while (i < arr_size) { int pid; size_t current = cmd_arr[i].ranking; while (i < arr_size && cmd_arr[i].ranking == current) { pid = fork(); // Spawn the child process to execute the command if (pid == 0) // child process { execute_command (cmd_arr[i].command_tree); exit (command_status (cmd_arr[i].command_tree)); } insert_node (pid_list, pid); i++; } // Wait for all the processes of this ranking to finish for (n = pid_list->next; n != pid_list; n = n->next) waitpid(n->val, &status, 0); // Processes finished waiting for while (pid_list != pid_list->next) remove_last_element (pid_list); } // Return the status of the last process waited on return status; }
/* * @brief Driver function */ int main(int argc, char *argv[]) { node *root = NULL; /* Start populating the BST */ root = insert_node(root, 50); insert_node(root, 30); insert_node(root, 20); insert_node(root, 40); insert_node(root, 70); insert_node(root, 60); insert_node(root, 80); /* Print inorder traversal sequence */ printf("Inorder traversal sequence for the BST -\n"); print_inorder(root); printf("\n"); return 0; }
int main (int argc, char* argv[]) { NODE* start = NULL; float data = 0 ; printf( "Enter a number: " ) ; while( scanf( "%f", &data ) != EOF ) { start = insert_node( data , start ) ; printf( "\nEnter another number or EOF( twice ): " ) ; } printf( "\n%f\n", start -> data ) ; free_list( start ); return 0; }
int add_upload_to_track(request_rec* r, const char* key) { ServerConfig *config = get_server_config(r); upload_progress_node_t* node; clean_old_connections(r); CACHE_LOCK(); node = find_node(r, key); if(node == NULL) { node = insert_node(r, key); ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "Upload Progress: Added upload with id=%s to list.", key); upload_progress_context_t *ctx = (upload_progress_context_t*)apr_pcalloc(r->pool, sizeof(upload_progress_context_t)); ctx->node = node; ctx->r = r; CACHE_UNLOCK(); apr_pool_cleanup_register(r->pool, ctx, upload_progress_cleanup, apr_pool_cleanup_null); return OK; } CACHE_UNLOCK(); return OK; }
/* Insere uma chave na árvore */ PTree * insert_tree(PTree * p, PKey chave) { unsigned int i; PNode ** no; PNode * pai; no =&(p->root); pai=NULL; for(i=0; i <= chave.size; i++) { *no = insert_node(*no); (*no)->pai=pai; pai = *(no); switch(chave.base[i]) { case 'A': case 'a': no = &((*no)->base[A]); break; case 'C': case 'c': no = &((*no)->base[C]); break; case 'G': case 'g': no = &((*no)->base[G]); break; case 'T': case 't': no = &((*no)->base[T]); break; } } return p; }
/* * coalesce - Coalesce adjacent free blocks. Sort the new free block into the * appropriate list. */ static void *coalesce(void *ptr) { size_t prev_alloc = GET_ALLOC(HEAD(PREV(ptr))); size_t next_alloc = GET_ALLOC(HEAD(NEXT(ptr))); size_t size = GET_SIZE(HEAD(ptr)); /* Return if previous and next blocks are allocated */ if (prev_alloc && next_alloc) { return ptr; } /* Do not coalesce with previous block if it is tagged */ if (GET_TAG(HEAD(PREV(ptr)))) prev_alloc = 1; /* Remove old block from list */ delete_node(ptr); /* Detect free blocks and merge, if possible */ if (prev_alloc && !next_alloc) { delete_node(NEXT(ptr)); size += GET_SIZE(HEAD(NEXT(ptr))); PUT(HEAD(ptr), PACK(size, 0)); PUT(FOOT(ptr), PACK(size, 0)); } else if (!prev_alloc && next_alloc) { delete_node(PREV(ptr)); size += GET_SIZE(HEAD(PREV(ptr))); PUT(FOOT(ptr), PACK(size, 0)); PUT(HEAD(PREV(ptr)), PACK(size, 0)); ptr = PREV(ptr); } else { delete_node(PREV(ptr)); delete_node(NEXT(ptr)); size += GET_SIZE(HEAD(PREV(ptr))) + GET_SIZE(HEAD(NEXT(ptr))); PUT(HEAD(PREV(ptr)), PACK(size, 0)); PUT(FOOT(NEXT(ptr)), PACK(size, 0)); ptr = PREV(ptr); } /* Adjust segregated linked lists */ insert_node(ptr, size); return ptr; }
PB_DS_CLASS_T_DEC void PB_DS_CLASS_C_DEC:: modify(point_iterator it, const_reference r_new_val) { PB_DS_ASSERT_VALID_COND((*this),true) node_pointer p_nd = it.m_p_nd; _GLIBCXX_DEBUG_ASSERT(p_nd != 0); PB_DS_ASSERT_BASE_NODE_CONSISTENT(p_nd, false) const bool bubble_up = Cmp_Fn::operator()(p_nd->m_value, r_new_val); p_nd->m_value = r_new_val; if (bubble_up) { node_pointer p_parent = base_type::parent(p_nd); while (p_parent != 0 && Cmp_Fn::operator()(p_parent->m_value, p_nd->m_value)) { base_type::swap_with_parent(p_nd, p_parent); p_parent = base_type::parent(p_nd); } if (p_nd->m_p_prev_or_parent == 0) base_type::m_p_root = p_nd; m_p_max = 0; PB_DS_ASSERT_VALID_COND((*this),true) return; } base_type::bubble_to_top(p_nd); remove_parentless_node(p_nd); insert_node(p_nd); m_p_max = 0; PB_DS_ASSERT_VALID_COND((*this),true) }
/** * Remove root of the heap. * * @param heap heap to modify * @return element data stored at the root node, NULL if heap is empty */ void * GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap) { void *ret; struct GNUNET_CONTAINER_HeapNode *root; if (NULL == (root = heap->root)) return NULL; heap->size--; ret = root->element; if (root->left_child == NULL) { heap->root = root->right_child; if (root->right_child != NULL) root->right_child->parent = NULL; } else if (root->right_child == NULL) { heap->root = root->left_child; root->left_child->parent = NULL; } else { root->left_child->parent = NULL; root->right_child->parent = NULL; heap->root = root->left_child; insert_node (heap, heap->root, root->right_child); } if (heap->walk_pos == root) heap->walk_pos = heap->root; GNUNET_free (root); #if EXTRA_CHECKS GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) || (heap->size == heap->root->tree_size + 1)); CHECK (heap->root); #endif return ret; }
static void process_file_change (FMTreeModelRoot *root, NemoFile *file) { TreeNode *node, *parent; node = get_node_from_file (root, file); if (node != NULL) { update_node (root->model, node); return; } if (!should_show_file (root->model, file)) { return; } parent = get_parent_node_from_file (root, file); if (parent == NULL) { return; } insert_node (root->model, parent, create_node_for_file (root, file)); }
static void reparent_node (FMTreeModel *model, TreeNode *node) { GtkTreePath *path; TreeNode *new_parent; new_parent = get_parent_node_from_file (node->root, node->file); if (new_parent == NULL || new_parent->directory == NULL) { destroy_node (model, node); return; } path = get_node_path (model, node); /* Report row_deleted before actually deleting */ gtk_tree_model_row_deleted (GTK_TREE_MODEL (model), path); gtk_tree_path_free (path); abandon_node_ref_count (model, node); tree_node_unparent (model, node); insert_node (model, new_parent, node); }