66 int numinfilelist = 0;
69 #define BANNER (4*sizeof(LONG)) 70 void *malloclist[60000];
71 LONG mallocsizes[60000];
72 char *mallocstrings[60000];
73 int nummalloclist = 0;
77 extern "C" getdtablesize();
90 #error "MALLOCPROTECT": undefine "TRAPSIGNALS" in unix.h first! 92 #include "mallocprotect.h" 98 #define M_alloc mprotectMalloc 102 #ifdef TERMMALLOCDEBUG 103 WORD **DebugHeap1, **DebugHeap2;
112 UBYTE *LoadInputFile(UBYTE *filename,
int type)
116 UBYTE *buffer, *name = filename;
118 handle = LocateFile(&name,type);
119 if ( handle < 0 )
return(0);
121 SeekFile(handle,&scrpos,SEEK_END);
122 TELLFILE(handle,&scrpos);
123 filesize = BASEPOSITION(scrpos);
125 SeekFile(handle,&scrpos,SEEK_SET);
126 buffer = (UBYTE *)Malloc1(filesize+2,
"LoadInputFile");
127 if ( ReadFile(handle,buffer,filesize) != filesize ) {
128 Error1(
"Read error for file ",name);
129 M_free(buffer,
"LoadInputFile");
130 if ( name != filename ) M_free(name,
"FromLoadInputFile");
135 if ( type == PROCEDUREFILE || type == SETUPFILE ) {
136 buffer[filesize] =
'\n';
137 buffer[filesize+1] = 0;
140 buffer[filesize] = 0;
142 if ( name != filename ) M_free(name,
"FromLoadInputFile");
151 UBYTE ReadFromStream(
STREAM *stream)
156 if ( stream->type == PIPESTREAM ) {
160 RWLOCKR(AM.handlelock);
161 f = (FILE *)(filelist[stream->handle]);
162 UNRWLOCK(AM.handlelock);
164 if ( cc == EOF )
return(ENDOFSTREAM);
170 if ( PF.me == MASTER ) {
173 RWLOCKR(AM.handlelock);
174 f = (FILE *)filelist[stream->handle];
175 UNRWLOCK(AM.handlelock);
177 end = stream->
buffer + stream->buffersize;
195 stream->inbuffer = len;
196 stream->
top = stream->
buffer + stream->inbuffer;
197 if ( stream->
pointer == stream->
top )
return ENDOFSTREAM;
201 if ( stream->eqnum == 1 ) { stream->eqnum = 0; stream->linenumber++; }
202 if ( c == LINEFEED ) stream->eqnum = 1;
207 #ifdef WITHEXTERNALCHANNEL 208 if ( stream->type == EXTERNALCHANNELSTREAM ) {
210 cc = getcFromExtChannel();
217 Error0(
"No current external channel");
223 if ( stream->eqnum == 1 ) { stream->eqnum = 0; stream->linenumber++; }
224 if ( c == LINEFEED ) stream->eqnum = 1;
230 if ( stream->type != FILESTREAM )
return(ENDOFSTREAM);
231 if ( stream->fileposition != stream->bufferposition+stream->inbuffer ) {
232 stream->fileposition = stream->bufferposition+stream->inbuffer;
233 SETBASEPOSITION(scrpos,stream->fileposition);
234 SeekFile(stream->handle,&scrpos,SEEK_SET);
236 stream->bufferposition = stream->fileposition;
237 stream->inbuffer = ReadFile(stream->handle,
238 stream->
buffer,stream->buffersize);
239 if ( stream->inbuffer <= 0 )
return(ENDOFSTREAM);
240 stream->
top = stream->
buffer + stream->inbuffer;
242 stream->fileposition = stream->bufferposition + stream->inbuffer;
244 if ( stream->eqnum == 1 ) { stream->eqnum = 0; stream->linenumber++; }
246 if ( c == LINEFEED ) stream->eqnum = 1;
255 UBYTE GetFromStream(
STREAM *stream)
258 if ( stream->isnextchar > 0 ) {
259 return(stream->nextchar[--stream->isnextchar]);
261 c1 = ReadFromStream(stream);
262 if ( c1 == LINEFEED || c1 == CARRIAGERETURN ) {
263 c2 = ReadFromStream(stream);
264 if ( c2 == c1 || ( c2 != LINEFEED && c2 != CARRIAGERETURN ) ) {
265 stream->isnextchar = 1;
266 stream->nextchar[0] = c2;
278 UBYTE LookInStream(
STREAM *stream)
280 UBYTE c = GetFromStream(stream);
281 UngetFromStream(stream,c);
290 STREAM *OpenStream(UBYTE *name,
int type,
int prevarmode,
int raiselow)
293 UBYTE *rhsofvariable, *s, *newname, c;
298 case REVERSEFILESTREAM:
306 handle = LocateFile(&newname,-1);
307 if ( handle < 0 )
return(0);
309 SeekFile(handle,&scrpos,SEEK_END);
310 TELLFILE(handle,&scrpos);
311 filesize = BASEPOSITION(scrpos);
313 SeekFile(handle,&scrpos,SEEK_SET);
314 if ( filesize > AM.MaxStreamSize && type == FILESTREAM )
315 filesize = AM.MaxStreamSize;
316 stream = CreateStream((UBYTE *)
"filestream");
320 stream->
buffer = (UBYTE *)Malloc1(filesize+1,
"name of input stream");
321 stream->inbuffer = ReadFile(handle,stream->
buffer,filesize);
322 if ( type == REVERSEFILESTREAM ) {
323 if ( ReverseStatements(stream) ) {
324 M_free(stream->
buffer,
"name of input stream");
328 stream->
top = stream->
buffer + stream->inbuffer;
330 stream->handle = handle;
331 stream->buffersize = filesize;
332 stream->fileposition = stream->inbuffer;
333 if ( newname != name ) stream->
name = newname;
334 else if ( name ) stream->
name = strDup1(name,
"name of input stream");
337 stream->prevline = stream->linenumber = 1;
341 if ( ( rhsofvariable = GetPreVar(name,WITHERROR) ) == 0 )
return(0);
342 stream = CreateStream((UBYTE *)
"var-stream");
346 stream->inbuffer = s - stream->
buffer;
347 stream->
name = AC.CurrentStream->name;
348 stream->linenumber = AC.CurrentStream->linenumber;
349 stream->prevline = AC.CurrentStream->prevline;
350 stream->eqnum = AC.CurrentStream->eqnum;
351 stream->
pname = strDup1(name,
"stream->pname");
352 stream->olddelay = AP.AllowDelay;
353 s = stream->
pname;
while ( *s ) s++;
354 while ( s[-1] ==
'+' || s[-1] ==
'-' ) s--;
359 if ( ( num = GetDollar(name) ) < 0 ) {
365 s = name;
while ( *s && *s !=
'[' ) s++;
366 if ( *s == 0 )
return(0);
368 if ( ( num = GetDollar(name) ) < 0 )
return(0);
371 if ( *s == 0 || FG.cTable[*s] != 1 || *s ==
']' ) {
372 MesPrint(
"@Illegal factor number for dollar variable");
375 while ( *s && FG.cTable[*s] == 1 ) {
376 numfac = 10*numfac+*s++-
'0';
378 if ( *s !=
']' || s[1] != 0 ) {
379 MesPrint(
"@Illegal factor number for $ variable");
382 stream = CreateStream((UBYTE *)
"dollar-stream");
383 stream->
buffer = stream->
pointer = s = WriteDollarFactorToBuffer(num,numfac,1);
386 stream = CreateStream((UBYTE *)
"dollar-stream");
387 stream->
buffer = stream->
pointer = s = WriteDollarToBuffer(num,1);
391 stream->inbuffer = s - stream->
buffer;
392 stream->
name = AC.CurrentStream->name;
393 stream->linenumber = AC.CurrentStream->linenumber;
394 stream->prevline= AC.CurrentStream->prevline;
395 stream->eqnum = AC.CurrentStream->eqnum;
396 stream->
pname = strDup1(name,
"stream->pname");
397 s = stream->
pname;
while ( *s ) s++;
398 while ( s[-1] ==
'+' || s[-1] ==
'-' ) s--;
401 AO.DollarOutSizeBuffer = 0;
402 AO.DollarOutBuffer = 0;
403 AO.DollarInOutBuffer = 0;
409 stream = CreateStream((UBYTE *)
"calculator");
413 stream->inbuffer = s - stream->
buffer;
414 stream->
name = AC.CurrentStream->name;
415 stream->linenumber = AC.CurrentStream->linenumber;
416 stream->prevline = AC.CurrentStream->prevline;
421 stream = CreateStream((UBYTE *)
"pipe");
425 if ( ( f = popen((
char *)name,
"r") ) == 0 ) {
426 Error0(
"@Cannot create pipe");
428 stream->handle = CreateHandle();
429 RWLOCKW(AM.handlelock);
430 filelist[stream->handle] = (FILES *)f;
431 UNRWLOCK(AM.handlelock);
434 stream->inbuffer = 0;
439 if ( PF.me == MASTER ) {
440 f = popen((
char *)name,
"r");
442 if ( f == 0 ) Error0(
"@Cannot create pipe");
448 stream->handle = CreateHandle();
449 RWLOCKW(AM.handlelock);
450 filelist[stream->handle] = (FILES *)f;
451 UNRWLOCK(AM.handlelock);
454 stream->buffersize = AM.MaxStreamSize;
455 stream->
buffer = (UBYTE *)Malloc1(stream->buffersize,
"pipe buffer");
456 stream->inbuffer = 0;
460 stream->
name = strDup1((UBYTE *)
"pipe",
"pipe");
461 stream->prevline = stream->linenumber = 1;
466 #ifdef WITHEXTERNALCHANNEL 467 case EXTERNALCHANNELSTREAM:
470 if( (n=getCurrentExternalChannel()) == 0 )
471 Error0(
"@No current extrenal channel");
472 stream = CreateStream((UBYTE *)
"externalchannel");
473 stream->handle = CreateHandle();
474 tmpn = (
int *)Malloc1(
sizeof(
int),
"external channel handle");
476 RWLOCKW(AM.handlelock);
477 filelist[stream->handle] = (FILES *)tmpn;
478 UNRWLOCK(AM.handlelock);
481 stream->inbuffer = 0;
482 stream->
name = strDup1((UBYTE *)
"externalchannel",
"externalchannel");
483 stream->prevline = stream->linenumber = 1;
491 stream->bufferposition = 0;
492 stream->isnextchar = 0;
494 stream->previousNoShowInput = AC.NoShowInput;
495 stream->afterwards = raiselow;
496 if ( AC.CurrentStream ) stream->previous = AC.CurrentStream - AC.Streams;
497 else stream->previous = -1;
499 if ( prevarmode == 0 ) stream->prevars = -1;
500 else if ( prevarmode > 0 ) stream->prevars = NumPre;
501 else if ( prevarmode < 0 ) stream->prevars = -prevarmode-1;
502 AC.CurrentStream = stream;
503 if ( type == PREREADSTREAM || type == PREREADSTREAM3 || type == PRECALCSTREAM
504 || type == DOLLARSTREAM ) AC.NoShowInput = 1;
513 int LocateFile(UBYTE **name,
int type)
515 int handle, namesize, i;
516 UBYTE *s, *to, *u1, *u2, *newname, *indir;
517 handle = OpenFile((
char *)(*name));
518 if ( handle >= 0 )
return(handle);
519 if ( type == SETUPFILE && AM.SetupFile ) {
520 handle = OpenFile((
char *)(AM.SetupFile));
521 if ( handle >= 0 )
return(handle);
522 MesPrint(
"Could not open setup file %s",(
char *)(AM.SetupFile));
524 namesize = 4; s = *name;
525 while ( *s ) { s++; namesize++; }
526 if ( type == SETUPFILE ) indir = AM.SetupDir;
527 else indir = AM.IncDir;
531 while ( *s ) { s++; i++; }
532 newname = (UBYTE *)Malloc1(namesize+i,
"LocateFile");
533 s = indir; to = newname;
534 while ( *s ) *to++ = *s++;
535 if ( to > newname && to[-1] != SEPARATOR ) *to++ = SEPARATOR;
537 while ( *s ) *to++ = *s++;
539 handle = OpenFile((
char *)newname);
544 M_free(newname,
"LocateFile, incdir/file");
546 if ( type == SETUPFILE ) {
547 handle = OpenFile(setupfilename);
548 if ( handle >= 0 )
return(handle);
549 s = (UBYTE *)getenv(
"FORMSETUP");
551 handle = OpenFile((
char *)s);
552 if ( handle >= 0 )
return(handle);
553 MesPrint(
"Could not open setup file %s",s);
556 if ( type != SETUPFILE && AM.Path ) {
561 while ( *u1 && *u1 !=
';' ) {
565 while ( *u1 && *u1 !=
':' ) {
566 if ( *u1 ==
'\\' ) u1++;
570 newname = (UBYTE *)Malloc1(namesize+i,
"LocateFile");
571 s = u2; to = newname;
574 if ( *s ==
'\\' ) s++;
578 if ( to > newname && to[-1] != SEPARATOR ) *to++ = SEPARATOR;
580 while ( *s ) *to++ = *s++;
582 handle = OpenFile((
char *)newname);
587 M_free(newname,
"LocateFile Path/file");
591 if ( type != SETUPFILE ) Error1(
"LocateFile: Cannot find file",*name);
602 int newstr = stream->previous, sgn;
603 UBYTE *t, numbuf[24];
606 M_free(stream->
FoldName,
"stream->FoldName");
609 if ( stream->type == FILESTREAM || stream->type == REVERSEFILESTREAM ) {
610 CloseFile(stream->handle);
611 if ( stream->
buffer != 0 ) M_free(stream->
buffer,
"name of input stream");
615 else if ( stream->type == PIPESTREAM ) {
616 RWLOCKW(AM.handlelock);
618 if ( PF.me == MASTER )
620 pclose((FILE *)(filelist[stream->handle]));
621 filelist[stream->handle] = 0;
623 UNRWLOCK(AM.handlelock);
625 if ( stream->
buffer != 0 ) {
626 M_free(stream->
buffer,
"pipe buffer");
633 #ifdef WITHEXTERNALCHANNEL 634 else if ( stream->type == EXTERNALCHANNELSTREAM ) {
636 RWLOCKW(AM.handlelock);
637 tmpn = (
int *)(filelist[stream->handle]);
638 filelist[stream->handle] = 0;
640 UNRWLOCK(AM.handlelock);
641 M_free(tmpn,
"external channel handle");
645 else if ( stream->type == PREVARSTREAM && (
646 stream->afterwards == PRERAISEAFTER || stream->afterwards == PRELOWERAFTER ) ) {
647 t = stream->
buffer; x = 0; sgn = 1;
648 while ( *t ==
'-' || *t ==
'+' ) {
649 if ( *t ==
'-' ) sgn = -sgn;
652 if ( FG.cTable[*t] == 1 ) {
653 while ( *t && FG.cTable[*t] == 1 ) x = 10*x + *t++ -
'0';
655 if ( stream->afterwards == PRERAISEAFTER ) x = sgn*x + 1;
662 else if ( stream->type == DOLLARSTREAM && (
663 stream->afterwards == PRERAISEAFTER || stream->afterwards == PRELOWERAFTER ) ) {
664 if ( stream->afterwards == PRERAISEAFTER ) x = 1;
666 DollarRaiseLow(stream->
pname,x);
668 else if ( stream->type == PRECALCSTREAM || stream->type == DOLLARSTREAM ) {
669 if ( stream->
buffer ) M_free(stream->
buffer,
"stream->buffer");
672 if ( stream->
name && stream->type != PREVARSTREAM
673 && stream->type != PREREADSTREAM && stream->type != PREREADSTREAM2 && stream->type != PREREADSTREAM3
674 && stream->type != PRECALCSTREAM && stream->type != DOLLARSTREAM ) {
675 M_free(stream->
name,
"stream->name");
679 AC.NoShowInput = stream->previousNoShowInput;
685 if ( stream->prevars >= 0 ) {
686 while ( NumPre > stream->prevars ) {
688 M_free(PreVar[NumPre].name,
"PreVar[NumPre].name");
689 PreVar[NumPre].name = PreVar[NumPre].value = 0;
692 if ( stream->type == PREVARSTREAM ) {
693 AP.AllowDelay = stream->olddelay;
694 ClearMacro(stream->
pname);
695 M_free(stream->
pname,
"stream->pname");
697 else if ( stream->type == DOLLARSTREAM ) {
698 M_free(stream->
pname,
"stream->pname");
701 if ( newstr >= 0 )
return(AC.Streams + newstr);
710 STREAM *CreateStream(UBYTE *where)
715 if ( AC.NumStreams >= AC.MaxNumStreams ) {
716 if ( AC.MaxNumStreams == 0 ) numnewstreams = 10;
717 else numnewstreams = 2*AC.MaxNumStreams;
718 newstreams = (
STREAM *)Malloc1(
sizeof(
STREAM)*(numnewstreams+1),
"CreateStream");
719 if ( AC.MaxNumStreams > 0 ) {
720 offset = AC.CurrentStream - AC.Streams;
721 for ( i = 0; i < AC.MaxNumStreams; i++ ) {
722 newstreams[i] = AC.Streams[i];
724 AC.CurrentStream = newstreams + offset;
726 else newstreams[0].previous = -1;
727 AC.MaxNumStreams = numnewstreams;
728 if ( AC.Streams ) M_free(AC.Streams,(
char *)where);
729 AC.Streams = newstreams;
731 newstreams = AC.Streams+AC.NumStreams++;
732 newstreams->
name = 0;
741 LONG GetStreamPosition(
STREAM *stream)
743 return(stream->bufferposition + ((LONG)stream->
pointer-(LONG)stream->
buffer));
751 VOID PositionStream(
STREAM *stream, LONG position)
754 if ( position >= stream->bufferposition
755 && position < stream->bufferposition + stream->inbuffer ) {
756 stream->
pointer = stream->
buffer + (position-stream->bufferposition);
758 else if ( stream->type == FILESTREAM ) {
759 SETBASEPOSITION(scrpos,position);
760 SeekFile(stream->handle,&scrpos,SEEK_SET);
761 stream->inbuffer = ReadFile(stream->handle,stream->
buffer,stream->buffersize);
763 stream->
top = stream->
buffer + stream->inbuffer;
764 stream->bufferposition = position;
765 stream->fileposition = position + stream->inbuffer;
766 stream->isnextchar = 0;
769 Error0(
"Illegal position for stream");
783 int ReverseStatements(
STREAM *stream)
785 UBYTE *spare = (UBYTE *)Malloc1((stream->inbuffer+1)*
sizeof(UBYTE),
"Reverse copy");
786 UBYTE *top = stream->
buffer + stream->inbuffer, *in, *s, *ss, *out;
787 out = spare+stream->inbuffer+1;
791 if ( *s == AP.ComChar ) {
794 if ( s == top ) { *--out =
'\n';
break; }
798 irrend: MesPrint(
"@Irregular end of reverse include file.");
802 else if ( *s ==
'\n' ) {
804 while ( ss > in ) *--out = *--ss;
806 if ( out[0] == AP.ComChar && ss+6 < s && out[3] ==
'#' ) {
810 if ( out[4] ==
'[' ) out[4] =
']';
811 else if ( out[4] ==
']' ) out[4] =
'[';
819 while ( s < top && ( *s ==
' ' || *s ==
'\t' ) ) s++;
837 while ( s < top && ( *s ==
' ' || *s ==
'\t' ) ) s++;
838 while ( s < top && *s ==
'\n' ) s++;
839 if ( s >= top && s[-1] !=
'\n' ) *s++ =
'\n';
841 while ( ss > in ) *--out = *--ss;
845 else if ( *s ==
'"' ) {
848 if ( *s ==
'"' )
break;
849 if ( *s ==
'\\' ) { s++; }
852 if ( s >= top )
goto irrend;
854 else if ( *s ==
'\\' ) {
856 if ( s >= top )
goto irrend;
861 if ( s >= top && s[-1] !=
'\n' ) *s++ =
'\n';
863 while ( ss > in ) *--out = *--ss;
867 if ( out == spare ) stream->inbuffer++;
868 if ( out > spare+1 ) {
869 MesPrint(
"@Internal error in #reverseinclude instruction.");
872 memcpy((
void *)(stream->
buffer),(
void *)out,(
size_t)(stream->inbuffer*
sizeof(UBYTE)));
873 M_free(spare,
"Reverse copy");
886 int i = CreateHandle();
887 filelist[i] = Ustdout;
892 AR.Fscr[0].handle = -1;
893 AR.Fscr[1].handle = -1;
894 AR.Fscr[2].handle = -1;
895 AR.FoStage4[0].handle = -1;
896 AR.FoStage4[1].handle = -1;
897 AR.infile = &(AR.Fscr[0]);
898 AR.outfile = &(AR.Fscr[1]);
899 AR.hidefile = &(AR.Fscr[2]);
900 AR.StoreData.Handle = -1;
903 AC.MaxNumStreams = 0;
911 int OpenFile(
char *name)
916 if ( ( f = Uopen(name,
"rb") ) == 0 )
return(-1);
919 RWLOCKW(AM.handlelock);
921 UNRWLOCK(AM.handlelock);
930 int OpenAddFile(
char *name)
935 if ( ( f = Uopen(name,
"a+b") ) == 0 )
return(-1);
938 RWLOCKW(AM.handlelock);
940 UNRWLOCK(AM.handlelock);
942 SeekFile(i,&scrpos,SEEK_SET);
951 int ReOpenFile(
char *name)
956 if ( ( f = Uopen(name,
"r+b") ) == 0 )
return(-1);
958 RWLOCKW(AM.handlelock);
960 UNRWLOCK(AM.handlelock);
962 SeekFile(i,&scrpos,SEEK_SET);
971 int CreateFile(
char *name)
975 if ( ( f = Uopen(name,
"w+b") ) == 0 )
return(-1);
977 RWLOCKW(AM.handlelock);
979 UNRWLOCK(AM.handlelock);
988 int CreateLogFile(
char *name)
992 if ( ( f = Uopen(name,
"w+b") ) == 0 )
return(-1);
995 RWLOCKW(AM.handlelock);
997 UNRWLOCK(AM.handlelock);
1006 VOID CloseFile(
int handle)
1008 if ( handle >= 0 ) {
1010 RWLOCKW(AM.handlelock);
1011 f = filelist[handle];
1012 filelist[handle] = 0;
1014 UNRWLOCK(AM.handlelock);
1031 #define COPYFILEBUFSIZE 40960L 1033 size_t countin, countout, sumcount;
1034 char *buffer = NULL;
1036 sumcount = (AM.S0->LargeSize+AM.S0->SmallEsize)*
sizeof(WORD);
1037 if ( sumcount <= COPYFILEBUFSIZE ) {
1038 sumcount = COPYFILEBUFSIZE;
1039 buffer = (
char*)Malloc1(sumcount,
"file copy buffer");
1042 buffer = (
char *)(AM.S0->lBuffer);
1045 in = fopen(source,
"rb");
1047 perror(
"CopyFile: ");
1050 out = fopen(dest,
"wb");
1051 if ( out == NULL ) {
1052 perror(
"CopyFile: ");
1056 while ( !feof(in) ) {
1057 countin = fread(buffer, 1, sumcount, in);
1058 if ( countin != sumcount ) {
1060 perror(
"CopyFile: ");
1064 countout = fwrite(buffer, 1, countin, out);
1065 if ( countin != countout ) {
1066 perror(
"CopyFile: ");
1073 if ( sumcount <= COPYFILEBUFSIZE ) {
1074 M_free(buffer,
"file copy buffer");
1094 RWLOCKW(AM.handlelock);
1096 if ( filelistsize == 0 ) {
1098 filelist = (FILES **)Malloc1(
sizeof(FILES *)*filelistsize,
"file handle");
1099 for ( j = 0; j < filelistsize; j++ ) filelist[j] = 0;
1103 else if ( numinfilelist >= filelistsize ) {
1104 VOID **fl = (VOID **)filelist;
1106 if ( DoubleList((VOID ***)(&fl),&filelistsize,(
int)
sizeof(FILES *),
1107 "list of open files") != 0 ) Terminate(-1);
1108 filelist = (FILES **)fl;
1109 for ( j = i; j < filelistsize; j++ ) filelist[j] = 0;
1110 numinfilelist = i + 1;
1114 for ( j = 0; j < filelistsize; j++ ) {
1115 if ( filelist[j] == 0 ) { i = j;
break; }
1119 filelist[i] = (FILES *)(filelist);
1125 if ( numinfilelist > MAX_OPEN_FILES ) {
1127 UNRWLOCK(AM.handlelock);
1129 MesPrint(
"More than %d open files",MAX_OPEN_FILES);
1130 Error0(
"System limit. This limit is not due to FORM!");
1134 UNRWLOCK(AM.handlelock);
1145 LONG ReadFile(
int handle, UBYTE *buffer, LONG size)
1152 RWLOCKR(AM.handlelock);
1153 f = filelist[handle];
1154 UNRWLOCK(AM.handlelock);
1158 r = Uread(b,1,size,f);
1159 if ( r < 0 )
return(r);
1160 if ( r == 0 )
return(inbuf);
1162 if ( r == size )
return(inbuf);
1163 if ( r > size )
return(-1);
1199 WORD *b = (WORD *)buffer, *t;
1201 if ( fi->handle < 0 ) {
1202 fi->POfill = (WORD *)((UBYTE *)(fi->PObuffer) + BASEPOSITION(*pos));
1204 while ( size > 0 && fi->POfill < fi->POfull ) { *b++ = *t++; size--; }
1207 if ( ISLESSPOS(*pos,fi->POposition) || ISGEPOSINC(*pos,fi->POposition,
1208 ((UBYTE *)(fi->POfull)-(UBYTE *)(fi->PObuffer))) ) {
1213 fi->POposition = *pos;
1214 LOCK(AS.inputslock);
1215 SeekFile(fi->handle,pos,SEEK_SET);
1216 retval = ReadFile(fi->handle,(UBYTE *)(fi->PObuffer),fi->POsize);
1217 UNLOCK(AS.inputslock);
1218 fi->POfull = fi->PObuffer+retval/
sizeof(WORD);
1219 fi->POfill = fi->PObuffer;
1220 if ( fi != AR.hidefile ) AR.InInBuf = retval/
sizeof(WORD);
1221 else AR.InHiBuf = retval/
sizeof(WORD);
1224 fi->POfill = (WORD *)((UBYTE *)(fi->PObuffer) + DIFBASE(*pos,fi->POposition));
1226 if ( fi->POfill + size <= fi->POfull ) {
1228 while ( size > 0 ) { *b++ = *t++; size--; }
1232 i = fi->POfull - fi->POfill; t = fi->POfill;
1233 if ( i > size ) i = size;
1235 while ( --i >= 0 ) *b++ = *t++;
1236 if ( size == 0 )
break;
1237 ADDPOS(fi->POposition,(UBYTE *)(fi->POfull)-(UBYTE *)(fi->PObuffer));
1238 LOCK(AS.inputslock);
1239 SeekFile(fi->handle,&(fi->POposition),SEEK_SET);
1240 retval = ReadFile(fi->handle,(UBYTE *)(fi->PObuffer),fi->POsize);
1241 UNLOCK(AS.inputslock);
1242 fi->POfull = fi->PObuffer+retval/
sizeof(WORD);
1243 fi->POfill = fi->PObuffer;
1244 if ( fi != AR.hidefile ) AR.InInBuf = retval/
sizeof(WORD);
1245 else AR.InHiBuf = retval/
sizeof(WORD);
1246 if ( retval == 0 ) { t = fi->POfill;
break; }
1250 retval = (UBYTE *)b - buffer;
1252 ADDPOS(*pos,retval);
1261 LONG WriteFileToFile(
int handle, UBYTE *buffer, LONG size)
1264 LONG retval, totalwritten = 0, stilltowrite;
1265 RWLOCKR(AM.handlelock);
1266 f = filelist[handle];
1267 UNRWLOCK(AM.handlelock);
1268 while ( totalwritten < size ) {
1269 stilltowrite = size - totalwritten;
1273 retval = Uwrite((
char *)buffer+totalwritten,1,stilltowrite,f);
1274 if ( retval < 0 )
return(retval);
1275 if ( retval == 0 )
return(totalwritten);
1276 totalwritten += retval;
1281 return(totalwritten);
1285 WRITEFILE WriteFile = &WriteFileToFile;
1299 VOID SeekFile(
int handle,
POSITION *offset,
int origin)
1302 RWLOCKR(AM.handlelock);
1303 f = filelist[handle];
1304 UNRWLOCK(AM.handlelock);
1308 if ( origin == SEEK_SET ) {
1309 Useek(f,BASEPOSITION(*offset),origin);
1310 SETBASEPOSITION(*offset,(Utell(f)));
1313 else if ( origin == SEEK_END ) {
1316 SETBASEPOSITION(*offset,(Utell(f)));
1324 LONG TellFile(
int handle)
1327 TELLFILE(handle,&pos);
1331 return(BASEPOSITION(pos));
1334 VOID TELLFILE(
int handle,
POSITION *position)
1337 RWLOCKR(AM.handlelock);
1338 f = filelist[handle];
1339 UNRWLOCK(AM.handlelock);
1340 SETBASEPOSITION(*position,(Utell(f)));
1348 void FlushFile(
int handle)
1351 RWLOCKR(AM.handlelock);
1352 f = filelist[handle];
1353 UNRWLOCK(AM.handlelock);
1362 int GetPosFile(
int handle, fpos_t *pospointer)
1365 RWLOCKR(AM.handlelock);
1366 f = filelist[handle];
1367 UNRWLOCK(AM.handlelock);
1368 return(Ugetpos(f,pospointer));
1376 int SetPosFile(
int handle, fpos_t *pospointer)
1379 RWLOCKR(AM.handlelock);
1380 f = filelist[handle];
1381 UNRWLOCK(AM.handlelock);
1382 return(Usetpos(f,(fpos_t *)pospointer));
1396 VOID SynchFile(
int handle)
1399 if ( handle >= 0 ) {
1400 RWLOCKR(AM.handlelock);
1401 f = filelist[handle];
1402 UNRWLOCK(AM.handlelock);
1418 VOID TruncateFile(
int handle)
1421 if ( handle >= 0 ) {
1422 RWLOCKR(AM.handlelock);
1423 f = filelist[handle];
1424 UNRWLOCK(AM.handlelock);
1437 int GetChannel(
char *name)
1442 for ( i = 0; i < NumOutputChannels; i++ ) {
1443 if ( channels[i].name == 0 )
continue;
1444 if ( StrCmp((UBYTE *)name,(UBYTE *)(channels[i].name)) == 0 )
return(channels[i].handle);
1446 for ( i = 0; i < NumOutputChannels; i++ ) {
1447 if ( channels[i].name == 0 )
break;
1449 if ( i < NumOutputChannels ) { ch = &(channels[i]); }
1450 else { ch = (
CHANNEL *)FromList(&AC.ChannelList); }
1451 ch->
name = (
char *)strDup1((UBYTE *)name,
"name of channel");
1452 ch->
handle = CreateFile(name);
1453 RWLOCKR(AM.handlelock);
1454 f = filelist[ch->
handle];
1455 UNRWLOCK(AM.handlelock);
1468 int GetAppendChannel(
char *name)
1473 for ( i = 0; i < NumOutputChannels; i++ ) {
1474 if ( channels[i].name == 0 )
continue;
1475 if ( StrCmp((UBYTE *)name,(UBYTE *)(channels[i].name)) == 0 )
return(channels[i].handle);
1477 for ( i = 0; i < NumOutputChannels; i++ ) {
1478 if ( channels[i].name == 0 )
break;
1480 if ( i < NumOutputChannels ) { ch = &(channels[i]); }
1481 else { ch = (
CHANNEL *)FromList(&AC.ChannelList); }
1482 ch->
name = (
char *)strDup1((UBYTE *)name,
"name of channel");
1483 ch->
handle = OpenAddFile(name);
1484 RWLOCKR(AM.handlelock);
1485 f = filelist[ch->
handle];
1486 UNRWLOCK(AM.handlelock);
1498 int CloseChannel(
char *name)
1501 for ( i = 0; i < NumOutputChannels; i++ ) {
1502 if ( channels[i].name == 0 )
continue;
1503 if ( channels[i].name[0] == 0 )
continue;
1504 if ( StrCmp((UBYTE *)name,(UBYTE *)(channels[i].name)) == 0 ) {
1505 CloseFile(channels[i].handle);
1506 M_free(channels[i].name,
"CloseChannel");
1507 channels[i].name = 0;
1532 void UpdateMaxSize()
1540 if ( PF.me != MASTER )
return;
1543 if ( AM.PrintTotalSize ) {
1548 scr = AB[0]->R.Fscr;
1552 for ( i = 0; i <=2; i++ ) {
1553 if ( scr[i].handle < 0 ) {
1554 SETBASEPOSITION(position,(scr[i].POfull-scr[i].PObuffer)*
sizeof(WORD));
1557 position = scr[i].filesize;
1559 ADD2POS(sumsize,position);
1568 for ( j = 0; j < AM.totalnumberofthreads; j++ ) {
1570 if ( AT.SS && AT.SS->file.handle >= 0 ) {
1571 position = AT.SS->file.filesize;
1577 ADD2POS(sumsize,position);
1579 if ( AR.FoStage4[0].handle >= 0 ) {
1580 position = AR.FoStage4[0].filesize;
1581 ADD2POS(sumsize,position);
1586 if ( AT.SS && AT.SS->file.handle >= 0 ) {
1587 position = AT.SS->file.filesize;
1588 ADD2POS(sumsize,position);
1590 if ( AR.FoStage4[0].handle >= 0 ) {
1591 position = AR.FoStage4[0].filesize;
1592 ADD2POS(sumsize,position);
1598 ADD2POS(sumsize,AC.StoreFileSize);
1602 if ( ISLESSPOS(AS.MaxExprSize,sumsize) ) {
1604 LOCK(AS.MaxExprSizeLock);
1605 if ( ISLESSPOS(AS.MaxExprSize,sumsize) ) AS.MaxExprSize = sumsize;
1606 UNLOCK(AS.MaxExprSizeLock);
1608 AS.MaxExprSize = sumsize;
1622 int StrCmp(UBYTE *s1, UBYTE *s2)
1624 while ( *s1 && *s1 == *s2 ) { s1++; s2++; }
1625 return((
int)*s1-(
int)*s2);
1633 int StrICmp(UBYTE *s1, UBYTE *s2)
1635 while ( *s1 && tolower(*s1) == tolower(*s2) ) { s1++; s2++; }
1636 return((
int)tolower(*s1)-(
int)tolower(*s2));
1644 int StrHICmp(UBYTE *s1, UBYTE *s2)
1646 while ( *s1 && tolower(*s1) == *s2 ) { s1++; s2++; }
1647 return((
int)tolower(*s1)-(
int)(*s2));
1655 int StrICont(UBYTE *s1, UBYTE *s2)
1657 while ( *s1 && tolower(*s1) == tolower(*s2) ) { s1++; s2++; }
1658 if ( *s1 == 0 )
return(0);
1659 return((
int)tolower(*s1)-(
int)tolower(*s2));
1667 int ConWord(UBYTE *s1, UBYTE *s2)
1669 while ( *s1 && ( tolower(*s1) == tolower(*s2) ) ) { s1++; s2++; }
1670 if ( *s1 == 0 )
return(1);
1679 int StrLen(UBYTE *s)
1682 while ( *s ) { s++; i++; }
1691 VOID NumToStr(UBYTE *s, LONG x)
1696 if ( x < 0 ) { *s++ =
'-'; xx = -x; }
1699 *t++ = xx % 10 +
'0';
1702 while ( t > str ) *s++ = *--t;
1715 VOID WriteString(
int type, UBYTE *str,
int num)
1719 if ( num > 0 && str[num-1] == 0 ) { num--; }
1720 else if ( num <= 0 || str[num-1] != LINEFEED ) {
1721 AddLineFeed(str,num);
1724 if(type == EXTERNALCHANNELOUT){
1725 if(WriteFile(0,str,num) != num) error = 1;
1728 if ( AM.silent == 0 || type == ERROROUT ) {
1729 if ( type == INPUTOUT ) {
1730 if ( !AM.FileOnlyFlag && WriteFile(AM.StdOut,(UBYTE *)
" ",4) != 4 ) error = 1;
1731 if ( AC.LogHandle >= 0 && WriteFile(AC.LogHandle,(UBYTE *)
" ",4) != 4 ) error = 1;
1733 if ( !AM.FileOnlyFlag && WriteFile(AM.StdOut,str,num) != num ) error = 1;
1734 if ( AC.LogHandle >= 0 && WriteFile(AC.LogHandle,str,num) != num ) error = 1;
1736 if ( error ) Terminate(-1);
1749 VOID WriteUnfinString(
int type, UBYTE *str,
int num)
1754 if(type == EXTERNALCHANNELOUT){
1755 if(WriteFile(0,str,num) != num) error = 1;
1758 if ( AM.silent == 0 || type == ERROROUT ) {
1759 if ( type == INPUTOUT ) {
1760 if ( !AM.FileOnlyFlag && WriteFile(AM.StdOut,(UBYTE *)
" ",4) != 4 ) error = 1;
1761 if ( AC.LogHandle >= 0 && WriteFile(AC.LogHandle,(UBYTE *)
" ",4) != 4 ) error = 1;
1763 if ( !AM.FileOnlyFlag && WriteFile(AM.StdOut,str,num) != num ) error = 1;
1764 if ( AC.LogHandle >= 0 && WriteFile(AC.LogHandle,str,num) != num ) error = 1;
1766 if ( error ) Terminate(-1);
1774 UBYTE *AddToString(UBYTE *outstring, UBYTE *extrastring,
int par)
1776 UBYTE *s = extrastring, *t, *newstring;
1778 while ( *s ) { s++; }
1780 if ( outstring == 0 ) {
1782 t = outstring = (UBYTE *)Malloc1(n+1,
"AddToString");
1791 t = newstring = (UBYTE *)Malloc1(n+nn+2,
"AddToString");
1794 if ( par == 1 ) *t++ =
',';
1798 M_free(outstring,
"AddToString");
1812 UBYTE *strDup1(UBYTE *instring,
char *ifwrong)
1814 UBYTE *s = instring, *to;
1816 to = s = (UBYTE *)Malloc1((s-instring)+1,ifwrong);
1817 while ( *instring ) *to++ = *instring++;
1827 UBYTE *EndOfToken(UBYTE *s)
1830 while ( ( c = (UBYTE)(FG.cTable[*s]) ) == 0 || c == 1 ) s++;
1839 UBYTE *ToToken(UBYTE *s)
1842 while ( *s && ( c = (UBYTE)(FG.cTable[*s]) ) != 0 && c != 1 ) s++;
1854 UBYTE *SkipField(UBYTE *s,
int level)
1857 if ( *s ==
',' && level == 0 )
return(s);
1858 if ( *s ==
'(' ) level++;
1859 else if ( *s ==
')' ) { level--;
if ( level < 0 ) level = 0; }
1860 else if ( *s ==
'[' ) {
1863 else if ( *s ==
'{' ) {
1881 WORD ReadSnum(UBYTE **p)
1886 if ( FG.cTable[*s] == 1 ) {
1888 x = ( x << 3 ) + ( x << 1 ) + ( *s++ -
'0' );
1889 if ( x > MAXPOSITIVE )
return(-1);
1890 }
while ( FG.cTable[*s] == 1 );
1905 UBYTE *NumCopy(WORD y, UBYTE *to)
1918 do { *s++ = (UBYTE)((x % 10)+
'0'); i++; }
while ( ( x /= 10 ) != 0 );
1922 i = to[j]; to[j] = s[-j]; s[-j] = (UBYTE)i; j--;
1935 char *LongCopy(LONG y,
char *to)
1948 do { *s++ = (x % 10)+
'0'; i++; }
while ( ( x /= 10 ) != 0 );
1952 i = to[j]; to[j] = s[-j]; s[-j] = (char)i; j--;
1965 char *LongLongCopy(off_t *y,
char *to)
1978 if ( x < 0 ) { x = -x; *to++ =
'-'; }
1980 do { *s++ = (x % 10)+
'0'; i++; }
while ( ( x /= 10 ) != 0 );
1984 i = to[j]; to[j] = s[-j]; s[-j] = (char)i; j--;
2000 static char notime[] =
"";
2009 return((UBYTE *)ctime(&tp));
2014 return((UBYTE *)ctime(&tp));
2016 return((UBYTE *)notime);
2030 case 0:
return(set->bit_0);
2031 case 1:
return(set->bit_1);
2032 case 2:
return(set->bit_2);
2033 case 3:
return(set->bit_3);
2034 case 4:
return(set->bit_4);
2035 case 5:
return(set->bit_5);
2036 case 6:
return(set->bit_6);
2037 case 7:
return(set->bit_7);
2051 case 0:
set->bit_0=1;
break;
2052 case 1:
set->bit_1=1;
break;
2053 case 2:
set->bit_2=1;
break;
2054 case 3:
set->bit_3=1;
break;
2055 case 4:
set->bit_4=1;
break;
2056 case 5:
set->bit_5=1;
break;
2057 case 6:
set->bit_6=1;
break;
2058 case 7:
set->bit_7=1;
break;
2072 case 0:
set->bit_0=0;
break;
2073 case 1:
set->bit_1=0;
break;
2074 case 2:
set->bit_2=0;
break;
2075 case 3:
set->bit_3=0;
break;
2076 case 4:
set->bit_4=0;
break;
2077 case 5:
set->bit_5=0;
break;
2078 case 6:
set->bit_6=0;
break;
2079 case 7:
set->bit_7=0;
break;
2096 case 0:
set->bit_0=(set1->bit_0&&(!set2->bit_0));
break;
2097 case 1:
set->bit_1=(set1->bit_1&&(!set2->bit_1));
break;
2098 case 2:
set->bit_2=(set1->bit_2&&(!set2->bit_2));
break;
2099 case 3:
set->bit_3=(set1->bit_3&&(!set2->bit_3));
break;
2100 case 4:
set->bit_4=(set1->bit_4&&(!set2->bit_4));
break;
2101 case 5:
set->bit_5=(set1->bit_5&&(!set2->bit_5));
break;
2102 case 6:
set->bit_6=(set1->bit_6&&(!set2->bit_6));
break;
2103 case 7:
set->bit_7=(set1->bit_7&&(!set2->bit_7));
break;
2104 case 8:
set++;set1++;set2++;
2117 #ifdef MALLOCPROTECT 2118 if ( mprotectInit() ) exit(0);
2131 char *dummymessage =
"Malloc";
2132 INILOCK(MallocLock);
2135 VOID *Malloc(LONG size)
2144 MesPrint(
"Asking for 0 bytes in Malloc");
2147 if ( ( size & 7 ) != 0 ) { size = size - ( size&7 ) + 8; }
2151 mem = (VOID *)M_alloc(size);
2154 MLOCK(ErrorMessageLock);
2156 Error0(
"No memory!");
2158 MUNLOCK(ErrorMessageLock);
2168 mallocsizes[nummalloclist] = size;
2169 mallocstrings[nummalloclist] = dummymessage;
2170 malloclist[nummalloclist++] = mem;
2171 if ( filelist ) MesPrint(
"Mem0 at 0x%x, %l bytes",mem,size);
2173 int i = nummalloclist-1;
2174 while ( --i >= 0 ) {
2175 if ( (
char *)mem < (((
char *)malloclist[i]) + mallocsizes[i])
2176 && (
char *)(malloclist[i]) < ((
char *)mem + size) ) {
2177 if ( filelist ) MesPrint(
"This memory overlaps with the block at 0x%x" 2184 for ( i = 0; i < (int)BANNER; i++ ) { *t++ = FILLVALUE; *--u = FILLVALUE; }
2187 int j = nummalloclist-1, i;
2188 while ( --j >= 0 ) {
2189 t = (
char *)(malloclist[j]);
2190 u = t + mallocsizes[j];
2191 for ( i = 0; i < (int)BANNER; i++ ) {
2193 if ( *t != FILLVALUE || *u != FILLVALUE ) {
2194 MesPrint(
"Writing outside memory for %s",malloclist[i]);
2217 VOID *Malloc1(LONG size,
const char *messageifwrong)
2226 MesPrint(
"%wAsking for 0 bytes in Malloc1");
2232 if ( ( size & 7 ) != 0 ) { size = size - ( size&7 ) + 8; }
2236 mem = (VOID *)M_alloc(size);
2239 MLOCK(ErrorMessageLock);
2241 Error1(
"No memory while allocating ",(UBYTE *)messageifwrong);
2243 MUNLOCK(ErrorMessageLock);
2253 mallocsizes[nummalloclist] = size;
2254 mallocstrings[nummalloclist] = (
char *)messageifwrong;
2255 malloclist[nummalloclist++] = mem;
2256 if ( AC.MemDebugFlag && filelist ) MesPrint(
"%wMem1 at 0x%x: %l bytes. %s",mem,size,messageifwrong);
2258 int i = nummalloclist-1;
2259 while ( --i >= 0 ) {
2260 if ( (
char *)mem < (((
char *)malloclist[i]) + mallocsizes[i])
2261 && (
char *)(malloclist[i]) < ((
char *)mem + size) ) {
2262 if ( filelist ) MesPrint(
"This memory overlaps with the block at 0x%x" 2268 #ifdef MALLOCDEBUGOUTPUT 2269 printf (
"Malloc1: %s, allocated %li bytes at %.8lx\n",messageifwrong,size,(
unsigned long)mem);
2275 for ( i = 0; i < (int)BANNER; i++ ) { *t++ = FILLVALUE; *--u = FILLVALUE; }
2296 void M_free(VOID *x,
const char *where)
2299 char *t = (
char *)x;
2302 x = (
void *)(((
char *)x)-BANNER);
2304 if ( AC.MemDebugFlag ) MesPrint(
"%wFreeing 0x%x: %s",x,where);
2306 for ( i = nummalloclist-1; i >= 0; i-- ) {
2307 if ( x == malloclist[i] ) {
2308 size = mallocsizes[i];
2309 for ( j = i+1; j < nummalloclist; j++ ) {
2310 malloclist[j-1] = malloclist[j];
2311 mallocsizes[j-1] = mallocsizes[j];
2312 mallocstrings[j-1] = mallocstrings[j];
2319 unsigned int xx = ((ULONG)x);
2320 printf(
"Error returning non-allocated address: 0x%x from %s\n" 2327 for ( k = 0, j = 0; k < (int)BANNER; k++ ) {
2328 if ( *--t != FILLVALUE ) j++;
2331 LONG *tt = (LONG *)x;
2332 MesPrint(
"%w!!!!! Banner has been written in !!!!!: %x %x %x %x",
2333 tt[0],tt[1],tt[2],tt[3]);
2336 for ( k = 0, j = 0; k < (int)BANNER; k++ ) {
2337 if ( *--t != FILLVALUE ) j++;
2340 LONG *tt = (LONG *)x;
2341 MesPrint(
"%w!!!!! Tail has been written in !!!!!: %x %x %x %x",
2342 tt[0],tt[1],tt[2],tt[3]);
2355 #ifdef MALLOCDEBUGOUTPUT 2356 printf (
"M_free: %s, memory freed at %.8lx\n",where,(
unsigned long)x);
2360 #ifdef MALLOCPROTECT 2361 mprotectFree((
void *)x);
2375 void M_check1() { MesPrint(
"Checking Malloc"); M_check(); }
2379 int i,j,k,error = 0;
2382 for ( i = 0; i < nummalloclist; i++ ) {
2383 t = (
char *)(malloclist[i]);
2384 for ( k = 0, j = 0; k < (int)BANNER; k++ ) {
2385 if ( *t++ != FILLVALUE ) j++;
2388 tt = (LONG *)(malloclist[i]);
2389 MesPrint(
"%w!!!!! Banner %d (%s) has been written in !!!!!: %x %x %x %x",
2390 i,mallocstrings[i],tt[0],tt[1],tt[2],tt[3]);
2391 tt[0] = tt[1] = tt[2] = tt[3] = 0;
2394 t = (
char *)(malloclist[i]) + mallocsizes[i];
2395 for ( k = 0, j = 0; k < (int)BANNER; k++ ) {
2396 if ( *--t != FILLVALUE ) j++;
2400 MesPrint(
"%w!!!!! Tail %d (%s) has been written in !!!!!: %x %x %x %x",
2401 i,mallocstrings[i],tt[0],tt[1],tt[2],tt[3]);
2402 tt[0] = tt[1] = tt[2] = tt[3] = 0;
2405 if ( ( mallocstrings[i][0] ==
' ' ) || ( mallocstrings[i][0] ==
'#' ) ) {
2406 MesPrint(
"%w!!!!! Funny mallocstring");
2421 MesPrint(
"We have the following memory allocations left:");
2422 for ( i = 0; i < nummalloclist; i++ ) {
2423 MesPrint(
"0x%x: %l bytes. number %d: '%s'",malloclist[i],mallocsizes[i],i,mallocstrings[i]);
2460 #define TERMMEMSTARTNUM 16 2461 #define TERMEXTRAWORDS 10 2463 VOID TermMallocAddMemory(PHEAD0)
2468 else extra = AT.TermMemMax;
2469 if ( AT.TermMemHeap ) M_free(AT.TermMemHeap,
"TermMalloc");
2470 newbufs = (WORD *)Malloc1(extra*(AM.MaxTer+TERMEXTRAWORDS*
sizeof(WORD)),
"TermMalloc");
2471 AT.TermMemHeap = (WORD **)Malloc1((extra+AT.TermMemMax)*
sizeof(WORD *),
"TermMalloc");
2472 for ( i = 0; i < extra; i++ ) {
2473 AT.TermMemHeap[i] = newbufs + i*(AM.MaxTer/
sizeof(WORD)+TERMEXTRAWORDS);
2475 #ifdef TERMMALLOCDEBUG 2476 DebugHeap2 = (WORD **)Malloc1((extra+AT.TermMemMax)*
sizeof(WORD *),
"TermMalloc");
2477 for ( i = 0; i < AT.TermMemMax; i++ ) { DebugHeap2[i] = DebugHeap1[i]; }
2478 for ( i = 0; i < extra; i++ ) {
2479 DebugHeap2[i+AT.TermMemMax] = newbufs + i*(AM.MaxTer/
sizeof(WORD)+TERMEXTRAWORDS);
2481 if ( DebugHeap1 ) M_free(DebugHeap1,
"TermMalloc");
2482 DebugHeap1 = DebugHeap2;
2484 AT.TermMemTop = extra;
2485 AT.TermMemMax += extra;
2486 #ifdef TERMMALLOCDEBUG 2487 MesPrint(
"AT.TermMemMax is now %l",AT.TermMemMax);
2491 #ifndef MEMORYMACROS 2493 WORD *TermMalloc2(
PHEAD char *text)
2495 if ( AT.TermMemTop <= 0 ) TermMallocAddMemory(BHEAD0);
2497 #ifdef TERMMALLOCDEBUG 2498 MesPrint(
"TermMalloc: %s, %d",text,(AT.TermMemMax-AT.TermMemTop));
2501 #ifdef MALLOCDEBUGOUTPUT 2502 MesPrint(
"TermMalloc: %s, %l/%l (%x)",text,AT.TermMemTop,AT.TermMemMax,AT.TermMemHeap[AT.TermMemTop-1]);
2506 return(AT.TermMemHeap[--AT.TermMemTop]);
2509 VOID TermFree2(
PHEAD WORD *TermMem,
char *text)
2511 #ifdef TERMMALLOCDEBUG 2515 for ( i = 0; i < AT.TermMemMax; i++ ) {
2516 if ( TermMem == DebugHeap1[i] )
break;
2518 if ( i >= AT.TermMemMax ) {
2519 MesPrint(
" ERROR: TermFree called with an address not given by TermMalloc.");
2524 AT.TermMemHeap[AT.TermMemTop++] = TermMem;
2526 #ifdef TERMMALLOCDEBUG 2527 MesPrint(
"TermFree: %s, %d",text,(AT.TermMemMax-AT.TermMemTop));
2529 #ifdef MALLOCDEBUGOUTPUT 2530 MesPrint(
"TermFree: %s, %l/%l (%x)",text,AT.TermMemTop,AT.TermMemMax,TermMem);
2560 #define NUMBERMEMSTARTNUM 16 2561 #define NUMBEREXTRAWORDS 10L 2563 #ifdef TERMMALLOCDEBUG 2564 UWORD **DebugHeap3, **DebugHeap4;
2567 VOID NumberMallocAddMemory(PHEAD0)
2573 else extra = AT.NumberMemMax;
2574 if ( AT.NumberMemHeap ) M_free(AT.NumberMemHeap,
"NumberMalloc");
2575 newbufs = (UWORD *)Malloc1(extra*(AM.MaxTal+NUMBEREXTRAWORDS)*
sizeof(UWORD),
"NumberMalloc");
2576 AT.NumberMemHeap = (UWORD **)Malloc1((extra+AT.NumberMemMax)*
sizeof(UWORD *),
"NumberMalloc");
2577 for ( i = 0; i < extra; i++ ) {
2578 AT.NumberMemHeap[i] = newbufs + i*(LONG)(AM.MaxTal+NUMBEREXTRAWORDS);
2580 #ifdef TERMMALLOCDEBUG 2581 DebugHeap4 = (UWORD **)Malloc1((extra+AT.NumberMemMax)*
sizeof(WORD *),
"NumberMalloc");
2582 for ( i = 0; i < AT.NumberMemMax; i++ ) { DebugHeap4[i] = DebugHeap3[i]; }
2583 for ( i = 0; i < extra; i++ ) {
2584 DebugHeap4[i+AT.NumberMemMax] = newbufs + i*(LONG)(AM.MaxTal+NUMBEREXTRAWORDS);
2586 if ( DebugHeap3 ) M_free(DebugHeap3,
"NumberMalloc");
2587 DebugHeap3 = DebugHeap4;
2589 AT.NumberMemTop = extra;
2590 AT.NumberMemMax += extra;
2596 #ifndef MEMORYMACROS 2598 UWORD *NumberMalloc2(
PHEAD char *text)
2600 if ( AT.NumberMemTop <= 0 ) NumberMallocAddMemory(BHEAD text);
2602 #ifdef MALLOCDEBUGOUTPUT 2603 if ( (AT.NumberMemMax-AT.NumberMemTop) > 10 )
2604 MesPrint(
"NumberMalloc: %s, %l/%l (%x)",text,AT.NumberMemTop,AT.NumberMemMax,AT.NumberMemHeap[AT.NumberMemTop-1]);
2608 return(AT.NumberMemHeap[--AT.NumberMemTop]);
2611 VOID NumberFree2(
PHEAD UWORD *NumberMem,
char *text)
2613 #ifdef TERMMALLOCDEBUG 2615 for ( i = 0; i < AT.NumberMemMax; i++ ) {
2616 if ( NumberMem == DebugHeap3[i] )
break;
2618 if ( i >= AT.NumberMemMax ) {
2619 MesPrint(
" ERROR: NumberFree called with an address not given by NumberMalloc.");
2624 AT.NumberMemHeap[AT.NumberMemTop++] = NumberMem;
2626 #ifdef MALLOCDEBUGOUTPUT 2627 if ( (AT.NumberMemMax-AT.NumberMemTop) > 10 )
2628 MesPrint(
"NumberFree: %s, %l/%l (%x)",text,AT.NumberMemTop,AT.NumberMemMax,NumberMem);
2641 VOID CacheNumberMallocAddMemory(PHEAD0)
2647 else extra = AT.CacheNumberMemMax;
2648 if ( AT.CacheNumberMemHeap ) M_free(AT.CacheNumberMemHeap,
"NumberMalloc");
2649 newbufs = (UWORD *)Malloc1(extra*(AM.MaxTal+NUMBEREXTRAWORDS)*
sizeof(UWORD),
"CacheNumberMalloc");
2650 AT.CacheNumberMemHeap = (UWORD **)Malloc1((extra+AT.NumberMemMax)*
sizeof(UWORD *),
"CacheNumberMalloc");
2651 for ( i = 0; i < extra; i++ ) {
2652 AT.CacheNumberMemHeap[i] = newbufs + i*(LONG)(AM.MaxTal+NUMBEREXTRAWORDS);
2654 AT.CacheNumberMemTop = extra;
2655 AT.CacheNumberMemMax += extra;
2658 #ifndef MEMORYMACROS 2660 UWORD *CacheNumberMalloc2(
PHEAD char *text)
2662 if ( AT.CacheNumberMemTop <= 0 ) CacheNumberMallocAddMemory(BHEAD0);
2664 #ifdef MALLOCDEBUGOUTPUT 2665 MesPrint(
"NumberMalloc: %s, %l/%l (%x)",text,AT.NumberMemTop,AT.NumberMemMax,AT.NumberMemHeap[AT.NumberMemTop-1]);
2669 return(AT.CacheNumberMemHeap[--AT.CacheNumberMemTop]);
2672 VOID CacheNumberFree2(
PHEAD UWORD *NumberMem,
char *text)
2675 AT.CacheNumberMemHeap[AT.CacheNumberMemTop++] = NumberMem;
2677 #ifdef MALLOCDEBUGOUTPUT 2678 MesPrint(
"NumberFree: %s, %l/%l (%x)",text,AT.NumberMemTop,AT.NumberMemMax,NumberMem);
2693 VOID *FromList(
LIST *L)
2702 i = ( L->
num * L->
size ) /
sizeof(
int);
2703 old = (
int *)L->
lijst; newL = (
int *)newlist;
2704 while ( --i >= 0 ) *newL++ = *old++;
2705 if ( L->
lijst ) M_free(L->
lijst,
"L->lijst FromList");
2719 VOID *From0List(
LIST *L)
2727 i = ( L->
num * L->
size ) /
sizeof(
int);
2728 old = (
int *)(L->
lijst); newL = (
int *)newlist;
2729 while ( --i >= 0 ) *newL++ = *old++;
2730 i = ( L->
maxnum - L->
num ) /
sizeof(
int);
2731 while ( --i >= 0 ) *newL++ = 0;
2732 if ( L->
lijst ) M_free(L->
lijst,
"L->lijst From0List");
2748 VOID *FromVarList(
LIST *L)
2754 else if ( L->
lijst ) {
2756 if ( L == &(AP.DollarList) ) {
2757 if ( L->
maxnum > MAXDOLLARVARIABLES ) L->
maxnum = MAXDOLLARVARIABLES;
2758 if ( L->
num >= MAXDOLLARVARIABLES ) {
2759 MesPrint(
"!!!More than %l objects in list of $-variables",
2760 MAXDOLLARVARIABLES);
2765 if ( L->
maxnum > MAXVARIABLES ) L->
maxnum = MAXVARIABLES;
2766 if ( L->
num >= MAXVARIABLES ) {
2767 MesPrint(
"!!!More than %l objects in list of variables",
2775 i = ( L->
num * L->
size ) /
sizeof(
int);
2776 old = (
int *)(L->
lijst); newL = (
int *)newlist;
2777 while ( --i >= 0 ) *newL++ = *old++;
2778 if ( L->
lijst ) M_free(L->
lijst,
"L->lijst from VarList");
2782 return( ((
char *)(L->
lijst)) + L->
size * ((L->
num)++) );
2790 int DoubleList(VOID ***lijst,
int *oldsize,
int objectsize,
char *nameoftype)
2793 LONG i, newsize, fullsize;
2795 static LONG maxlistsize = (LONG)(MAXPOSITIVE);
2796 if ( *lijst == 0 ) {
2797 if ( *oldsize > 0 ) newsize = *oldsize;
2800 else newsize = *oldsize * 2;
2801 if ( newsize > maxlistsize ) {
2802 if ( *oldsize == maxlistsize ) {
2803 MesPrint(
"No memory for extra space in %s",nameoftype);
2806 newsize = maxlistsize;
2808 fullsize = ( newsize * objectsize +
sizeof(VOID *)-1 ) & (-
sizeof(VOID *));
2809 newlist = (VOID **)Malloc1(fullsize,nameoftype);
2811 to = newlist; from = *lijst; i = (*oldsize * objectsize)/
sizeof(VOID *);
2818 while ( --i >= 0 ) *to++ = *from++;
2820 if ( *lijst ) M_free(*lijst,
"DoubleLList");
2842 int DoubleLList(VOID ***lijst, LONG *oldsize,
int objectsize,
char *nameoftype)
2845 LONG i, newsize, fullsize;
2847 static LONG maxlistsize = (LONG)(MAXLONG);
2848 if ( *lijst == 0 ) {
2849 if ( *oldsize > 0 ) newsize = *oldsize;
2852 else newsize = *oldsize * 2;
2853 if ( newsize > maxlistsize ) {
2854 if ( *oldsize == maxlistsize ) {
2855 MesPrint(
"No memory for extra space in %s",nameoftype);
2858 newsize = maxlistsize;
2860 fullsize = ( newsize * objectsize +
sizeof(VOID *)-1 ) & (-
sizeof(VOID *));
2861 newlist = (VOID **)Malloc1(fullsize,nameoftype);
2863 to = newlist; from = *lijst; i = (*oldsize * objectsize)/
sizeof(VOID *);
2870 while ( --i >= 0 ) *to++ = *from++;
2872 if ( *lijst ) M_free(*lijst,
"DoubleLList");
2883 #define DODOUBLE(x) { x *s, *t, *u; if ( *start ) { \ 2884 oldsize = *(x **)stop - *(x **)start; newsize = 2*oldsize; \ 2885 t = u = (x *)Malloc1(newsize*sizeof(x),text); s = *(x **)start; \ 2886 for ( i = 0; i < oldsize; i++ ) *t++ = *s++; M_free(*start,"double"); } \ 2887 else { newsize = 100; u = (x *)Malloc1(newsize*sizeof(x),text); } \ 2888 *start = (void *)u; *stop = (void *)(u+newsize); } 2890 void DoubleBuffer(
void **start,
void **stop,
int size,
char *text)
2892 LONG oldsize, newsize, i;
2893 if ( size ==
sizeof(
char) ) DODOUBLE(
char)
2894 else if ( size ==
sizeof(
short) ) DODOUBLE(
short)
2895 else if ( size ==
sizeof(
int) ) DODOUBLE(
int)
2896 else if ( size ==
sizeof(LONG) ) DODOUBLE(LONG)
2897 else if ( size %
sizeof(
int) == 0 ) DODOUBLE(
int)
2899 MesPrint(
"---Cannot handle doubling buffers of size %d",size);
2909 #define DOEXPAND(x) { x *newbuffer, *t, *m; \ 2910 t = newbuffer = (x *)Malloc1((newsize+2)*type,"ExpandBuffer"); \ 2911 if ( *buffer ) { m = (x *)*buffer; i = *oldsize; \ 2912 while ( --i >= 0 ) *t++ = *m++; M_free(*buffer,"ExpandBuffer"); \ 2913 } *buffer = newbuffer; *oldsize = newsize; } 2915 void ExpandBuffer(
void **buffer, LONG *oldsize,
int type)
2918 if ( *oldsize <= 0 ) { newsize = 100; }
2919 else newsize = 2*(*oldsize);
2920 if ( type ==
sizeof(
char) ) DOEXPAND(
char)
2921 else if ( type ==
sizeof(
short) ) DOEXPAND(
short)
2922 else if ( type ==
sizeof(
int) ) DOEXPAND(
int)
2923 else if ( type ==
sizeof(LONG) ) DOEXPAND(LONG)
2926 MesPrint(
"---Cannot handle expanding buffers with objects of size %d",type);
2939 LONG iexp(LONG x,
int p)
2943 if ( x == 0 )
return(0);
2944 if ( p == 0 )
return(1);
2945 if ( x < 0 ) { sign = -1; x = -x; }
2947 if ( sign < 0 && ( p & 1 ) == 0 ) sign = 1;
2948 if ( x == 1 )
return(sign);
2949 if ( p < 0 )
return(0);
2952 if ( ( p & 1 ) != 0 ) y *= x;
2956 if ( sign < 0 ) y = -y;
2969 void ToGeneral(WORD *r, WORD *m, WORD par)
2973 else { m[1] = 0; m += ARGHEAD + 1; }
2977 if ( j >= FUNCTION ) { *m++ = j; *m++ = FUNHEAD; FILLFUN(m) }
2980 case SYMBOL: *m++ = j; *m++ = 4; *m++ = *r++; *m++ = 1;
break;
2982 if ( *r > 0 ) { *m++ = *r; *m++ = 1; *m++ = 3; }
2983 else if ( *r == 0 ) { m--; }
2984 else { *m++ = -*r; *m++ = 1; *m++ = -3; }
2986 case MINVECTOR: k = -k;
2988 case VECTOR: *m++ = INDEX; *m++ = 3; *m++ = *r++;
break;
2991 *m++ = 1; *m++ = 1; *m++ = k;
2994 if ( !par ) mm[ARGHEAD] = *mm-ARGHEAD;
3009 int ToFast(WORD *r, WORD *m)
3012 if ( *r == ARGHEAD ) { *m++ = -SNUMBER; *m++ = 0;
return(1); }
3013 if ( *r != r[ARGHEAD]+ARGHEAD )
return(0);
3016 if ( r[2] != 1 || r[1] <= 0 )
return(0);
3017 *m++ = -SNUMBER; *m = ( r[3] < 0 ) ? -r[1] : r[1];
return(1);
3020 if ( r[i-1] != 1 || r[i-2] != 1 )
return(0);
3022 if ( r[i] == -3 && r[2] == *r-4 && r[2] == 3 && r[1] == INDEX
3023 && r[3] < MINSPEC ) {}
3026 else if ( r[2] != *r - 4 )
return(0);
3028 if ( *r >= FUNCTION ) {
3029 if ( r[1] <= FUNHEAD ) { *m++ = -*r;
return(1); }
3031 else if ( *r == SYMBOL ) {
3032 if ( r[1] == 4 && r[3] == 1 )
3033 { *m++ = -SYMBOL; *m++ = r[2];
return(1); }
3035 else if ( *r == INDEX ) {
3037 if ( r[2] >= MINSPEC ) {
3038 if ( r[2] >= 0 && r[2] < AM.OffsetIndex ) *m++ = -SNUMBER;
3042 if ( r[5] == -3 ) *m++ = -MINVECTOR;
3043 else *m++ = -VECTOR;
3062 WORD ToPolyFunGeneral(
PHEAD WORD *term)
3064 WORD *t = term+1, *tt, *to, *to1, *termout, *tstop, *tnext;
3065 WORD numarg, i, change = 0;
3066 tstop = term + *term; tstop -= ABS(tstop[-1]);
3067 termout = to = AT.WorkPointer;
3069 while ( t < tstop ) {
3070 if ( *t == AR.PolyFun ) {
3071 tt = t+FUNHEAD; tnext = t + t[1];
3073 while ( tt < tnext ) { numarg++; NEXTARG(tt); }
3074 if ( numarg == 2 ) {
3077 i = FUNHEAD; NCOPY(to,t,i);
3078 while ( tt < tnext ) {
3080 i = *tt; NCOPY(to,tt,i);
3082 else if ( *tt == -SYMBOL ) {
3083 to1[1] += 6+ARGHEAD; to1[2] |= MUSTCLEANPRF; change++;
3084 *to++ = 8+ARGHEAD; *to++ = 0; FILLARG(to);
3085 *to++ = 8; *to++ = SYMBOL; *to++ = 4; *to++ = tt[1];
3086 *to++ = 1; *to++ = 1; *to++ = 1; *to++ = 3;
3089 else if ( *tt == -SNUMBER ) {
3091 to1[1] += 2+ARGHEAD; to1[2] |= MUSTCLEANPRF; change++;
3092 *to++ = 4+ARGHEAD; *to++ = 0; FILLARG(to);
3093 *to++ = 4; *to++ = tt[1]; *to++ = 1; *to++ = 3;
3096 else if ( tt[1] < 0 ) {
3097 to1[1] += 2+ARGHEAD; to1[2] |= MUSTCLEANPRF; change++;
3098 *to++ = 4+ARGHEAD; *to++ = 0; FILLARG(to);
3099 *to++ = 4; *to++ = -tt[1]; *to++ = 1; *to++ = -3;
3103 MLOCK(ErrorMessageLock);
3104 MesPrint(
"Internal error: Zero in PolyRatFun");
3105 MUNLOCK(ErrorMessageLock);
3114 i = t[1]; NCOPY(to,t,i)
3118 while ( t < tt ) *to++ = *t++;
3119 *termout = to - termout;
3120 t = term; i = *termout; tt = termout;
3122 AT.WorkPointer = term + *term;
3137 int IsLikeVector(WORD *arg)
3139 WORD *sstop, *t, *tstop;
3141 if ( *arg == -VECTOR || *arg == -INDEX )
return(1);
3142 if ( *arg == -SNUMBER && arg[1] >= 0 && arg[1] < AM.OffsetIndex )
3146 sstop = arg + *arg; arg += ARGHEAD;
3147 while ( arg < sstop ) {
3149 tstop = t - ABS(t[-1]);
3151 while ( arg < tstop ) {
3152 if ( *arg == INDEX )
return(1);
3165 int AreArgsEqual(WORD *arg1, WORD *arg2)
3168 if ( *arg2 != *arg1 )
return(0);
3171 while ( --i > 0 ) {
if ( arg1[i] != arg2[i] )
return(0); }
3174 else if ( *arg1 <= -FUNCTION )
return(1);
3175 else if ( arg1[1] == arg2[1] )
return(1);
3184 int CompareArgs(WORD *arg1, WORD *arg2)
3188 if ( *arg2 < 0 )
return(-1);
3189 i1 = *arg1-ARGHEAD; arg1 += ARGHEAD;
3190 i2 = *arg2-ARGHEAD; arg2 += ARGHEAD;
3191 while ( i1 > 0 && i2 > 0 ) {
3192 if ( *arg1 != *arg2 )
return((
int)(*arg1)-(
int)(*arg2));
3193 i1--; i2--; arg1++; arg2++;
3197 else if ( *arg2 > 0 )
return(1);
3199 if ( *arg1 != *arg2 ) {
3200 if ( *arg1 < *arg2 )
return(-1);
3203 if ( *arg1 <= -FUNCTION )
return(0);
3204 return((
int)(arg1[1])-(
int)(arg2[1]));
3215 int CompArg(WORD *s1, WORD *s2)
3218 WORD *st1, *st2, x[7];
3222 if ( *s1 <= -FUNCTION && *s2 <= -FUNCTION ) {
3223 if ( *s1 > *s2 )
return(-1);
3224 if ( *s1 < *s2 )
return(1);
3227 if ( *s1 > *s2 )
return(1);
3228 if ( *s1 < *s2 )
return(-1);
3229 if ( *s1 <= -FUNCTION )
return(0);
3231 if ( *s1 > *s2 )
return(1);
3232 if ( *s1 < *s2 )
return(-1);
3235 x[1] = AT.comsym[3];
3236 x[2] = AT.comnum[1];
3237 x[3] = AT.comnum[3];
3238 x[4] = AT.comind[3];
3239 x[5] = AT.comind[6];
3240 x[6] = AT.comfun[1];
3241 if ( *s1 == -SYMBOL ) {
3242 AT.comsym[3] = s1[1];
3243 st1 = AT.comsym+8; s1 = AT.comsym;
3245 else if ( *s1 == -SNUMBER ) {
3247 AT.comnum[1] = -s1[1]; AT.comnum[3] = -3;
3250 AT.comnum[1] = s1[1]; AT.comnum[3] = 3;
3255 else if ( *s1 == -INDEX || *s1 == -VECTOR ) {
3256 AT.comind[3] = s1[1]; AT.comind[6] = 3;
3257 st1 = AT.comind+7; s1 = AT.comind;
3259 else if ( *s1 == -MINVECTOR ) {
3260 AT.comind[3] = s1[1]; AT.comind[6] = -3;
3261 st1 = AT.comind+7; s1 = AT.comind;
3263 else if ( *s1 <= -FUNCTION ) {
3264 AT.comfun[1] = -*s1;
3265 st1 = AT.comfun+FUNHEAD+4; s1 = AT.comfun;
3271 else if ( *s1 == -ARGWILD ) {
3274 else {
goto argerror; }
3275 st2 = s2 + *s2; s2 += ARGHEAD;
3278 else if ( *s2 < 0 ) {
3279 x[1] = AT.comsym[3];
3280 x[2] = AT.comnum[1];
3281 x[3] = AT.comnum[3];
3282 x[4] = AT.comind[3];
3283 x[5] = AT.comind[6];
3284 x[6] = AT.comfun[1];
3285 if ( *s2 == -SYMBOL ) {
3286 AT.comsym[3] = s2[1];
3287 st2 = AT.comsym+8; s2 = AT.comsym;
3289 else if ( *s2 == -SNUMBER ) {
3291 AT.comnum[1] = -s2[1]; AT.comnum[3] = -3;
3294 else if ( s2[1] == 0 ) {
3295 st2 = AT.comnum+4; s2 = st2;
3298 AT.comnum[1] = s2[1]; AT.comnum[3] = 3;
3303 else if ( *s2 == -INDEX || *s2 == -VECTOR ) {
3304 AT.comind[3] = s2[1]; AT.comind[6] = 3;
3305 st2 = AT.comind+7; s2 = AT.comind;
3307 else if ( *s2 == -MINVECTOR ) {
3308 AT.comind[3] = s2[1]; AT.comind[6] = -3;
3309 st2 = AT.comind+7; s2 = AT.comind;
3311 else if ( *s2 <= -FUNCTION ) {
3312 AT.comfun[1] = -*s2;
3313 st2 = AT.comfun+FUNHEAD+4; s2 = AT.comfun;
3319 else if ( *s2 == -ARGWILD ) {
3322 else {
goto argerror; }
3323 st1 = s1 + *s1; s1 += ARGHEAD;
3327 x[1] = AT.comsym[3];
3328 x[2] = AT.comnum[1];
3329 x[3] = AT.comnum[3];
3330 x[4] = AT.comind[3];
3331 x[5] = AT.comind[6];
3332 x[6] = AT.comfun[1];
3333 st1 = s1 + *s1; st2 = s2 + *s2;
3334 s1 += ARGHEAD; s2 += ARGHEAD;
3336 while ( s1 < st1 && s2 < st2 ) {
3337 if ( ( k = CompareTerms(BHEAD s1,s2,(WORD)2) ) != 0 ) {
3338 AT.comsym[3] = x[1];
3339 AT.comnum[1] = x[2];
3340 AT.comnum[3] = x[3];
3341 AT.comind[3] = x[4];
3342 AT.comind[6] = x[5];
3343 AT.comfun[1] = x[6];
3346 s1 += *s1; s2 += *s2;
3348 AT.comsym[3] = x[1];
3349 AT.comnum[1] = x[2];
3350 AT.comnum[3] = x[3];
3351 AT.comind[3] = x[4];
3352 AT.comind[6] = x[5];
3353 AT.comfun[1] = x[6];
3354 if ( s1 < st1 )
return(1);
3355 if ( s2 < st2 )
return(-1);
3360 MesPrint(
"Illegal type of short function argument in Normalize");
3361 Terminate(-1);
return(0);
3369 #include <sys/timeb.h> 3385 return(((LONG)(tp.time)-AM.OldSecTime)*100 +
3386 ((LONG)(tp.millitm)-AM.OldMilliTime)/10);
3389 AM.OldSecTime = (LONG)(tp.time);
3390 AM.OldMilliTime = (LONG)(tp.millitm);
3400 LONG TimeChildren(WORD par)
3402 if ( par )
return(Timer(1)-AM.OldChildTime);
3403 AM.OldChildTime = Timer(1);
3421 if ( par )
return(Timer(0)-AR.OldTime);
3422 AR.OldTime = Timer(0);
3430 #if defined(WINDOWS) 3434 #ifndef WITHPTHREADS 3435 static int initialized = 0;
3436 static HANDLE hProcess;
3437 FILETIME ftCreate, ftExit, ftKernel, ftUser;
3440 if ( !initialized ) {
3441 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId());
3443 if ( GetProcessTimes(hProcess, &ftCreate, &ftExit, &ftKernel, &ftUser) ) {
3444 PFILETIME pftKernel = &ftKernel;
3445 PFILETIME pftUser = &ftUser;
3446 __int64 t = *(__int64 *)pftKernel + *(__int64 *)pftUser;
3447 return (LONG)(t / 10000);
3453 FILETIME ftCreate, ftExit, ftKernel, ftUser;
3456 hThread = OpenThread(THREAD_QUERY_INFORMATION, FALSE, GetCurrentThreadId());
3458 if ( GetThreadTimes(hThread, &ftCreate, &ftExit, &ftKernel, &ftUser) ) {
3459 PFILETIME pftKernel = &ftKernel;
3460 PFILETIME pftUser = &ftUser;
3461 __int64 t = *(__int64 *)pftKernel + *(__int64 *)pftUser;
3462 lResult = (LONG)(t / 10000);
3464 CloseHandle(hThread);
3471 #include <sys/time.h> 3472 #include <sys/resource.h> 3473 #ifdef WITHPOSIXCLOCK 3482 #ifdef WITHPOSIXCLOCK 3501 if ( clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t) ) {
3502 MesPrint(
"Error in getting timing information");
3504 return (LONG)t.tv_sec * 1000 + (LONG)t.tv_nsec / 1000000;
3508 struct rusage rusage;
3510 getrusage(RUSAGE_CHILDREN,&rusage);
3511 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3512 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3515 getrusage(RUSAGE_SELF,&rusage);
3516 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3517 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3524 #include <sys/time.h> 3525 #include <sys/resource.h> 3529 struct rusage rusage;
3531 getrusage(RUSAGE_CHILDREN,&rusage);
3532 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3533 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3536 getrusage(RUSAGE_SELF,&rusage);
3537 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3538 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3543 #include <sys/time.h> 3544 #include <sys/resource.h> 3548 struct rusage rusage;
3550 getrusage(RUSAGE_CHILDREN,&rusage);
3551 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3552 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3555 getrusage(RUSAGE_SELF,&rusage);
3556 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3557 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3573 #include <sys/time.h> 3574 #include <sys/resource.h> 3575 struct rusage rusage;
3577 getrusage(RUSAGE_CHILDREN,&rusage);
3578 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3579 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3582 getrusage(RUSAGE_SELF,&rusage);
3583 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3584 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3588 clock_t tikken = clock();
3589 return((LONG)tikken/1000);
3591 clock_t t, tikken = clock();
3592 t = tikken % CLK_TCK;
3595 tikken += (t*1000)/CLK_TCK;
3603 void times(tbuffer_t *buffer);
3609 if ( par == 1 ) {
return(0); }
3612 return(buffer.proc_user_time * 10);
3625 #include <sys/types.h> 3626 #include <sys/times.h> 3631 #include <sys/time.h> 3632 #include <sys/resource.h> 3640 if ( par == 1 ) {
return(0); }
3642 if ( ( AO.wrapnum & 1 ) != 0 ) t ^= 0x80000000;
3648 return(AO.wrap+(t/1000));
3653 ULONG a1, a2, a3, a4;
3654 if ( par == 1 ) {
return(0); }
3656 a1 = (ULONG)buffer.tms_utime;
3660 a2 = 1000*a2 + (a3 >> 16);
3665 ret = (LONG)((a4 << 16) + a3 / CLK_TCK);
3671 struct timezone tzp;
3672 if ( par == 1 ) {
return(0); }
3673 gettimeofday(&tp,&tzp); */
3674 return(tp.tv_sec*1000+tp.tv_usec/1000);
3676 struct rusage rusage;
3678 getrusage(RUSAGE_CHILDREN,&rusage);
3679 return((rusage.ru_utime.tv_sec+rusage.ru_stime.tv_sec)*1000
3680 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3683 getrusage(RUSAGE_SELF,&rusage);
3684 return((rusage.ru_utime.tv_sec+rusage.ru_stime.tv_sec)*1000
3685 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3731 int errorcode = 0, coeffsize;
3732 WORD *t, *tt, *tstop, *endterm, *targ, *targstop, *funstop, *argterm;
3733 endterm = term + *term;
3734 coeffsize = ABS(endterm[-1]);
3735 if ( coeffsize >= *term ) {
3736 MLOCK(ErrorMessageLock);
3737 MesPrint(
"TestTerm: Internal inconsistency in term. Coefficient too big.");
3738 MUNLOCK(ErrorMessageLock);
3742 if ( ( coeffsize < 3 ) || ( ( coeffsize & 1 ) != 1 ) ) {
3743 MLOCK(ErrorMessageLock);
3744 MesPrint(
"TestTerm: Internal inconsistency in term. Wrong size coefficient.");
3745 MUNLOCK(ErrorMessageLock);
3750 tstop = endterm - coeffsize;
3751 while ( t < tstop ) {
3762 MLOCK(ErrorMessageLock);
3763 MesPrint(
"TestTerm: Internal inconsistency in term. L or S number");
3764 MUNLOCK(ErrorMessageLock);
3770 case DOLLAREXPRESSION:
3783 MLOCK(ErrorMessageLock);
3784 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal subterm.");
3785 MUNLOCK(ErrorMessageLock);
3793 MLOCK(ErrorMessageLock);
3794 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal subterm number.");
3795 MUNLOCK(ErrorMessageLock);
3802 if ( *t-FUNCTION >= NumFunctions ) {
3803 MLOCK(ErrorMessageLock);
3804 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal function number");
3805 MUNLOCK(ErrorMessageLock);
3810 if ( funstop > tstop )
goto subtermsize;
3812 MLOCK(ErrorMessageLock);
3813 MesPrint(
"TestTerm: Internal inconsistency in term. Dirty flag nonzero.");
3814 MUNLOCK(ErrorMessageLock);
3819 if ( targ > funstop ) {
3820 MLOCK(ErrorMessageLock);
3821 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal function size.");
3822 MUNLOCK(ErrorMessageLock);
3826 if ( functions[*t-FUNCTION].spec >= TENSORFUNCTION ) {
3829 while ( targ < funstop ) {
3831 if ( *targ <= -(FUNCTION+NumFunctions) ) {
3832 MLOCK(ErrorMessageLock);
3833 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal function number in argument.");
3834 MUNLOCK(ErrorMessageLock);
3838 if ( *targ <= -FUNCTION ) { targ++; }
3840 if ( ( *targ != -SYMBOL ) && ( *targ != -VECTOR )
3841 && ( *targ != -MINVECTOR )
3842 && ( *targ != -SNUMBER )
3843 && ( *targ != -ARGWILD )
3844 && ( *targ != -INDEX ) ) {
3845 MLOCK(ErrorMessageLock);
3846 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal object in argument.");
3847 MUNLOCK(ErrorMessageLock);
3854 else if ( ( *targ < ARGHEAD ) || ( targ+*targ > funstop ) ) {
3855 MLOCK(ErrorMessageLock);
3856 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal size of argument.");
3857 MUNLOCK(ErrorMessageLock);
3861 else if ( targ[1] != 0 ) {
3862 MLOCK(ErrorMessageLock);
3863 MesPrint(
"TestTerm: Internal inconsistency in term. Dirty flag in argument.");
3864 MUNLOCK(ErrorMessageLock);
3869 targstop = targ + *targ;
3870 argterm = targ + ARGHEAD;
3871 while ( argterm < targstop ) {
3872 if ( ( *argterm < 4 ) || ( argterm + *argterm > targstop ) ) {
3873 MLOCK(ErrorMessageLock);
3874 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal termsize in argument.");
3875 MUNLOCK(ErrorMessageLock);
3880 MLOCK(ErrorMessageLock);
3881 MesPrint(
"TestTerm: Internal inconsistency in term. Called from TestTerm.");
3882 MUNLOCK(ErrorMessageLock);
3886 argterm += *argterm;
3897 MLOCK(ErrorMessageLock);
3898 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal subterm size.");
3899 MUNLOCK(ErrorMessageLock);
int PutPreVar(UBYTE *, UBYTE *, UBYTE *, int)
LONG PF_BroadcastNumber(LONG x)
int PF_Bcast(void *buffer, int count)
struct bit_field * one_byte
LONG PF_WriteFileToFile(int handle, UBYTE *buffer, LONG size)
struct bit_field set_of_char[32]