void freeRST(RST* root){ if(root->indegree == 0){ freePath(root->path); free(root); return; } for(int i=0; i<root->indegree; i++){ freeRST(root->child[i]); } freePath(root->path); if(root == NULL){ return; } free(root); }
void freeDTree(dTree* root){ if(root == NULL){ return; } freeDTree(root->xTraversal); freeDTree(root->yTraversal); freePath(root->path); free(root); }
/* * frees all paths, and then frees the level path structure itself */ void freeLevelPaths() { LevelPaths lP = getLevelPaths(NULL); for(int i = 0; i < lP->numberOfPaths; i++) { freePath(lP->paths[i]); } free(lP->paths); free(lP); }
/* * Traverse CFG in reverse topological order (already given in bblist) * to collect cost, eliminating infeasible paths. */ int traverse( int pid, block **bblist, int num_bb, int *in_degree, uint *cost ) { DSTART( "traverse" ); int i, j, k, id, pt; char direction, extend; path *pu, *pv; block *bu, *bv; branch *bru; for( i = 0; i < num_bb; i++ ) { bu = bblist[i]; bru = branchlist[pid][bu->bbid]; // printf( "Node %d cost %d: \n", bu->bbid, cost[bu->bbid] ); fflush( stdout ); // printBlock( bu ); if( !bu->num_outgoing ) { // bu is a sink // cost(bu) = { sum(bu) | sum(bu) is the sum of costs of each instruction in bu }; MALLOC( pu, path*, sizeof(path), "path" ); pu->cost = cost[bu->bbid]; pu->bb_len = 1; MALLOC( pu->bb_seq, int*, sizeof(int), "path bb_seq" ); pu->bb_seq[0] = bu->bbid; pu->branch_len = 0; pu->branch_eff = NULL; pu->branch_dir = NULL; num_paths[bu->bbid]++; REALLOC( pathlist[bu->bbid], path**, num_paths[bu->bbid] * sizeof(path*), "pathlist elm" ); pathlist[bu->bbid][ num_paths[bu->bbid]-1 ] = pu; continue; } // Step 1: Compute the WCET paths of each branch for( j = 0; j < bu->num_outgoing; j++ ) { id = getblock( bu->outgoing[j], bblist, 0, i-1 ); if( id == -1 ) prerr( "Block %d-%d not found.\n", pid, bu->outgoing[j] ); bv = bblist[id]; // printf( "out: " ); printBlock( bv ); for( pt = 0; pt < num_paths[bv->bbid]; pt++ ) { pv = pathlist[bv->bbid][pt]; // branches with potential conflict if( bru != NULL ) { direction = detectDirection( bru, bv ); // printf( "%d:%d->%d: dirn = %d\n", pid, bu->bbid, bv->bbid, direction ); /* * temporary disable conflict detection * // test BB conflicts if( BBconflictInPath( bru, direction, bv, pv, bblist, num_bb )) continue; */ } /* * temporary disable conflict detection * // test BA conflicts if( BAconflictInPath( bu, bv, pv, bblist, num_bb )) continue; */ // else, include this path for bu MALLOC( pu, path*, sizeof(path), "path" ); pu->bb_len = pv->bb_len + 1; pu->cost = pv->cost + cost[bu->bbid]; // extra cost if bu-->bv is a region transition //int rid; //if( regionmode ) { // if( bu->callpid != -1 ) // rid = procs[bu->callpid]->bblist[ procs[bu->callpid]->num_bb - 1 ]->regid; // if( bu->callpid == -1 || rid == -1 ) { // if( bu->regid != -1 && bv->regid != -1 && bu->regid != bv->regid ) { // printf( "region transition %d-%d(%d) --> %d-%d(%d) cost: %u\n", // bu->pid, bu->bbid, bu->regid, bv->pid, bv->bbid, bv->regid, regioncost[bv->regid] ); // fflush( stdout ); // pu->cost += regioncost[bv->regid]; // } // } // // region transition due to procedure call at end of bu // else { // if( rid != -1 && bv->regid != -1 && rid != bv->regid ) { // printf( "region transition %d-%d(%d) procedure return %d(%d) --> %d-%d(%d) cost: %u\n", // bu->pid, bu->bbid, bu->regid, bu->callpid, rid, // bv->pid, bv->bbid, bv->regid, regioncost[bv->regid] ); fflush( stdout ); // pu->cost += regioncost[bv->regid]; // } // } //} // end if( regionmode ) extend = 0; if( bru != NULL && hasIncomingConflict( bru, direction, bblist, i+1, num_bb )) extend = 1; pu->branch_len = pv->branch_len; if( extend ) pu->branch_len++; MALLOC( pu->bb_seq, int*, pu->bb_len * sizeof(int), "path bb_seq" ); MALLOC( pu->branch_eff, branch**, pu->branch_len * sizeof(branch*), "path branch_eff" ); MALLOC( pu->branch_dir, char*, pu->branch_len * sizeof(char), "path branch_dir" ); copySeq( pu, pv ); pu->bb_seq[ pu->bb_len - 1 ] = bu->bbid; if( extend ) sortedInsertBranch( pu, bru, direction ); num_paths[bu->bbid]++; REALLOC( pathlist[bu->bbid], path**, num_paths[bu->bbid] * sizeof(path*), "pathlist elm" ); pathlist[bu->bbid][ num_paths[bu->bbid]-1 ] = pu; } // end for paths of bv } // end for bu's children if( num_paths[bu->bbid] <= 0 ) prerr( "\nNo feasible path at %d-%d!\n\n", pid, bu->bbid ); // Step 2: Consolidate // if( edges e1, ..., en in subgraph(bu) are not conflicting with some predecessors ) // combine the two paths in cost(bu) if they only differ in term ei // Note that for each node bu, we keep a list of nodes conflicting with bu and can reach bu. // Step 2.1 Update incoming conflicts list: clear bu since it is already visited for( j = 0; j < procs[pid]->num_bb; j++ ) { bru = branchlist[pid][j]; if( bru != NULL && bru->in_conflict[bu->bbid] ) bru->num_active_incfs--; } // Step 2.2 Merge paths for( pt = 0; pt < num_paths[bu->bbid]; pt++ ) { pu = pathlist[bu->bbid][pt]; // check each branch in this path for expired conflicts k = 0; while( k < pu->branch_len ) { // remove if no more incoming conflict, or cancelled by assignment in bu if( !pu->branch_eff[k]->num_active_incfs || assignsTo( bu, pu->branch_eff[k]->deri_tree )) removeBranch( pu, k ); else k++; } } // end for paths // printf( "Consolidation: Decision cancelled over\n" ); fflush( stdout ); if( num_paths[bu->bbid] > 1 ) { // sort by increasing cost, then decreasing number of branches sortPath( pathlist[bu->bbid], num_paths[bu->bbid] ); for( pt = 0; pt < num_paths[bu->bbid] - 1; pt++ ) { pu = pathlist[bu->bbid][pt]; for( k = pt + 1; k < num_paths[bu->bbid]; k++ ) { pv = pathlist[bu->bbid][k]; // remove pu if its conflict list is a superset of pv's // (i.e. pu has less cost yet more conflicts than pv, thus cannot be wcet path) if( subsetConflict( pv, pu )) { pu->bb_len = -1; break; } } } // remove the marked paths (id: the index to overwrite) id = -1; for( pt = 0; pt < num_paths[bu->bbid]; pt++ ) { pu = pathlist[bu->bbid][pt]; if( pu->bb_len == -1 ) { freePath( bu->bbid, pt ); if( id == -1 ) id = pt; } else { if( id > -1 ) pathlist[bu->bbid][id++] = pu; } } if( id > -1 ) num_paths[bu->bbid] = id; } // stats if( num_paths[bu->bbid] > max_paths ) max_paths = num_paths[bu->bbid]; // free paths in nodes which are dead (i.e. already processed) for( j = 0; j < bu->num_outgoing; j++ ) in_degree[ bu->outgoing[j] ]--; // note that the node in top topo order is excluded for( j = 0; j < num_bb - 1; j++ ) { id = bblist[j]->bbid; if( in_degree[id] == 0 && pathFreed[id] == 0 ) { freePathsInNode( id ); pathFreed[id] = 1; } } DOUT( "Paths at %d-%d: %d\n", pid, bu->bbid, num_paths[bu->bbid] ); DACTION( for( pt = 0; pt < num_paths[bu->bbid]; pt++ ) printPath( pathlist[bu->bbid][pt] ); );
/* * ResolveExternalCmd * * arguments: * commandT *cmd: the command to be run * * returns: bool: whether the given command exists * * Determines whether the command to be run actually exists. */ static bool ResolveExternalCmd(commandT* cmd, char* path) { //Check to see if in home directory: if(*(cmd->argv[0])=='.'){ char* cwd = getCurrentWorkingDir(); //char* cwd; //getCurrentWorkingDir(cwd); sprintf(path,"%s/%s",cwd,cmd->name); free(cwd); return TRUE; } char** memLocs = getPath(); char dest[500]; int i=0; struct stat buf; /*If already absolute path*/ if(stat(cmd->name,&buf)==0){ /*Set path = to entered absolute path*/ strcpy(path,cmd->name); freePath(memLocs); return TRUE; } while(memLocs[i]!=NULL){ //Concatanate Paths with cmd->name: int size = (snprintf( dest, 499,"%s/%s",memLocs[i],cmd->name)+1)*sizeof(char); char* exeName = (char*)malloc(size); sprintf(exeName,"%s/%s",memLocs[i],cmd->name); //Check to see if exists and executable: if(stat(exeName,&buf)==0){ if(S_ISREG(buf.st_mode) && buf.st_mode & 0111){ strncpy(path,exeName,size); freePath(memLocs); free(exeName); return TRUE; } freePath(memLocs); free(exeName); return FALSE; } i++; free(exeName); } freePath(memLocs); return FALSE; } /* ResolveExternalCmd */
int main(int argc, char* argv[]) { Map map; Car cars[3] = {NULL, NULL, NULL}; int x, y, i, rounds = 0, needPathfinding = 0, firstLoop = 1; List path; Position position; Vector acc; int fuel; srand(time(NULL)); freopen("pshr4log.txt", "w+", stderr); // freopen("map.txt", "r", stdin); LOGINFO("Starting"); initGC(); map = Map_load(stdin); LOGINFO("Map loaded"); while(!feof(stdin)) { LOGINFO1I("===== Round number %d =====", rounds); for(i = 0; i < 3; i++) { fscanf(stdin, "%d %d", &x, &y); LOGINFO3I("Car number %i, position : %d %d", i, x, y); if(cars[i] == NULL) cars[i] = Car_new(x, y); else Car_updatePosition(cars[i], x, y); if(Car_updateArrivedStatus(cars[i], map)) { recomputeDistances(map, cars); LOGINFO("I see you've arrived, let me see where I can park now..."); needPathfinding = 1; } } if(firstLoop) { firstLoop = 0; path = doPathfinding(map, cars); List_tail(path); List_prev(path); } position = List_getCurrent(path); for(i = 1; i < 3; i++) { if(Position_equal(position, Car_getPosition(cars[i]))) { needPathfinding = 1; LOGINFO("YOU TOOK MY SPOT JERK"); } } if(needPathfinding) { freePath(path); path = doPathfinding(map, cars); List_tail(path); List_prev(path); needPathfinding = 0; position = List_getCurrent(path); } if(position == NULL) { goRandom(); } else { LOGINFO2I("Going to %d %d", position->x, position->y); acc = Car_getAccelerationToReach(cars[0], position); go(acc->x, acc->y); if(Vector_squaredLength(acc) > 2) { Car_useBoost(cars[0]); LOGINFO("Using a boost"); } Vector_delete(acc); } List_prev(path); rounds++; } LOGINFO("End"); freePath(path); for(i = 0; i < 3; i++) { Car_delete(cars[i]); } Map_delete(map); freeGC(); return 0; }
void PathPlanner::freeResources() { freeSearchSpace(); freePath(); p = root = test = NULL; }
int main(int argc, char** argv) { global.debug = 0; global.verbose = 0; uint i,j; const long seed = time(0); srand(seed); if (global.verbose) printf("seed = %li\n",seed); FILE* output; uint n,v,nb; long timeS; if (argc < 7) { printf("Argument error !\n"); printf("syntax : %s filename n V\nn = number of items, V = bag volume\n",argv[0]); return 0; } uint nMin,nMax,vMin,vMax; graph* g = 0; path* p = 0; nb = atoi(argv[2]); nMin = atoi(argv[3]); nMax = atoi(argv[4]); vMin = atoi(argv[5]); vMax = atoi(argv[6]); if (!(output = fopen(argv[1],"a"))) { perror("File error."); exit(EXIT_FAILURE); } for (i = 0 ; i < nb ; i++) { n = rand() % (nMax - nMin) + nMin; g = generateGraph(n,vMin,vMax); timeS = time(0); p = tsp(g); timeS = time(0)-timeS; printf("[TEST] %i : n = %i (%li seconds)\n",i,n,timeS); fprintf(output,"%u %li\n",n,timeS,v); freeGraph(g); freePath(p); g = 0; p = 0; } fclose(output); /* // ex prog dyn g = createGraph(5); for (i = 0 ; i < 5 ; i++) setEdge(g,i,i,50); setEdge(g,0,1,1); setEdge(g,0,2,2); setEdge(g,0,3,1); setEdge(g,0,4,0); setEdge(g,1,2,3); setEdge(g,1,3,5); setEdge(g,1,4,0); setEdge(g,2,3,2); setEdge(g,2,4,1); setEdge(g,3,4,4); printGraph(g); p = tsp(g); printPath(p); freePath(p); freeGraph(g); */ return EXIT_SUCCESS; }
int main(void) { signal(SIGINT, intHandler); char *command = NULL, *filePath = NULL; char **args = NULL; node *root = NULL; fd_set rfds; struct timeval tv; int retval; while(run) { tv.tv_sec = 1; tv.tv_usec = 0; FD_ZERO(&rfds); FD_SET(0, &rfds); int retval = select(1, &rfds, NULL, NULL, &tv); if(retval == -1) { printf("\nShutting Down\n\n"); return 0; }else if(retval == 0) { continue; } args = getInput(); if(args == NULL) { fflush(stdin); continue; } root = collapseFilePath(args[0]); if(root == NULL) { freeArgs(args); continue; } filePath = getValidFilePath(root); if(filePath == NULL) { freePath(root); freeArgs(args); continue; } char *data = parseData(args[1]); if(data == NULL) { freePath(root); freeArgs(args); free(filePath); continue; } command = (char *) malloc(strlen(filePath) + strlen(data) + 18); if(command == NULL) { printf("ERROR: Not enough memory to malloc the echo command\n"); }else { strncpy(command, "echo ", 6); strncat(command, "\"", 3); strncat(command, data, strlen(data)+1); strncat(command, "\"", 3); strncat(command, " >> ", 5); strncat(command, "\"", 3); strncat(command, filePath, strlen(filePath)+1); strncat(command, "\"", 3); //printf("Command:\n"); //printf("%s\n", command); int result = system(command); if(result == -1) { printf("ERROR: Could not append to file\n"); } printf("SUCCESS\n"); } free(data); free(command); free(filePath); freePath(root); freeArgs(args); } if(command != NULL) free(command); if(filePath != NULL) free(filePath); if(root != NULL) freePath(root); if(args != NULL) freeArgs(args); return 0; }