=================================================================== RCS file: /home/cvs/OpenXM/src/hgm/mh/src/sfile.c,v retrieving revision 1.4 retrieving revision 1.8 diff -u -p -r1.4 -r1.8 --- OpenXM/src/hgm/mh/src/sfile.c 2013/02/20 05:56:16 1.4 +++ OpenXM/src/hgm/mh/src/sfile.c 2013/02/25 12:11:23 1.8 @@ -1,12 +1,18 @@ /* - $OpenXM: OpenXM/src/hgm/mh/src/sfile.c,v 1.3 2013/02/20 05:20:49 takayama Exp $ + $OpenXM: OpenXM/src/hgm/mh/src/sfile.c,v 1.7 2013/02/24 21:36:49 takayama Exp $ */ #include +#include +#include #include "sfile.h" #define SSIZE 5 +int MH_DEBUG = 1; void *mh_malloc(int s) { void *p; + static int total=0; + total += s; + if (MH_DEBUG) printf("mh_malloc total allocated memory: %f M\n",(float)total/(float)(1024*1024)); p = (void*)malloc(s); if (p == NULL) { fprintf(stderr,"No memory.\n"); mh_exit(-1); @@ -14,13 +20,15 @@ void *mh_malloc(int s) { return(p); } mh_free(void *p) { - free(p); return(0); + if (MH_DEBUG) printf("mh_free at %p\n",p); + free(p); /* free in mh_free */ + return(0); } mh_exit(int n) { static int standalone=0; - if (n == 0x7fffffff) { standalone=1; return(0);} + if (n == MH_RESET_EXIT) { standalone=1; return(0);} if (standalone) exit(n); else { fprintf(stderr,"Fatal error mh_exit(%d) in mh-w-n.\n",n); @@ -36,7 +44,11 @@ struct SFILE *mh_fopen(char *name,char *mode,int byFil if (byFile) { sfp->byFile = 1; - sfp->fp = fopen(name,mode); + if (strcmp(name,"stdout")==0) { + sfp->fp = stdout; + }else{ + sfp->fp = fopen(name,mode); + } if (sfp->fp == NULL) return(NULL); else return(sfp); }else if (strcmp(mode,"r")==0) { @@ -89,14 +101,15 @@ int mh_fputs(char *str,struct SFILE *sfp) { s = sfp->s; len = sfp->len; pt = sfp->pt; limit=sfp->limit; inputLen=strlen(str); if (inputLen+len+1 > limit) { - limit *= 2; + limit = (inputLen+len+1)*2; s = (char *) mh_malloc(limit); if (s == NULL) return(EOF); strcpy(s,sfp->s); - free(sfp->s); + mh_free(sfp->s); } strcpy(&(s[len]),str); len += inputLen; + /* printf("mh_fputs(%d):[%s]\n",len,s); */ sfp->s=s; sfp->len=len; sfp->limit=limit; return(0); } @@ -107,9 +120,9 @@ int mh_fclose(struct SFILE *sfp) { if (sfp->byFile) return fclose(sfp->fp); if (! (sfp->forRead)) { if (!sfp->copied) fprintf(stderr,"Warning in mh_fclose. sfp->s has not been copied, but deallocated.\n"); - if (sfp->s != NULL) { free(sfp->s); sfp->s = NULL; } + if (sfp->s != NULL) { mh_free(sfp->s); sfp->s = NULL; } } - free(sfp); + mh_free(sfp); } int mh_outstr(char *str,int size,struct SFILE *sfp) {