static int __init init_pfair(void) { int cpu, i; struct pfair_state *state; /* * initialize short_cut for per-cpu pfair state; * there may be a problem here if someone removes a cpu * while we are doing this initialization... and if cpus * are added / removed later... is it a _real_ problem? */ pstate = kmalloc(sizeof(struct pfair_state*) * num_online_cpus(), GFP_KERNEL); /* initialize release queue */ for (i = 0; i < PFAIR_MAX_PERIOD; i++) bheap_init(&release_queue[i]); /* initialize CPU state */ for (cpu = 0; cpu < num_online_cpus(); cpu++) { state = &per_cpu(pfair_state, cpu); state->cpu = cpu; state->cur_tick = 0; state->local_tick = 0; state->linked = NULL; state->local = NULL; state->scheduled = NULL; state->missed_quanta = 0; state->offset = cpu_stagger_offset(cpu); pstate[cpu] = state; } rt_domain_init(&pfair, pfair_ready_order, NULL, NULL); return register_sched_plugin(&pfair_plugin); }
int main(void) { plan_tests(1); struct binary_heap bh; bheap_init(&bh, X_ord); struct X foo = { .v = 2 }; bheap_push(&bh, &foo.bhn); struct binary_heap_node *foo_b = bheap_pop(&bh); ok1(foo_b == &foo.bhn); return exit_status(); }
int main(void) { bheap_t bh; bheap_init(&bh,10,cmp); long long *arr = malloc(sizeof(long long)*1024*1024*4); long long i; for(i=0;i<1024*1024*4;i++) { arr[i]=1024*1024*4-i; bheap_insert(&bh,&arr[i]); } while(bheap_get_size(&bh) > 0) { long long *head = bheap_remove(&bh); printf("poped: %lld\n",*head); } free(arr); bheap_destroy(&bh); return 0; }
th_path *ai_shortes_path(int player, int unit, th_point source, th_point goal) { int i, a; int count; th_vector vector; th_point pt; th_point *solution; th_path *path; bheap *open; struct hashtable *closed; bheap_node *e; bheap_node *p; bheap_node *n; // Are the source and goal point valid? if( source.x >= 0 && source.x <= x_tildes && source.y >= 0 && source.y <= y_tildes && goal.x >= 0 && goal.x <= x_tildes && goal.y >= 0 && goal.y <= y_tildes ) { // TODO: Actual way to store binary heap nodes is a bad idea. We need make // te code to use the dinamic strucures in bheap_add. e = (bheap_node *)malloc(1000 * x_tildes * y_tildes * sizeof(bheap_node)); if(e == NULL) return NULL; count = 0; i = 0; // Creating open and closed lists open = bheap_init(x_tildes * y_tildes); closed = make_hashtable(hashtable_default_hash, x_tildes * y_tildes); // Defining the initial node sprintf(e[count].id, "%03d%03d", source.x, source.y); //printf("====================== A* STARTING... =====================\n"); //printf("Element id to store: %s\n", e[count].id); e[count].deph = 0; e[count].point = source; e[count].h = HDIST(e[count].point.x, e[count].point.y, goal.x, goal.y); e[count].g = 0; e[count].val = e[count].g + e[count].h; e[count].index = count; e[count].parent = NULL; // Insert the initial node to the open list if(!bheap_add(open, &e[count])) { printf("Coudn't add element to the open list!\n"); return NULL; } //bheap_print(open); while(open->count >= 0) { //printf("********** New Loop Cycle\n"); // Remove the lowest element in open list // and add it to the closed list n = bheap_del(open); if(n == NULL) { printf("Error deleting the priority element from open list!\n"); return NULL; } //printf("Removed id: %s\n", n->id); //bheap_print(open); //printf("Element id to store in loop: %s, index: %d\n", n->id, n->index); if(!hashtable_add(closed, e[n->index].id, &e[n->index])) { printf("Error adding to hashtable!\n"); return NULL; } //hashtable_iter(closed, hashtable_default_hash); //Is this element the goal? if(n->point.x == goal.x && n->point.y == goal.y) { printf("Solution deph is %d\n", n->deph); solution = (th_point *)malloc(n->deph * sizeof(th_point)); if(!solution) return NULL; path = (th_path *)malloc(sizeof(th_path)); if(!path) return NULL; i=0; while(n->parent) { printf("(%d,%d)\n",n->point.x, n->point.y); solution[i] = n->point; n = n->parent; i++; } path->path = solution; path->size = i - 1; free_hashtable(closed); bheap_free(open); FREE(e); return path; } //printf("This element is not the goal!.. Trying...\n"); //For each valid move for n for(a = 0; a < NUM_DIRS; a++) { vector = get_vector(n->point, a); if(vector.x != -2 && vector.y != -2) { //printf("Vector is valid... \n"); //printf("For %d direction tile in (%d,%d) is valid?\n", a, n->point.x, n->point.y); pt.x = vector.x + n->point.x; pt.y = vector.y + n->point.y; if(ai_valid_tile(player, unit, pt)) { //printf("Adding direction %d to open list!\n", a); //New valid element count++; e[count].deph = n->deph + 1; e[count].point = pt; memset(e[count].id, 0, 7); sprintf(e[count].id, "%03d%03d", pt.x, pt.y); e[count].index = count; e[count].h = HDIST(e[count].point.x, e[count].point.y, goal.x, goal.y); if( a == ISO_N || a == ISO_S || a == ISO_W || a == ISO_E) e[count].g = n->g + 10; else e[count].g = n->g + 14; e[count].val = e[count].g + e[count].h; // F = G + H e[count].parent = n; //printf("Actual id: %s, H: %d G:%d F:%d Deph:%d\n", e[count].id, e[count].h, // e[count].g, e[count].val, e[count].deph); //Is this element in closed list? if((p = hashtable_lookup(closed, e[count].id)) != NULL) { //printf("P exists in cloded list!\n"); if(p->val > e[count].val) { if(!hashtable_remove(closed, p->id)) { printf("Error ocurred while trying to remove key in hashtable!\n"); hashtable_iter(closed, hashtable_default_hash); return NULL; } //else //{ //printf("Removes OK, let's check integrity!\n"); //hashtable_iter(closed, hashtable_default_hash); //} if(!bheap_add(open, p)) { printf("Error ocurred while adding a element to open list\n"); return NULL; } //printf("Succesfully removed from closed list and added to open list\n"); } } else { //printf("P doesn't exist in closed list!\n"); if(!bheap_add(open, &e[count])) { printf("Error ocurred while adding a new element to open list\n"); return NULL; } } //bheap_print(open); } } } } free_hashtable(closed); bheap_free(open); FREE(e); } else { printf("Bad point references : Origin(%d, %d) Dest(%d, %d)\n", source.x, source.y, goal.x, goal.y); } }