void destroy_eval__safe() { prim_container *c = all_prims; while (c != NULL) { prim_container *t; free_malloced(c->prim); t = c; c = c->next; free_malloced(t); } /* Frees containers and non heap values (?) held in environments. */ destroy_env(&special_forms); destroy_env(&toplevel); destroy_env(&primitives); destroy_env(&lib); destroy_env(&internal); //orig_sexpr == NULL; //*live_root; /* This will free resources held by symbol representation. */ free_symtab(global_symtab); global_symtab = NULL; __gs.head = NULL; }
static void write_resolution (void) { unsigned int i; FILE *f; check (resolution_file, LDPL_FATAL, "resolution file not specified"); f = fopen (resolution_file, "w"); check (f, LDPL_FATAL, "could not open file"); fprintf (f, "%d\n", num_claimed_files); for (i = 0; i < num_claimed_files; i++) { struct plugin_file_info *info = &claimed_files[i]; struct plugin_symtab *symtab = &info->symtab; struct ld_plugin_symbol *syms = symtab->syms; get_symbols (info->handle, symtab->nsyms, syms); finish_conflict_resolution (symtab, &info->conflicts); fprintf (f, "%s %d\n", info->name, symtab->nsyms + info->conflicts.nsyms); dump_symtab (f, symtab); if (info->conflicts.nsyms) { dump_symtab (f, &info->conflicts); free_symtab (&info->conflicts); } } fclose (f); }
static gmx_bool pdb_next_x(t_trxstatus *status, FILE *fp, t_trxframe *fr) { t_atoms atoms; t_symtab *symtab; matrix boxpdb; // Initiate model_nr to -1 rather than NOTSET. // It is not worthwhile introducing extra variables in the // read_pdbfile call to verify that a model_nr was read. int ePBC, model_nr = -1, na; char title[STRLEN], *time, *step; double dbl; atoms.nr = fr->natoms; atoms.atom = nullptr; atoms.pdbinfo = nullptr; /* the other pointers in atoms should not be accessed if these are NULL */ snew(symtab, 1); open_symtab(symtab); na = read_pdbfile(fp, title, &model_nr, &atoms, symtab, fr->x, &ePBC, boxpdb, TRUE, nullptr); free_symtab(symtab); sfree(symtab); set_trxframe_ePBC(fr, ePBC); if (nframes_read(status) == 0) { fprintf(stderr, " '%s', %d atoms\n", title, fr->natoms); } fr->bPrec = TRUE; fr->prec = 10000; fr->bX = TRUE; fr->bBox = (boxpdb[XX][XX] != 0.0); if (fr->bBox) { copy_mat(boxpdb, fr->box); } fr->step = 0; step = std::strstr(title, " step= "); fr->bStep = ((step != nullptr) && sscanf(step+7, "%" SCNd64, &fr->step) == 1); dbl = 0.0; time = std::strstr(title, " t= "); fr->bTime = ((time != nullptr) && sscanf(time+4, "%lf", &dbl) == 1); fr->time = dbl; if (na == 0) { return FALSE; } else { if (na != fr->natoms) { gmx_fatal(FARGS, "Number of atoms in pdb frame %d is %d instead of %d", nframes_read(status), na, fr->natoms); } return TRUE; } }
void clear_profiler() { if (!newprofile) { newprofile = true; profile = false; traversed = false; free_tree(calltree); calltree = NULL; free_symtab(); symtab = NULL; } }
void Estimator_Destroy(Estimator_type * est) { prng_Destroy(est->prng); for(int i=0; i < est->c; i++) { Sample_Destroy(est->samplers[i]); } free(est->samplers); Freq_Destroy(est->freq); free_bheap(est->bheap); free_heap(est->prim_heap); free_symtab(est->hashtable); free(est); }
static void read_whole_conf(const char *infile, char *title, t_atoms *atoms, rvec x[], rvec *v, matrix box) { FILE *in; int ndec; t_symtab symtab; /* open file */ in = gmx_fio_fopen(infile, "r"); open_symtab(&symtab); get_w_conf(in, infile, title, &symtab, atoms, &ndec, x, v, box); /* We can't free the symbols, as they are still used in atoms, so * the only choice is to leak them. */ free_symtab(&symtab); gmx_fio_fclose(in); }
static void write_resolution (void) { unsigned int i; FILE *f; check (resolution_file, LDPL_FATAL, "resolution file not specified"); f = fopen (resolution_file, "w"); check (f, LDPL_FATAL, "could not open file"); fprintf (f, "%d\n", num_claimed_files); for (i = 0; i < num_claimed_files; i++) { struct plugin_file_info *info = &claimed_files[i]; struct plugin_symtab *symtab = &info->symtab; struct ld_plugin_symbol *syms = symtab->syms; /* Version 2 of API supports IRONLY_EXP resolution that is accepted by GCC-4.7 and newer. */ if (get_symbols_v2) get_symbols_v2 (info->handle, symtab->nsyms, syms); else get_symbols (info->handle, symtab->nsyms, syms); finish_conflict_resolution (symtab, &info->conflicts); fprintf (f, "%s %d\n", info->name, symtab->nsyms + info->conflicts.nsyms); dump_symtab (f, symtab); if (info->conflicts.nsyms) { dump_symtab (f, &info->conflicts); free_symtab (&info->conflicts); } } fclose (f); }
static gmx_bool pdb_next_x(t_trxstatus *status, FILE *fp, t_trxframe *fr) { t_atoms atoms; t_symtab *symtab; matrix boxpdb; // Initiate model_nr to -1 rather than NOTSET. // It is not worthwhile introducing extra variables in the // read_pdbfile call to verify that a model_nr was read. int ePBC, model_nr = -1, na; char title[STRLEN], *time; double dbl; atoms.nr = fr->natoms; atoms.atom = NULL; atoms.pdbinfo = NULL; /* the other pointers in atoms should not be accessed if these are NULL */ snew(symtab, 1); open_symtab(symtab); na = read_pdbfile(fp, title, &model_nr, &atoms, symtab, fr->x, &ePBC, boxpdb, TRUE, NULL); free_symtab(symtab); sfree(symtab); set_trxframe_ePBC(fr, ePBC); if (nframes_read(status) == 0) { fprintf(stderr, " '%s', %d atoms\n", title, fr->natoms); } fr->bPrec = TRUE; fr->prec = 10000; fr->bX = TRUE; fr->bBox = (boxpdb[XX][XX] != 0.0); if (fr->bBox) { copy_mat(boxpdb, fr->box); } if (model_nr != -1) { fr->bStep = TRUE; fr->step = model_nr; } time = std::strstr(title, " t= "); if (time) { fr->bTime = TRUE; sscanf(time+4, "%lf", &dbl); fr->time = (real)dbl; } else { fr->bTime = FALSE; /* this is a bit dirty, but it will work: if no time is read from comment line in pdb file, set time to current frame number */ if (fr->bStep) { fr->time = (real)fr->step; } else { fr->time = (real)nframes_read(status); } } if (na == 0) { return FALSE; } else { if (na != fr->natoms) { gmx_fatal(FARGS, "Number of atoms in pdb frame %d is %d instead of %d", nframes_read(status), na, fr->natoms); } return TRUE; } }
void decode(char *read_line,ssize_t read_len,char **label,char **pneumonic,char **operand,char *int_filename,FILE *inp_file,FILE *int_file,FILE *out_file,char ***optab,int num_ops,sym_tab *symtab,long int LOCCTR,char *read_raw_line) { (*label)=NULL; (*pneumonic)=NULL; (*operand)=NULL; char *work_string; if( (work_string=malloc(read_len+1)) == NULL ) { printf("Out of memory\n"); if(inp_file) fclose(inp_file); if(int_file) fclose(int_file); if(out_file) fclose(out_file); if(int_filename) free(int_filename); if(read_raw_line) free(read_raw_line); else free(read_line); free_optab(optab,num_ops); free_symtab(symtab); exit(12); } sscanf(read_line,"%s",work_string); if( work_string[strlen(work_string)-1] == ':' ) (*label)=work_string; else free(work_string); work_string=NULL; if( (work_string=malloc(read_len+1)) == NULL ) { printf("Out of memory\n"); if(inp_file) fclose(inp_file); if(int_file) fclose(int_file); if(out_file) fclose(out_file); if(int_filename) free(int_filename); if(read_raw_line) free(read_raw_line); else free(read_line); if((*pneumonic)) free((*pneumonic)); else free((*label)); free_optab(optab,num_ops); free_symtab(symtab); exit(12); } if((*label)) { strcpy(work_string,""); sscanf(strstr(read_line,(*label))+strlen((*label)),"%s",work_string); if((*work_string)=='\0') { printf("Bad Assembly coding...\n"); printf("No peumonic corresponding to label at %lX\n",LOCCTR); if(inp_file) fclose(inp_file); if(int_file) fclose(int_file); if(out_file) fclose(out_file); if(int_filename) free(int_filename); if(read_raw_line) free(read_raw_line); else free(read_line); free((*label)); free(work_string); free_optab(optab,num_ops); free_symtab(symtab); exit(12); } (*pneumonic)=work_string; work_string=NULL; if( (work_string=malloc(read_len+1)) == NULL ) { printf("Out of memory\n"); if(inp_file) fclose(inp_file); if(int_file) fclose(int_file); if(out_file) fclose(out_file); if(int_filename) free(int_filename); if(read_raw_line) free(read_raw_line); else free(read_line); free((*label)); free((*pneumonic)); free_optab(optab,num_ops); free_symtab(symtab); exit(14); } strcpy(work_string,""); sscanf(strstr(read_line,(*pneumonic))+strlen((*pneumonic)),"%s",work_string); if((*work_string)!='\0') (*operand)=work_string; else free(work_string); } else { sscanf(read_line,"%s",work_string); (*pneumonic)=work_string; work_string=NULL; if( (work_string=malloc(read_len+1)) == NULL ) { printf("Out of memory\n"); if(inp_file) fclose(inp_file); if(int_file) fclose(int_file); if(out_file) fclose(out_file); if(int_filename) free(int_filename); if(read_raw_line) free(read_raw_line); else free(read_line); free((*pneumonic)); free_optab(optab,num_ops); free_symtab(symtab); exit(14); } strcpy(work_string,""); sscanf(strstr(read_line,(*pneumonic))+strlen((*pneumonic)),"%s",work_string); if((*work_string)=='\0') free(work_string); else { (*operand)=work_string; work_string=NULL; } } }
int main(int argc,char *argv[]) { if(argc!=2) { printf("Usage: %s <input file>\n",argv[0]); exit(1); } /* Load Optab */ char ***optab=NULL; int num_ops; if( (optab=load_optab("opfile",&num_ops)) == NULL) { printf("Optab generation failed\n"); exit(2); } /* Now let us load and initialize the symtab */ sym_tab *symtab=NULL; if( (symtab=init_symtab())==NULL ) { printf("Failed to initiate symtab\n"); free_optab(optab,num_ops); exit(3); } FILE *inp_file=NULL; if( (inp_file=fopen(argv[1],"r")) == NULL ) { printf("Failed to open input file\n"); free_optab(optab,num_ops); free_symtab(symtab); exit(4); } FILE *out_file=NULL; char *int_filename=NULL; if( (int_filename=(char *)malloc(strlen(argv[1])+5)) == NULL ) { printf("Out of memory\n"); free_optab(optab,num_ops); free_symtab(symtab); exit(5); } strcpy(int_filename,argv[1]); strcat(int_filename,"_INT"); FILE *int_file=NULL; if( (int_file=fopen(int_filename,"w+")) == NULL ) { printf("Failed to open intermediate file\n"); free_optab(optab,num_ops); free_symtab(symtab); free(int_filename); fclose(inp_file); exit(6); } /* And Here We Go */ long int LOCCTR=0; long int start_addr; char *read_line=NULL; size_t to_read = 0; ssize_t read_len; char *work_string=NULL; char *work_string2=NULL; char *prog_name=NULL; char *search_space=NULL; int start_loop=0; while( (read_len=getline(&read_line,&to_read,inp_file)) != -1 ) { if( (search_space=(char *)malloc(read_len+1)) == NULL ) { printf("Out of memory\n"); fclose(inp_file); fclose(int_file); free(int_filename); free(read_line); free_optab(optab,num_ops); free_symtab(symtab); exit(10); } strcpy(search_space,""); sscanf(read_line,"%s",search_space); if(((*search_space)=='\0')||((*search_space)==';')) { free(search_space); search_space=NULL; free(read_line); read_line=NULL; continue; } free(search_space); search_space=NULL; if( (prog_name=malloc(read_len+1)) == NULL ) { printf("Out of memory\n"); fclose(inp_file); fclose(int_file); free(int_filename); free_optab(optab,num_ops); free_symtab(symtab); exit(7); } if( (work_string=malloc( (read_len-strlen(prog_name)) +1) ) == NULL) { printf("Out of memory\n"); fclose(inp_file); fclose(int_file); free(prog_name); free(int_filename); free_optab(optab,num_ops); free_symtab(symtab); exit(7); } sscanf(read_line,"%s%s",prog_name,work_string); if(!strcmp(work_string,"START")) { if( (work_string2=malloc((read_len-strlen(work_string))+1)) == NULL ) { free(work_string); free(prog_name); fclose(inp_file); fclose(int_file); free(int_filename); free_optab(optab,num_ops); free_symtab(symtab); printf("Out of memory\n"); exit(8); } char *searched=strstr(read_line,work_string); sscanf(searched+strlen(work_string),"%s",work_string2); if((*work_string2)=='\0') { printf("START directive requires an argument\n"); free(work_string); free(prog_name); free(work_string2); fclose(inp_file); fclose(int_file); free(int_filename); free_optab(optab,num_ops); free_symtab(symtab); exit(9); } char *waste; LOCCTR=strtol(work_string2,&waste,16); if(waste == work_string2) { printf("Bad Assembly coding...\n"); printf("No suitable value for operand for START directive\n"); free(work_string); free(prog_name); free(work_string2); fclose(inp_file); fclose(int_file); free(int_filename); free_optab(optab,num_ops); free_symtab(symtab); exit(9); } start_addr=LOCCTR; fprintf(int_file,"%lX\t%s\t%s\t%lX\n",LOCCTR,prog_name,work_string,start_addr); free(work_string2); free(prog_name); prog_name=NULL; free(work_string); work_string=NULL; free(read_line); read_line=NULL; to_read=0; while( (read_len=getline(&read_line,&to_read,inp_file)) != -1) { if( (search_space=(char *)malloc(read_len+1)) == NULL ) { printf("Out of memory\n"); fclose(inp_file); fclose(int_file); free(int_filename); free(read_line); free_optab(optab,num_ops); free_symtab(symtab); exit(10); } strcpy(search_space,""); sscanf(read_line,"%s",search_space); if(((*search_space)=='\0')||((*search_space)==';')) { free(search_space); search_space=NULL; free(read_line); read_line=NULL; continue; } free(search_space); search_space=NULL; break; } start_loop=1; } else { LOCCTR=start_addr=0; start_loop=1; } if(start_loop) break; } // Check if the loop exited becuase the EOF was reached. If so exit... if(read_len == -1) { if(read_line) free(read_line); fclose(inp_file); fclose(int_file); free(int_filename); free_optab(optab,num_ops); free_symtab(symtab); exit(0); } if( (work_string=malloc(read_len+1)) == NULL ) { printf("Out of memory\n"); fclose(inp_file); fclose(int_file); free(int_filename); free(read_line); free_optab(optab,num_ops); free_symtab(symtab); exit(11); } char *label=NULL; char *pneumonic=NULL; char *operand=NULL; decode(read_line,read_len,&label,&pneumonic,&operand,int_filename,inp_file,int_file,NULL,optab,num_ops,symtab,LOCCTR,NULL); if(label) label[strlen(label)-1]='\0'; while(strcmp(pneumonic,"END")) { if(label) fprintf(int_file,"%lX\t%s\t%s\t%s\n",LOCCTR,label,pneumonic,operand); else { if(operand) fprintf(int_file,"%lX\t%s\t%s\n",LOCCTR,pneumonic,operand); else fprintf(int_file,"%lX\t%s\n",LOCCTR,pneumonic); } if(label) { label[strlen(label)-1]='\0'; if(search_symtab(symtab,label)>0) { printf("Duplicate symbol at %lX\n",LOCCTR); fclose(inp_file); fclose(int_file); free(int_filename); free(read_line); if(label) free(label); if(operand) free(operand); if(pneumonic) free(pneumonic); free_optab(optab,num_ops); free_symtab(symtab); exit(12); } else { if(insert_symtab(label,symtab,LOCCTR)!=0) { printf("Failed to insert into symtab\n"); fclose(inp_file); fclose(int_file); free(int_filename); free(read_line); if(label) free(label); if(operand) free(operand); if(pneumonic) free(pneumonic); free_optab(optab,num_ops); free_symtab(symtab); exit(13); } } } char *opcode,*waste; if( (opcode=search_optab(optab,pneumonic,num_ops)) != NULL) { LOCCTR+=3; } else if(!strcmp(pneumonic,"WORD")) { LOCCTR+=3; } else if(!strcmp(pneumonic,"RESW")) { if(operand == NULL) { printf("Bad assembly coding...\n"); printf("No value given for operand near RESW directive at %lX\n",LOCCTR); fclose(inp_file); fclose(int_file); free(int_filename); free(read_line); if(label) free(label); if(operand) free(operand); if(pneumonic) free(pneumonic); free_optab(optab,num_ops); free_symtab(symtab); exit(23); } else { LOCCTR+=(3*strtol(operand,&waste,10)); if(waste == operand) { printf("Bad assembly coding...\n"); printf("No suitable value was found for allocating memory for RESW directive at %lX\n",LOCCTR); fclose(inp_file); fclose(int_file); free(int_filename); free(read_line); if(label) free(label); if(operand) free(operand); if(pneumonic) free(pneumonic); free_optab(optab,num_ops); free_symtab(symtab); exit(21); } } } else if(!strcmp(pneumonic,"RESB")) { if(operand == NULL) { printf("Bad assembly coding...\n"); printf("No value given for operand near RESB directive at %lX\n",LOCCTR); fclose(inp_file); fclose(int_file); free(int_filename); free(read_line); if(label) free(label); if(operand) free(operand); if(pneumonic) free(pneumonic); free_optab(optab,num_ops); free_symtab(symtab); exit(23); } else { LOCCTR+=(strtol(operand,&waste,10)); if(waste == operand) { printf("Bad assembly coding...\n"); printf("No suitable value was found for allocating memory for RESB directive at %lX\n",LOCCTR); fclose(inp_file); fclose(int_file); free(int_filename); free(read_line); if(label) free(label); if(operand) free(operand); if(pneumonic) free(pneumonic); free_optab(optab,num_ops); free_symtab(symtab); exit(22); } } } else if(!strcmp(pneumonic,"BYTE")) { long int old_locctr=LOCCTR; if(!operand) { printf("Bad assembly coding...\n"); printf("No operand corresponding to BYTE directive at %lX\n",LOCCTR); fclose(inp_file); fclose(int_file); free(int_filename); free(read_line); if(label) free(label); if(operand) free(operand); if(pneumonic) free(pneumonic); free_optab(optab,num_ops); free_symtab(symtab); exit(52); } if( ((*operand)=='c')|| ((*operand)=='C')) LOCCTR+=(strlen(operand)-3); else if( ((*operand)=='x')|| ((*operand)=='X')) { LOCCTR+=(strlen(operand)-3)/2; if( (strlen(operand)-3)%2) LOCCTR+=1; } else { printf("Bad assembly coding...\n"); printf("Wrong format for constant\n"); fclose(inp_file); fclose(int_file); free(int_filename); free(read_line); if(label) free(label); if(operand) free(operand); if(pneumonic) free(pneumonic); free_optab(optab,num_ops); free_symtab(symtab); exit(14); } if( (LOCCTR-old_locctr)>MC_SIZE ) { printf("Constant value too long...\n"); fclose(inp_file); fclose(int_file); free(int_filename); free(read_line); if(label) free(label); if(operand) free(operand); if(pneumonic) free(pneumonic); free_optab(optab,num_ops); free_symtab(symtab); exit(50); } if( !( (strlen(operand)>=4)&&(operand[1]=='\'')&&(operand[strlen(operand)-1]=='\'') ) ) { printf("Format for constant is convoluted at %lX...\n",old_locctr); fclose(inp_file); fclose(int_file); free(int_filename); free(read_line); if(label) free(label); if(operand) free(operand); if(pneumonic) free(pneumonic); free_optab(optab,num_ops); free_symtab(symtab); exit(51); } } else { printf("Opcode not found at %lX...\n",LOCCTR); fclose(inp_file); fclose(int_file); free(int_filename); free(read_line); if(label) free(label); if(operand) free(operand); if(pneumonic) free(pneumonic); free_optab(optab,num_ops); free_symtab(symtab); exit(15); } if(label) free(label); if(operand) free(operand); if(pneumonic) free(pneumonic); read_line=NULL; to_read=0; while( (read_len=getline(&read_line,&to_read,inp_file)) != -1) { if( (search_space=(char *)malloc(read_len+1)) == NULL ) { printf("Out of memory\n"); fclose(inp_file); fclose(int_file); free(int_filename); free(read_line); free_optab(optab,num_ops); free_symtab(symtab); exit(10); } strcpy(search_space,""); sscanf(read_line,"%s",search_space); if(((*search_space)=='\0')||((*search_space)==';')) { free(search_space); search_space=NULL; free(read_line); read_line=NULL; continue; } free(search_space); search_space=NULL; break; } if(read_len == -1) { printf("Bad assembly coding...\n"); printf("End of file was reached before END directive\n"); fclose(int_file); fclose(inp_file); free(int_filename); free_optab(optab,num_ops); free_symtab(symtab); exit(16); } label=NULL; pneumonic=NULL; operand=NULL; decode(read_line,read_len,&label,&pneumonic,&operand,int_filename,inp_file,int_file,NULL,optab,num_ops,symtab,LOCCTR,NULL); } //First Pass finished... Beginning second pass if(label) free(label); if(operand) free(operand); if(pneumonic) free(pneumonic); fprintf(int_file,"%lX\t%s",LOCCTR,read_line); free(read_line); label=NULL; operand=NULL; pneumonic=NULL; read_line=NULL; fclose(inp_file); out_file=fopen("a.out","w+"); if( out_file == NULL) { printf("Failed to open output file...\n"); fclose(int_file); free(int_filename); free_optab(optab,num_ops); free_symtab(symtab); exit(30); } //Write out the intermediate file and re-open it for second pass fclose(int_file); int_file=fopen(int_filename,"r"); if( int_file == NULL) { printf("Failed to open intermediate file...\n"); fclose(out_file); free(int_filename); free_optab(optab,num_ops); free_symtab(symtab); exit(31); } int_file=fopen(int_filename,"r"); if( int_file == NULL) { printf("Failed to open intermediate file...\n"); fclose(out_file); free(int_filename); free_optab(optab,num_ops); free_symtab(symtab); exit(31); } char *read_raw_line=NULL; char string_loc[35]; while( (read_len=getline(&read_raw_line,&to_read,int_file)) != -1) { read_raw_line[strlen(read_raw_line)-1]='\0'; fprintf(out_file,"%s",read_raw_line); read_raw_line[strlen(read_raw_line)]='\n'; memset(mc,0,MC_SIZE); //We dont have to worry about about newlines spaces etc sscanf(read_raw_line,"%s",string_loc); read_line=read_raw_line+strlen(string_loc)+1; decode(read_line,read_len,&label,&pneumonic,&operand,int_filename,NULL,int_file,out_file,optab,num_ops,symtab,LOCCTR,read_raw_line); if(!label) fprintf(out_file,"\t"); if(!operand) fprintf(out_file,"\t"); char *opcode; long int add; if( (opcode=search_optab(optab,pneumonic,num_ops)) != NULL ) { sprintf(mc,"%s",opcode); if(operand) { if( (add=search_symtab(symtab,operand)) > 0 ) { sprintf(mc+strlen(mc),"%lX",add); } else { printf("Operand not available in symbol table\n"); fclose(int_file); fclose(out_file); free(int_filename); free(read_raw_line); free_optab(optab,num_ops); free_symtab(symtab); exit(35); } } } else if(!strcmp(pneumonic,"BYTE")) { convert_const(&operand); } read_raw_line[strlen(read_raw_line)-1]='\0'; fprintf(out_file,"\t\t%s\n",mc); if(label) free(label); if(operand) free(operand); if(pneumonic) free(pneumonic); label=pneumonic=operand=NULL; free(read_raw_line); read_raw_line=NULL; } if(label) free(label); if(operand) free(operand); if(pneumonic) free(pneumonic); fclose(int_file); fclose(out_file); free(int_filename); free_optab(optab,num_ops); free_symtab(symtab); return 0; }
void iprofiletable() { nialptr result; /* result to be returned */ int c1, c2; int num_funs = 0; /* total number of used functions */ int pos = 0; /* current count of used fucntions */ if (newprofile) { buildfault("no profile available"); return; } #ifndef OLD_BUILD_SYMBOL_TABLE if (symtab) free_symtab(); symtab = NULL; build_symbol_table(); #endif /* If profiling is still on, then turn it off */ if (profile == true) { apush(createbool(0)); isetprofile(); apop(); } /* traverse the call tree placing nodes in the symbol table entries */ if (!traversed) { traverse_tree(calltree); traversed = true; } /* count the number of called functions so we know how big to make the container array. Funcitons in the symbol table that are not called at all are excluded */ for (c1 = 0; c1 < symtabsize; c1++) if ((symtab[c1]->num_locs > 0) || (symtab[c1]->num_rcalls > 0)) num_funs++; /* create the outer most container to hold the table */ result = new_create_array(atype, 1, 0, &num_funs); for (c1 = 0; c1 < symtabsize; c1++) { if ((symtab[c1]->num_locs > 0) || (symtab[c1]->num_rcalls > 0)) { double totaloptime = 0; int totalopcalls = 0; int totalropcalls; int len = 5; /* create the table entry */ nialptr table_entry = new_create_array(atype, 1, 0, &len); /* store it in the outer table */ store_array(result, pos, table_entry); for (c2 = 0; c2 < symtab[c1]->num_locs; c2++) { if (symtab[c1]->id != symtab[c1]->locations[c2]->parent->opid) /* omit adding calls and time for direct recursions */ { totaloptime += symtab[c1]->locations[c2]->total_time; totalopcalls += symtab[c1]->locations[c2]->total_calls; } } totalropcalls = symtab[c1]->num_rcalls; /* fill in the cells in the table entry */ store_array(table_entry, 0, makephrase(symtab[c1]->name)); store_array(table_entry, 1, createint(totalopcalls)); store_array(table_entry, 2, createint(totalropcalls)); store_array(table_entry, 3, createreal(totaloptime)); { struct node **chlist; nialptr child_array; int c, used, cpos = 0; int children = 0; chlist = merge_children(symtab[c1], &used); for (c = 0; c < used; c++) if (chlist[c]->opid != symtab[c1]->id) /* recursions only counted */ children++; child_array = new_create_array(atype, 1, 0, &children); store_array(table_entry, 4, child_array); for (c = 0; c < used; c++) { nialptr child_entry; int len = 5; if (chlist[c]->opid == symtab[c1]->id) /* recursions only counted */ break; /* create each child entry and place it in the child list */ child_entry = new_create_array(atype, 1, 0, &len); store_array(child_array, cpos, child_entry); /* fill in the information about the child entry */ store_array(child_entry, 0, makephrase(num_to_name(chlist[c]->opid))); store_array(child_entry, 1, createint(chlist[c]->total_calls)); store_array(child_entry, 2, createint(0)); store_array(child_entry, 3, createreal(chlist[c]->total_time)); store_array(child_entry, 4, Null); cpos++; } free_merge_list(chlist, used); } } pos++; } apush(result); }
void iprofile() { char sbuf[1000]; nialptr z; FILE *prf = STDOUT; int c1, c2; int i; double real_total_time = 0; double profile_duration_time; int NMWIDTH = 30; /* grab the argument */ z = apop(); if (newprofile) { buildfault("no profile available"); freeup(z); return; } /* handle input argument */ if (kind(z) == phrasetype) { apush(z); istring(); z = apop(); } if (tally(z) != 0) { if (!istext(z)) { buildfault("profile file name arg is not text"); freeup(z); return; } else { prf = openfile(pfirstchar(z), 'w', 't'); if (prf == OPENFAILED) { buildfault("unable to open specified file to write profile to"); freeup(z); return; /* exit_cover(NC_PROFILE_FILE_W); removed Nov 22/95 because this * cleared profile information */ } } } freeup(z); /* If profiling is still on, then turn it off */ if (profile == true) { apush(createbool(0)); isetprofile(); apop(); } #ifndef OLD_BUILD_SYMBOL_TABLE if (symtab) free_symtab(); symtab = NULL; build_symbol_table(); #endif profile_duration_time = (calltree->total_time); for (i = 0;i <calltree->num_children;i++) { real_total_time += calltree->children[i]->total_time; } /* traverse the call tree placing nodes in the symbol table entries */ if (!traversed) { traverse_tree(calltree); traversed = true; } /* generate the output and place it in the output file or stdout */ /* if a filename has been specified then write that out */ if (tally(z)) { sprintf(sbuf,"Profile output file: \"%s\"\n\n",pfirstchar(z)); writechars(prf, sbuf, strlen(sbuf), false); } sprintf(sbuf, "\nTotal execution time of profile session: \t%f\n", profile_duration_time); writechars(prf, sbuf, strlen(sbuf), false); sprintf(sbuf, "Total execution time in top level calls: \t%f\n\n",real_total_time); writechars(prf, sbuf, strlen(sbuf), false); /* header line for all data */ sprintf(sbuf, "op name[.tr arg] calls[rec] "); writechars(prf, sbuf, strlen(sbuf), false); sprintf(sbuf, "time time/call %% time\n"); writechars(prf, sbuf, strlen(sbuf), false); for (c1 = 0; c1 < symtabsize; c1++) { if ((symtab[c1]->num_locs > 0) || (symtab[c1]->num_rcalls > 0)) { double totaloptime = 0; int totalopcalls = 0; int totalropcalls; char *tmp; for (c2 = 0; c2 < symtab[c1]->num_locs; c2++) { if (symtab[c1]->id != symtab[c1]->locations[c2]->parent->opid) /* omit adding calls and time for direct recursions */ { totaloptime += symtab[c1]->locations[c2]->total_time; totalopcalls += symtab[c1]->locations[c2]->total_calls; } } totalropcalls = symtab[c1]->num_rcalls; sprintf(sbuf, "%s%5d", (tmp = padright(NMWIDTH, (char *) symtab[c1]->name)), totalopcalls); writechars(prf, sbuf, strlen(sbuf), false); free(tmp); if (totalropcalls != 0) { sprintf(sbuf, "[%5d]", totalropcalls); writechars(prf, sbuf, strlen(sbuf), false); } else { sprintf(sbuf, " "); writechars(prf, sbuf, strlen(sbuf), false); } /* details for each definition */ sprintf(sbuf, "%8.2f %8.4f %8.1f%s\n", totaloptime, (totaloptime / totalopcalls), 100 * (totaloptime / real_total_time), ((symtab[c1]->toplevel_call == true)?"<":"")); writechars(prf, sbuf, strlen(sbuf), false); { struct node **chlist; int c, used; char tname[40]; chlist = merge_children(symtab[c1], &used); for (c = 0; c < used; c++) { char *tmp; if (chlist[c]->opid == symtab[c1]->id) /* recursions only counted */ break; strcpy(tname, num_to_name(chlist[c]->opid)); /* details for each definition it calls */ if (chlist[c]->total_time > 0.0) sprintf(sbuf, " %s%5d %8.2f %8.4f %8.2f\n", (tmp = padright(NMWIDTH - 1, tname)), chlist[c]->total_calls, chlist[c]->total_time, chlist[c]->total_time / chlist[c]->total_calls, 100 * (chlist[c]->total_time / totaloptime)); else sprintf(sbuf, " %s%5d %8.2f %8.4f %8.2f\n", (tmp = padright(NMWIDTH - 1, tname)), chlist[c]->total_calls, chlist[c]->total_time, 0.0, 0.0); writechars(prf, sbuf, strlen(sbuf), false); free(tmp); } free_merge_list(chlist, used); } sprintf(sbuf, "\n"); writechars(prf, sbuf, strlen(sbuf), false); } } if (prf != STDOUT) closefile(prf); apush(Nullexpr); return; }
int read_g96_conf(FILE *fp, const char *infile, t_trxframe *fr, char *line) { t_symtab *symtab = NULL; gmx_bool bAtStart, bTime, bAtoms, bPos, bVel, bBox, bEnd, bFinished; int natoms, nbp; double db1, db2, db3, db4, db5, db6, db7, db8, db9; bAtStart = (ftell(fp) == 0); clear_trxframe(fr, FALSE); if (!symtab) { snew(symtab, 1); open_symtab(symtab); } natoms = 0; if (bAtStart) { while (!fr->bTitle && fgets2(line, STRLEN, fp)) { fr->bTitle = (strcmp(line, "TITLE") == 0); } if (fr->title == NULL) { fgets2(line, STRLEN, fp); fr->title = gmx_strdup(line); } bEnd = FALSE; while (!bEnd && fgets2(line, STRLEN, fp)) { bEnd = (strcmp(line, "END") == 0); } fgets2(line, STRLEN, fp); } /* Do not get a line if we are not at the start of the file, * * because without a parameter file we don't know what is in * * the trajectory and we have already read the line in the * * previous call (VERY DIRTY). */ bFinished = FALSE; do { bTime = (strcmp(line, "TIMESTEP") == 0); bAtoms = (strcmp(line, "POSITION") == 0); bPos = (bAtoms || (strcmp(line, "POSITIONRED") == 0)); bVel = (strncmp(line, "VELOCITY", 8) == 0); bBox = (strcmp(line, "BOX") == 0); if (bTime) { if (!fr->bTime && !fr->bX) { fr->bStep = bTime; fr->bTime = bTime; do { bFinished = (fgets2(line, STRLEN, fp) == NULL); } while (!bFinished && (line[0] == '#')); sscanf(line, "%15d%15lf", &(fr->step), &db1); fr->time = db1; } else { bFinished = TRUE; } } if (bPos) { if (!fr->bX) { fr->bAtoms = bAtoms; fr->bX = bPos; natoms = read_g96_pos(line, symtab, fp, infile, fr); } else { bFinished = TRUE; } } if (fr->v && bVel) { fr->bV = bVel; natoms = read_g96_vel(line, fp, infile, fr); } if (bBox) { fr->bBox = bBox; clear_mat(fr->box); bEnd = FALSE; while (!bEnd && fgets2(line, STRLEN, fp)) { bEnd = (strncmp(line, "END", 3) == 0); if (!bEnd && (line[0] != '#')) { nbp = sscanf(line, "%15lf%15lf%15lf%15lf%15lf%15lf%15lf%15lf%15lf", &db1, &db2, &db3, &db4, &db5, &db6, &db7, &db8, &db9); if (nbp < 3) { gmx_fatal(FARGS, "Found a BOX line, but no box in %s", infile); } fr->box[XX][XX] = db1; fr->box[YY][YY] = db2; fr->box[ZZ][ZZ] = db3; if (nbp == 9) { fr->box[XX][YY] = db4; fr->box[XX][ZZ] = db5; fr->box[YY][XX] = db6; fr->box[YY][ZZ] = db7; fr->box[ZZ][XX] = db8; fr->box[ZZ][YY] = db9; } } } bFinished = TRUE; } } while (!bFinished && fgets2(line, STRLEN, fp)); free_symtab(symtab); fr->natoms = natoms; return natoms; }
void read_stx_conf(const char *infile, char *title, t_atoms *atoms, rvec x[], rvec *v, int *ePBC, matrix box) { FILE *in; char buf[256]; gmx_mtop_t *mtop; t_topology top; t_trxframe fr; int i, ftp, natoms; real d; char g96_line[STRLEN+1]; if (atoms->nr == 0) { fprintf(stderr, "Warning: Number of atoms in %s is 0\n", infile); } else if (atoms->atom == NULL) { gmx_mem("Uninitialized array atom"); } if (ePBC) { *ePBC = -1; } ftp = fn2ftp(infile); switch (ftp) { case efGRO: read_whole_conf(infile, title, atoms, x, v, box); break; case efG96: fr.title = NULL; fr.natoms = atoms->nr; fr.atoms = atoms; fr.x = x; fr.v = v; fr.f = NULL; in = gmx_fio_fopen(infile, "r"); read_g96_conf(in, infile, &fr, g96_line); gmx_fio_fclose(in); copy_mat(fr.box, box); strncpy(title, fr.title, STRLEN); break; case efPDB: case efBRK: case efENT: read_pdb_conf(infile, title, atoms, x, ePBC, box, TRUE, NULL); break; case efESP: read_espresso_conf(infile, atoms, x, v, box); break; case efTPR: case efTPB: case efTPA: snew(mtop, 1); i = read_tpx(infile, NULL, box, &natoms, x, v, NULL, mtop); if (ePBC) { *ePBC = i; } strcpy(title, *(mtop->name)); /* Free possibly allocated memory */ done_atom(atoms); *atoms = gmx_mtop_global_atoms(mtop); top = gmx_mtop_t_to_t_topology(mtop); tpx_make_chain_identifiers(atoms, &top.mols); sfree(mtop); /* The strings in the symtab are still in use in the returned t_atoms * structure, so we should not free them. But there is no place to put the * symbols; the only choice is to leak the memory... * So we clear the symbol table before freeing the topology structure. */ free_symtab(&top.symtab); done_top(&top); break; default: gmx_incons("Not supported in read_stx_conf"); } }
gmx_bool read_next_frame(const gmx_output_env_t *oenv, t_trxstatus *status, t_trxframe *fr) { real pt; int ct; gmx_bool bOK, bRet, bMissingData = FALSE, bSkip = FALSE; int ftp; bRet = FALSE; pt = fr->tf; do { clear_trxframe(fr, FALSE); fr->tppf = fr->tpf; fr->tpf = fr->tf; if (status->tng) { /* Special treatment for TNG files */ ftp = efTNG; } else { ftp = gmx_fio_getftp(status->fio); } switch (ftp) { case efTRR: bRet = gmx_next_frame(status, fr); break; case efCPT: /* Checkpoint files can not contain mulitple frames */ break; case efG96: { t_symtab *symtab; snew(symtab, 1); open_symtab(symtab); read_g96_conf(gmx_fio_getfp(status->fio), NULL, fr, symtab, status->persistent_line); free_symtab(symtab); bRet = (fr->natoms > 0); break; } case efXTC: /* B. Hess 2005-4-20 * Sometimes is off by one frame * and sometimes reports frame not present/file not seekable */ /* DvdS 2005-05-31: this has been fixed along with the increased * accuracy of the control over -b and -e options. */ if (bTimeSet(TBEGIN) && (fr->tf < rTimeValue(TBEGIN))) { if (xtc_seek_time(status->fio, rTimeValue(TBEGIN), fr->natoms, TRUE)) { gmx_fatal(FARGS, "Specified frame (time %f) doesn't exist or file corrupt/inconsistent.", rTimeValue(TBEGIN)); } initcount(status); } bRet = read_next_xtc(status->fio, fr->natoms, &fr->step, &fr->time, fr->box, fr->x, &fr->prec, &bOK); fr->bPrec = (bRet && fr->prec > 0); fr->bStep = bRet; fr->bTime = bRet; fr->bX = bRet; fr->bBox = bRet; if (!bOK) { /* Actually the header could also be not ok, but from bOK from read_next_xtc this can't be distinguished */ fr->not_ok = DATA_NOT_OK; } break; case efTNG: bRet = gmx_read_next_tng_frame(status->tng, fr, NULL, 0); break; case efPDB: bRet = pdb_next_x(status, gmx_fio_getfp(status->fio), fr); break; case efGRO: bRet = gro_next_x_or_v(gmx_fio_getfp(status->fio), fr); break; default: #ifdef GMX_USE_PLUGINS bRet = read_next_vmd_frame(fr); #else gmx_fatal(FARGS, "DEATH HORROR in read_next_frame ftp=%s,status=%s", ftp2ext(gmx_fio_getftp(status->fio)), gmx_fio_getname(status->fio)); #endif } fr->tf = fr->time; if (bRet) { bMissingData = (((fr->flags & TRX_NEED_X) && !fr->bX) || ((fr->flags & TRX_NEED_V) && !fr->bV) || ((fr->flags & TRX_NEED_F) && !fr->bF)); bSkip = FALSE; if (!bMissingData) { ct = check_times2(fr->time, fr->t0, fr->bDouble); if (ct == 0 || ((fr->flags & TRX_DONT_SKIP) && ct < 0)) { printcount(status, oenv, fr->time, FALSE); } else if (ct > 0) { bRet = FALSE; } else { printcount(status, oenv, fr->time, TRUE); bSkip = TRUE; } } } } while (bRet && (bMissingData || bSkip)); if (!bRet) { printlast(status, oenv, pt); if (fr->not_ok) { printincomp(status, fr); } } return bRet; }
int read_first_frame(const gmx_output_env_t *oenv, t_trxstatus **status, const char *fn, t_trxframe *fr, int flags) { t_fileio *fio; gmx_bool bFirst, bOK; int ftp = fn2ftp(fn); clear_trxframe(fr, TRUE); fr->flags = flags; bFirst = TRUE; snew((*status), 1); status_init( *status ); (*status)->nxframe = 1; initcount(*status); if (efTNG == ftp) { /* Special treatment for TNG files */ gmx_tng_open(fn, 'r', &(*status)->tng); } else { fio = (*status)->fio = gmx_fio_open(fn, "r"); } switch (ftp) { case efTRR: break; case efCPT: read_checkpoint_trxframe(fio, fr); bFirst = FALSE; break; case efG96: { /* Can not rewind a compressed file, so open it twice */ if (!(*status)->persistent_line) { /* allocate the persistent line */ snew((*status)->persistent_line, STRLEN+1); } t_symtab *symtab; snew(symtab, 1); open_symtab(symtab); read_g96_conf(gmx_fio_getfp(fio), fn, fr, symtab, (*status)->persistent_line); free_symtab(symtab); gmx_fio_close(fio); clear_trxframe(fr, FALSE); if (flags & (TRX_READ_X | TRX_NEED_X)) { snew(fr->x, fr->natoms); } if (flags & (TRX_READ_V | TRX_NEED_V)) { snew(fr->v, fr->natoms); } (*status)->fio = gmx_fio_open(fn, "r"); break; } case efXTC: if (read_first_xtc(fio, &fr->natoms, &fr->step, &fr->time, fr->box, &fr->x, &fr->prec, &bOK) == 0) { GMX_RELEASE_ASSERT(!bOK, "Inconsistent results - OK status from read_first_xtc, but 0 atom coords read"); fr->not_ok = DATA_NOT_OK; } if (fr->not_ok) { fr->natoms = 0; printincomp(*status, fr); } else { fr->bPrec = (fr->prec > 0); fr->bStep = TRUE; fr->bTime = TRUE; fr->bX = TRUE; fr->bBox = TRUE; printcount(*status, oenv, fr->time, FALSE); } bFirst = FALSE; break; case efTNG: fr->step = -1; if (!gmx_read_next_tng_frame((*status)->tng, fr, NULL, 0)) { fr->not_ok = DATA_NOT_OK; fr->natoms = 0; printincomp(*status, fr); } else { printcount(*status, oenv, fr->time, FALSE); } bFirst = FALSE; break; case efPDB: pdb_first_x(*status, gmx_fio_getfp(fio), fr); if (fr->natoms) { printcount(*status, oenv, fr->time, FALSE); } bFirst = FALSE; break; case efGRO: if (gro_first_x_or_v(gmx_fio_getfp(fio), fr)) { printcount(*status, oenv, fr->time, FALSE); } bFirst = FALSE; break; default: #ifdef GMX_USE_PLUGINS fprintf(stderr, "The file format of %s is not a known trajectory format to GROMACS.\n" "Please make sure that the file is a trajectory!\n" "GROMACS will now assume it to be a trajectory and will try to open it using the VMD plug-ins.\n" "This will only work in case the VMD plugins are found and it is a trajectory format supported by VMD.\n", fn); gmx_fio_fp_close(fio); /*only close the file without removing FIO entry*/ if (!read_first_vmd_frame(fn, fr)) { gmx_fatal(FARGS, "Not supported in read_first_frame: %s", fn); } #else gmx_fatal(FARGS, "Not supported in read_first_frame: %s. Please make sure that the file is a trajectory.\n" "GROMACS is not compiled with plug-in support. Thus it cannot read non-GROMACS trajectory formats using the VMD plug-ins.\n" "Please compile with plug-in support if you want to read non-GROMACS trajectory formats.\n", fn); #endif break; } fr->tf = fr->time; /* Return FALSE if we read a frame that's past the set ending time. */ if (!bFirst && (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) > 0)) { fr->t0 = fr->time; return FALSE; } if (bFirst || (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) < 0)) { /* Read a frame when no frame was read or the first was skipped */ if (!read_next_frame(oenv, *status, fr)) { return FALSE; } } fr->t0 = fr->time; /* We need the number of atoms for random-access XTC searching, even when * we don't have access to the actual frame data. */ (*status)->natoms = fr->natoms; return (fr->natoms > 0); }
int read_pdbfile(FILE *in, char *title, int *model_nr, t_atoms *atoms, rvec x[], int *ePBC, matrix box, gmx_bool bChange, gmx_conect conect) { gmx_conect_t *gc = (gmx_conect_t *)conect; t_symtab symtab; gmx_bool bCOMPND; gmx_bool bConnWarn = FALSE; char line[STRLEN+1]; int line_type; char *c, *d; int natom, chainnum, nres_ter_prev = 0; char chidmax = ' '; gmx_bool bStop = FALSE; if (ePBC) { /* Only assume pbc when there is a CRYST1 entry */ *ePBC = epbcNONE; } if (box != NULL) { clear_mat(box); } open_symtab(&symtab); bCOMPND = FALSE; title[0] = '\0'; natom = 0; chainnum = 0; while (!bStop && (fgets2(line, STRLEN, in) != NULL)) { line_type = line2type(line); switch (line_type) { case epdbATOM: case epdbHETATM: natom = read_atom(&symtab, line, line_type, natom, atoms, x, chainnum, bChange); break; case epdbANISOU: if (atoms->pdbinfo) { read_anisou(line, natom, atoms); } break; case epdbCRYST1: read_cryst1(line, ePBC, box); break; case epdbTITLE: case epdbHEADER: if (strlen(line) > 6) { c = line+6; /* skip HEADER or TITLE and spaces */ while (c[0] != ' ') { c++; } while (c[0] == ' ') { c++; } /* truncate after title */ d = strstr(c, " "); if (d) { d[0] = '\0'; } if (strlen(c) > 0) { strcpy(title, c); } } break; case epdbCOMPND: if ((!strstr(line, ": ")) || (strstr(line+6, "MOLECULE:"))) { if (!(c = strstr(line+6, "MOLECULE:")) ) { c = line; } /* skip 'MOLECULE:' and spaces */ while (c[0] != ' ') { c++; } while (c[0] == ' ') { c++; } /* truncate after title */ d = strstr(c, " "); if (d) { while ( (d[-1] == ';') && d > c) { d--; } d[0] = '\0'; } if (strlen(c) > 0) { if (bCOMPND) { strcat(title, "; "); strcat(title, c); } else { strcpy(title, c); } } bCOMPND = TRUE; } break; case epdbTER: chainnum++; break; case epdbMODEL: if (model_nr) { sscanf(line, "%*s%d", model_nr); } break; case epdbENDMDL: bStop = TRUE; break; case epdbCONECT: if (gc) { gmx_conect_addline(gc, line); } else if (!bConnWarn) { fprintf(stderr, "WARNING: all CONECT records are ignored\n"); bConnWarn = TRUE; } break; default: break; } } free_symtab(&symtab); return natom; }