69#define BANNER (4*sizeof(LONG))
70void *malloclist[60000];
71LONG mallocsizes[60000];
72char *mallocstrings[60000];
77extern "C" getdtablesize();
90#error "MALLOCPROTECT": undefine "TRAPSIGNALS" in unix.h first!
92#include "mallocprotect.h"
98#define M_alloc mprotectMalloc
102#ifdef TERMMALLOCDEBUG
103WORD **DebugHeap1, **DebugHeap2;
112UBYTE *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");
151UBYTE 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;
229 if ( stream->type == INPUTSTREAM ) {
234 if ( ReadFile(stream->handle,&c,1) != 1 ) {
237 if ( stream->fileposition == 0 ) {
239 stream->buffersize = 32;
240 stream->
buffer = (UBYTE *)Malloc1(stream->buffersize,
"input stream buffer");
244 if ( stream->
top - stream->
buffer >= stream->buffersize ) {
245 LONG oldsize = stream->buffersize;
246 DoubleBuffer((
void**)&stream->
buffer,(
void**)&stream->
top,
sizeof(UBYTE),
"double input stream buffer");
247 stream->buffersize = stream->
top - stream->
buffer;
256 if ( stream->eqnum == 1 ) { stream->eqnum = 0; stream->linenumber++; }
257 if ( c == LINEFEED ) stream->eqnum = 1;
261 if ( stream->type != FILESTREAM )
return(ENDOFSTREAM);
262 if ( stream->fileposition != stream->bufferposition+stream->inbuffer ) {
263 stream->fileposition = stream->bufferposition+stream->inbuffer;
264 SETBASEPOSITION(scrpos,stream->fileposition);
265 SeekFile(stream->handle,&scrpos,SEEK_SET);
267 stream->bufferposition = stream->fileposition;
268 stream->inbuffer = ReadFile(stream->handle,
269 stream->
buffer,stream->buffersize);
270 if ( stream->inbuffer <= 0 )
return(ENDOFSTREAM);
271 stream->
top = stream->
buffer + stream->inbuffer;
273 stream->fileposition = stream->bufferposition + stream->inbuffer;
275 if ( stream->eqnum == 1 ) { stream->eqnum = 0; stream->linenumber++; }
277 if ( c == LINEFEED ) stream->eqnum = 1;
286UBYTE GetFromStream(
STREAM *stream)
289 if ( stream->isnextchar > 0 ) {
290 return(stream->nextchar[--stream->isnextchar]);
292 c1 = ReadFromStream(stream);
293 if ( c1 == LINEFEED || c1 == CARRIAGERETURN ) {
294 c2 = ReadFromStream(stream);
295 if ( c2 == c1 || ( c2 != LINEFEED && c2 != CARRIAGERETURN ) ) {
296 stream->isnextchar = 1;
297 stream->nextchar[0] = c2;
309UBYTE LookInStream(
STREAM *stream)
311 UBYTE c = GetFromStream(stream);
312 UngetFromStream(stream,c);
321STREAM *OpenStream(UBYTE *name,
int type,
int prevarmode,
int raiselow)
324 UBYTE *rhsofvariable, *s, *newname, c;
329 case REVERSEFILESTREAM:
337 handle = LocateFile(&newname,-1);
338 if ( handle < 0 )
return(0);
340 SeekFile(handle,&scrpos,SEEK_END);
341 TELLFILE(handle,&scrpos);
342 filesize = BASEPOSITION(scrpos);
344 SeekFile(handle,&scrpos,SEEK_SET);
345 if ( filesize > AM.MaxStreamSize && type == FILESTREAM )
346 filesize = AM.MaxStreamSize;
347 stream = CreateStream((UBYTE *)
"filestream");
351 stream->
buffer = (UBYTE *)Malloc1(filesize+1,
"name of input stream");
352 stream->inbuffer = ReadFile(handle,stream->
buffer,filesize);
353 if ( type == REVERSEFILESTREAM ) {
354 if ( ReverseStatements(stream) ) {
355 M_free(stream->
buffer,
"name of input stream");
359 stream->
top = stream->
buffer + stream->inbuffer;
361 stream->handle = handle;
362 stream->buffersize = filesize;
363 stream->fileposition = stream->inbuffer;
364 if ( newname != name ) stream->
name = newname;
365 else if ( name ) stream->
name = strDup1(name,
"name of input stream");
368 stream->prevline = stream->linenumber = 1;
372 if ( ( rhsofvariable = GetPreVar(name,WITHERROR) ) == 0 )
return(0);
373 stream = CreateStream((UBYTE *)
"var-stream");
377 stream->inbuffer = s - stream->
buffer;
378 stream->
name = AC.CurrentStream->name;
379 stream->linenumber = AC.CurrentStream->linenumber;
380 stream->prevline = AC.CurrentStream->prevline;
381 stream->eqnum = AC.CurrentStream->eqnum;
382 stream->
pname = strDup1(name,
"stream->pname");
383 stream->olddelay = AP.AllowDelay;
384 s = stream->
pname;
while ( *s ) s++;
385 while ( s[-1] ==
'+' || s[-1] ==
'-' ) s--;
390 if ( ( num = GetDollar(name) ) < 0 ) {
396 s = name;
while ( *s && *s !=
'[' ) s++;
397 if ( *s == 0 )
return(0);
399 if ( ( num = GetDollar(name) ) < 0 )
return(0);
402 if ( *s == 0 || FG.cTable[*s] != 1 || *s ==
']' ) {
403 MesPrint(
"@Illegal factor number for dollar variable");
406 while ( *s && FG.cTable[*s] == 1 ) {
407 numfac = 10*numfac+*s++-
'0';
409 if ( *s !=
']' || s[1] != 0 ) {
410 MesPrint(
"@Illegal factor number for $ variable");
413 stream = CreateStream((UBYTE *)
"dollar-stream");
414 stream->
buffer = stream->
pointer = s = WriteDollarFactorToBuffer(num,numfac,1);
417 stream = CreateStream((UBYTE *)
"dollar-stream");
418 stream->
buffer = stream->
pointer = s = WriteDollarToBuffer(num,1);
422 stream->inbuffer = s - stream->
buffer;
423 stream->
name = AC.CurrentStream->name;
424 stream->linenumber = AC.CurrentStream->linenumber;
425 stream->prevline= AC.CurrentStream->prevline;
426 stream->eqnum = AC.CurrentStream->eqnum;
427 stream->
pname = strDup1(name,
"stream->pname");
428 s = stream->
pname;
while ( *s ) s++;
429 while ( s[-1] ==
'+' || s[-1] ==
'-' ) s--;
432 AO.DollarOutSizeBuffer = 0;
433 AO.DollarOutBuffer = 0;
434 AO.DollarInOutBuffer = 0;
440 stream = CreateStream((UBYTE *)
"calculator");
444 stream->inbuffer = s - stream->
buffer;
445 stream->
name = AC.CurrentStream->name;
446 stream->linenumber = AC.CurrentStream->linenumber;
447 stream->prevline = AC.CurrentStream->prevline;
452 stream = CreateStream((UBYTE *)
"pipe");
456 if ( ( f = popen((
char *)name,
"r") ) == 0 ) {
457 Error0(
"@Cannot create pipe");
459 stream->handle = CreateHandle();
460 RWLOCKW(AM.handlelock);
461 filelist[stream->handle] = (FILES *)f;
462 UNRWLOCK(AM.handlelock);
465 stream->inbuffer = 0;
470 if ( PF.me == MASTER ) {
471 f = popen((
char *)name,
"r");
473 if ( f == 0 ) Error0(
"@Cannot create pipe");
479 stream->handle = CreateHandle();
480 RWLOCKW(AM.handlelock);
481 filelist[stream->handle] = (FILES *)f;
482 UNRWLOCK(AM.handlelock);
485 stream->buffersize = AM.MaxStreamSize;
486 stream->
buffer = (UBYTE *)Malloc1(stream->buffersize,
"pipe buffer");
487 stream->inbuffer = 0;
491 stream->
name = strDup1((UBYTE *)
"pipe",
"pipe");
492 stream->prevline = stream->linenumber = 1;
497#ifdef WITHEXTERNALCHANNEL
498 case EXTERNALCHANNELSTREAM:
501 if( (n=getCurrentExternalChannel()) == 0 )
502 Error0(
"@No current external channel");
503 stream = CreateStream((UBYTE *)
"externalchannel");
504 stream->handle = CreateHandle();
505 tmpn = (
int *)Malloc1(
sizeof(
int),
"external channel handle");
507 RWLOCKW(AM.handlelock);
508 filelist[stream->handle] = (FILES *)tmpn;
509 UNRWLOCK(AM.handlelock);
512 stream->inbuffer = 0;
513 stream->
name = strDup1((UBYTE *)
"externalchannel",
"externalchannel");
514 stream->prevline = stream->linenumber = 1;
525 stream = CreateStream((UBYTE *)
"input stream");
526 stream->handle = CreateHandle();
528 FILES *f = (FILES *)Malloc1(
sizeof(
int),
"input stream handle");
531 f->descriptor = (int)(ssize_t)name;
533 f = name ? (FILES *)name : stdin;
535 RWLOCKW(AM.handlelock);
536 filelist[stream->handle] = f;
537 UNRWLOCK(AM.handlelock);
540 stream->inbuffer = 0;
541 stream->
name = strDup1((UBYTE *)(name ?
"INPUT" :
"STDIN"),
"input stream name");
542 stream->prevline = stream->linenumber = 1;
549 stream->fileposition = -1;
554 stream->bufferposition = 0;
555 stream->isnextchar = 0;
557 stream->previousNoShowInput = AC.NoShowInput;
558 stream->afterwards = raiselow;
559 if ( AC.CurrentStream ) stream->previous = AC.CurrentStream - AC.Streams;
560 else stream->previous = -1;
562 if ( prevarmode == 0 ) stream->prevars = -1;
563 else if ( prevarmode > 0 ) stream->prevars = NumPre;
564 else if ( prevarmode < 0 ) stream->prevars = -prevarmode-1;
565 AC.CurrentStream = stream;
566 if ( type == PREREADSTREAM || type == PREREADSTREAM3 || type == PRECALCSTREAM
567 || type == DOLLARSTREAM ) AC.NoShowInput = 1;
576int LocateFile(UBYTE **name,
int type)
578 int handle, namesize, i;
579 UBYTE *s, *to, *u1, *u2, *newname, *indir;
580 handle = OpenFile((
char *)(*name));
581 if ( handle >= 0 )
return(handle);
582 if ( type == SETUPFILE && AM.SetupFile ) {
583 handle = OpenFile((
char *)(AM.SetupFile));
584 if ( handle >= 0 )
return(handle);
585 MesPrint(
"Could not open setup file %s",(
char *)(AM.SetupFile));
587 namesize = 4; s = *name;
588 while ( *s ) { s++; namesize++; }
589 if ( type == SETUPFILE ) indir = AM.SetupDir;
590 else indir = AM.IncDir;
594 while ( *s ) { s++; i++; }
595 newname = (UBYTE *)Malloc1(namesize+i,
"LocateFile");
596 s = indir; to = newname;
597 while ( *s ) *to++ = *s++;
598 if ( to > newname && to[-1] != SEPARATOR ) *to++ = SEPARATOR;
600 while ( *s ) *to++ = *s++;
602 handle = OpenFile((
char *)newname);
607 M_free(newname,
"LocateFile, incdir/file");
609 if ( type == SETUPFILE ) {
610 handle = OpenFile(setupfilename);
611 if ( handle >= 0 )
return(handle);
612 s = (UBYTE *)getenv(
"FORMSETUP");
614 handle = OpenFile((
char *)s);
615 if ( handle >= 0 )
return(handle);
616 MesPrint(
"Could not open setup file %s",s);
619 if ( type != SETUPFILE && AM.Path ) {
624 while ( *u1 && *u1 !=
';' ) {
628 while ( *u1 && *u1 !=
':' ) {
629 if ( *u1 ==
'\\' ) u1++;
633 newname = (UBYTE *)Malloc1(namesize+i,
"LocateFile");
634 s = u2; to = newname;
637 if ( *s ==
'\\' ) s++;
641 if ( to > newname && to[-1] != SEPARATOR ) *to++ = SEPARATOR;
643 while ( *s ) *to++ = *s++;
645 handle = OpenFile((
char *)newname);
650 M_free(newname,
"LocateFile Path/file");
654 if ( type != SETUPFILE && type >= -1 ) Error1(
"LocateFile: Cannot find file",*name);
665 int newstr = stream->previous, sgn;
666 UBYTE *t, numbuf[24];
669 M_free(stream->
FoldName,
"stream->FoldName");
672 if ( stream->type == FILESTREAM || stream->type == REVERSEFILESTREAM ) {
673 CloseFile(stream->handle);
674 if ( stream->
buffer != 0 ) M_free(stream->
buffer,
"name of input stream");
678 else if ( stream->type == PIPESTREAM ) {
679 RWLOCKW(AM.handlelock);
681 if ( PF.me == MASTER )
683 pclose((FILE *)(filelist[stream->handle]));
684 filelist[stream->handle] = 0;
686 UNRWLOCK(AM.handlelock);
688 if ( stream->
buffer != 0 ) {
689 M_free(stream->
buffer,
"pipe buffer");
696#ifdef WITHEXTERNALCHANNEL
697 else if ( stream->type == EXTERNALCHANNELSTREAM ) {
699 RWLOCKW(AM.handlelock);
700 tmpn = (
int *)(filelist[stream->handle]);
701 filelist[stream->handle] = 0;
703 UNRWLOCK(AM.handlelock);
704 M_free(tmpn,
"external channel handle");
708 else if ( stream->type == INPUTSTREAM ) {
710 RWLOCKW(AM.handlelock);
711 f = filelist[stream->handle];
712 filelist[stream->handle] = 0;
714 UNRWLOCK(AM.handlelock);
715 M_free(f,
"input stream handle");
717 else if ( stream->type == PREVARSTREAM && (
718 stream->afterwards == PRERAISEAFTER || stream->afterwards == PRELOWERAFTER ) ) {
719 t = stream->
buffer; x = 0; sgn = 1;
720 while ( *t ==
'-' || *t ==
'+' ) {
721 if ( *t ==
'-' ) sgn = -sgn;
724 if ( FG.cTable[*t] == 1 ) {
725 while ( *t && FG.cTable[*t] == 1 ) x = 10*x + *t++ -
'0';
727 if ( stream->afterwards == PRERAISEAFTER ) x = sgn*x + 1;
734 else if ( stream->type == DOLLARSTREAM && (
735 stream->afterwards == PRERAISEAFTER || stream->afterwards == PRELOWERAFTER ) ) {
736 if ( stream->afterwards == PRERAISEAFTER ) x = 1;
738 DollarRaiseLow(stream->
pname,x);
739 if ( stream->
buffer ) M_free(stream->
buffer,
"stream->buffer");
742 else if ( stream->type == PRECALCSTREAM || stream->type == DOLLARSTREAM ) {
743 if ( stream->
buffer ) M_free(stream->
buffer,
"stream->buffer");
746 if ( stream->
name && stream->type != PREVARSTREAM
747 && stream->type != PREREADSTREAM && stream->type != PREREADSTREAM2 && stream->type != PREREADSTREAM3
748 && stream->type != PRECALCSTREAM && stream->type != DOLLARSTREAM ) {
749 M_free(stream->
name,
"stream->name");
753 AC.NoShowInput = stream->previousNoShowInput;
759 if ( stream->prevars >= 0 ) {
760 while ( NumPre > stream->prevars ) {
762 M_free(PreVar[NumPre].name,
"PreVar[NumPre].name");
763 PreVar[NumPre].name = PreVar[NumPre].value = 0;
766 if ( stream->type == PREVARSTREAM ) {
767 AP.AllowDelay = stream->olddelay;
768 ClearMacro(stream->
pname);
769 M_free(stream->
pname,
"stream->pname");
771 else if ( stream->type == DOLLARSTREAM ) {
772 M_free(stream->
pname,
"stream->pname");
775 if ( newstr >= 0 )
return(AC.Streams + newstr);
784STREAM *CreateStream(UBYTE *where)
789 if ( AC.NumStreams >= AC.MaxNumStreams ) {
790 if ( AC.MaxNumStreams == 0 ) numnewstreams = 10;
791 else numnewstreams = 2*AC.MaxNumStreams;
792 newstreams = (
STREAM *)Malloc1(
sizeof(
STREAM)*(numnewstreams+1),
"CreateStream");
793 if ( AC.MaxNumStreams > 0 ) {
794 offset = AC.CurrentStream - AC.Streams;
795 for ( i = 0; i < AC.MaxNumStreams; i++ ) {
796 newstreams[i] = AC.Streams[i];
798 AC.CurrentStream = newstreams + offset;
800 else newstreams[0].previous = -1;
801 AC.MaxNumStreams = numnewstreams;
802 if ( AC.Streams ) M_free(AC.Streams,(
char *)where);
803 AC.Streams = newstreams;
805 newstreams = AC.Streams+AC.NumStreams++;
806 newstreams->
name = 0;
815LONG GetStreamPosition(
STREAM *stream)
817 return(stream->bufferposition + ((LONG)stream->
pointer-(LONG)stream->
buffer));
825void PositionStream(
STREAM *stream, LONG position)
828 if ( position >= stream->bufferposition
829 && position < stream->bufferposition + stream->inbuffer ) {
830 stream->
pointer = stream->
buffer + (position-stream->bufferposition);
832 else if ( stream->type == FILESTREAM ) {
833 SETBASEPOSITION(scrpos,position);
834 SeekFile(stream->handle,&scrpos,SEEK_SET);
835 stream->inbuffer = ReadFile(stream->handle,stream->
buffer,stream->buffersize);
837 stream->
top = stream->
buffer + stream->inbuffer;
838 stream->bufferposition = position;
839 stream->fileposition = position + stream->inbuffer;
840 stream->isnextchar = 0;
843 Error0(
"Illegal position for stream");
857int ReverseStatements(
STREAM *stream)
859 UBYTE *spare = (UBYTE *)Malloc1((stream->inbuffer+1)*
sizeof(UBYTE),
"Reverse copy");
860 UBYTE *top = stream->
buffer + stream->inbuffer, *in, *s, *ss, *out;
861 out = spare+stream->inbuffer+1;
865 if ( *s == AP.ComChar ) {
868 if ( s == top ) { *--out =
'\n';
break; }
872irrend: MesPrint(
"@Irregular end of reverse include file.");
876 else if ( *s ==
'\n' ) {
878 while ( ss > in ) *--out = *--ss;
880 if ( out[0] == AP.ComChar && ss+6 < s && out[3] ==
'#' ) {
884 if ( out[4] ==
'[' ) out[4] =
']';
885 else if ( out[4] ==
']' ) out[4] =
'[';
893 while ( s < top && ( *s ==
' ' || *s ==
'\t' ) ) s++;
911 while ( s < top && ( *s ==
' ' || *s ==
'\t' ) ) s++;
912 while ( s < top && *s ==
'\n' ) s++;
913 if ( s >= top && s[-1] !=
'\n' ) *s++ =
'\n';
915 while ( ss > in ) *--out = *--ss;
919 else if ( *s ==
'"' ) {
922 if ( *s ==
'"' )
break;
923 if ( *s ==
'\\' ) { s++; }
926 if ( s >= top )
goto irrend;
928 else if ( *s ==
'\\' ) {
930 if ( s >= top )
goto irrend;
935 if ( s >= top && s[-1] !=
'\n' ) *s++ =
'\n';
937 while ( ss > in ) *--out = *--ss;
941 if ( out == spare ) stream->inbuffer++;
942 if ( out > spare+1 ) {
943 MesPrint(
"@Internal error in #reverseinclude instruction.");
946 memcpy((
void *)(stream->
buffer),(
void *)out,(
size_t)(stream->inbuffer*
sizeof(UBYTE)));
947 M_free(spare,
"Reverse copy");
960 int i = CreateHandle();
961 filelist[i] = Ustdout;
966 AR.Fscr[0].handle = -1;
967 AR.Fscr[1].handle = -1;
968 AR.Fscr[2].handle = -1;
969 AR.FoStage4[0].handle = -1;
970 AR.FoStage4[1].handle = -1;
971 AR.infile = &(AR.Fscr[0]);
972 AR.outfile = &(AR.Fscr[1]);
973 AR.hidefile = &(AR.Fscr[2]);
974 AR.StoreData.Handle = -1;
977 AC.MaxNumStreams = 0;
985int OpenFile(
char *name)
990 if ( ( f = Uopen(name,
"rb") ) == 0 )
return(-1);
993 RWLOCKW(AM.handlelock);
995 UNRWLOCK(AM.handlelock);
1004int OpenAddFile(
char *name)
1009 if ( ( f = Uopen(name,
"a+b") ) == 0 )
return(-1);
1012 RWLOCKW(AM.handlelock);
1014 UNRWLOCK(AM.handlelock);
1015 TELLFILE(i,&scrpos);
1016 SeekFile(i,&scrpos,SEEK_SET);
1025int ReOpenFile(
char *name)
1030 if ( ( f = Uopen(name,
"r+b") ) == 0 )
return(-1);
1032 RWLOCKW(AM.handlelock);
1034 UNRWLOCK(AM.handlelock);
1035 TELLFILE(i,&scrpos);
1036 SeekFile(i,&scrpos,SEEK_SET);
1045int CreateFile(
char *name)
1049 if ( ( f = Uopen(name,
"w+b") ) == 0 )
return(-1);
1051 RWLOCKW(AM.handlelock);
1053 UNRWLOCK(AM.handlelock);
1062int CreateLogFile(
char *name)
1066 if ( ( f = Uopen(name,
"w+b") ) == 0 )
return(-1);
1069 RWLOCKW(AM.handlelock);
1071 UNRWLOCK(AM.handlelock);
1080void CloseFile(
int handle)
1082 if ( handle >= 0 ) {
1084 RWLOCKW(AM.handlelock);
1085 f = filelist[handle];
1086 filelist[handle] = 0;
1088 UNRWLOCK(AM.handlelock);
1105 #define COPYFILEBUFSIZE 40960L
1107 size_t countin, countout, sumcount;
1108 char *buffer = NULL;
1110 sumcount = (AM.S0->LargeSize+AM.S0->SmallEsize)*
sizeof(WORD);
1111 if ( sumcount <= COPYFILEBUFSIZE ) {
1112 sumcount = COPYFILEBUFSIZE;
1113 buffer = (
char*)Malloc1(sumcount,
"file copy buffer");
1116 buffer = (
char *)(AM.S0->lBuffer);
1119 in = fopen(source,
"rb");
1121 perror(
"CopyFile: ");
1124 out = fopen(dest,
"wb");
1125 if ( out == NULL ) {
1126 perror(
"CopyFile: ");
1130 while ( !feof(in) ) {
1131 countin = fread(buffer, 1, sumcount, in);
1132 if ( countin != sumcount ) {
1134 perror(
"CopyFile: ");
1138 countout = fwrite(buffer, 1, countin, out);
1139 if ( countin != countout ) {
1140 perror(
"CopyFile: ");
1147 if ( sumcount <= COPYFILEBUFSIZE ) {
1148 M_free(buffer,
"file copy buffer");
1164int CreateHandle(
void)
1168 RWLOCKW(AM.handlelock);
1170 if ( filelistsize == 0 ) {
1172 filelist = (FILES **)Malloc1(
sizeof(FILES *)*filelistsize,
"file handle");
1173 for ( j = 0; j < filelistsize; j++ ) filelist[j] = 0;
1177 else if ( numinfilelist >= filelistsize ) {
1178 void **fl = (
void **)filelist;
1180 if ( DoubleList((
void ***)(&fl),&filelistsize,(
int)
sizeof(FILES *),
1181 "list of open files") != 0 ) Terminate(-1);
1182 filelist = (FILES **)fl;
1183 for ( j = i; j < filelistsize; j++ ) filelist[j] = 0;
1184 numinfilelist = i + 1;
1188 for ( j = 0; j < filelistsize; j++ ) {
1189 if ( filelist[j] == 0 ) { i = j;
break; }
1193 filelist[i] = (FILES *)(filelist);
1199 if ( numinfilelist > MAX_OPEN_FILES ) {
1201 UNRWLOCK(AM.handlelock);
1203 MesPrint(
"More than %d open files",MAX_OPEN_FILES);
1204 Error0(
"System limit. This limit is not due to FORM!");
1208 UNRWLOCK(AM.handlelock);
1219LONG ReadFile(
int handle, UBYTE *buffer, LONG size)
1226 RWLOCKR(AM.handlelock);
1227 f = filelist[handle];
1228 UNRWLOCK(AM.handlelock);
1232 r = Uread(b,1,size,f);
1233 if ( r < 0 )
return(r);
1234 if ( r == 0 )
return(inbuf);
1236 if ( r == size )
return(inbuf);
1237 if ( r > size )
return(-1);
1273 WORD *b = (WORD *)buffer, *t;
1275 if ( fi->handle < 0 ) {
1276 fi->POfill = (WORD *)((UBYTE *)(fi->PObuffer) + BASEPOSITION(*pos));
1278 while ( size > 0 && fi->POfill < fi->POfull ) { *b++ = *t++; size--; }
1281 if ( ISLESSPOS(*pos,fi->POposition) || ISGEPOSINC(*pos,fi->POposition,
1282 ((UBYTE *)(fi->POfull)-(UBYTE *)(fi->PObuffer))) ) {
1287 fi->POposition = *pos;
1288 LOCK(AS.inputslock);
1289 SeekFile(fi->handle,pos,SEEK_SET);
1290 retval = ReadFile(fi->handle,(UBYTE *)(fi->PObuffer),fi->POsize);
1291 UNLOCK(AS.inputslock);
1292 fi->POfull = fi->PObuffer+retval/
sizeof(WORD);
1293 fi->POfill = fi->PObuffer;
1294 if ( fi != AR.hidefile ) AR.InInBuf = retval/
sizeof(WORD);
1295 else AR.InHiBuf = retval/
sizeof(WORD);
1298 fi->POfill = (WORD *)((UBYTE *)(fi->PObuffer) + DIFBASE(*pos,fi->POposition));
1300 if ( fi->POfill + size <= fi->POfull ) {
1302 while ( size > 0 ) { *b++ = *t++; size--; }
1306 i = fi->POfull - fi->POfill; t = fi->POfill;
1307 if ( i > size ) i = size;
1309 while ( --i >= 0 ) *b++ = *t++;
1310 if ( size == 0 )
break;
1311 ADDPOS(fi->POposition,(UBYTE *)(fi->POfull)-(UBYTE *)(fi->PObuffer));
1312 LOCK(AS.inputslock);
1313 SeekFile(fi->handle,&(fi->POposition),SEEK_SET);
1314 retval = ReadFile(fi->handle,(UBYTE *)(fi->PObuffer),fi->POsize);
1315 UNLOCK(AS.inputslock);
1316 fi->POfull = fi->PObuffer+retval/
sizeof(WORD);
1317 fi->POfill = fi->PObuffer;
1318 if ( fi != AR.hidefile ) AR.InInBuf = retval/
sizeof(WORD);
1319 else AR.InHiBuf = retval/
sizeof(WORD);
1320 if ( retval == 0 ) { t = fi->POfill;
break; }
1324 retval = (UBYTE *)b - buffer;
1326 ADDPOS(*pos,retval);
1335LONG WriteFileToFile(
int handle, UBYTE *buffer, LONG size)
1338 LONG retval, totalwritten = 0, stilltowrite;
1339 RWLOCKR(AM.handlelock);
1340 f = filelist[handle];
1341 UNRWLOCK(AM.handlelock);
1342 while ( totalwritten < size ) {
1343 stilltowrite = size - totalwritten;
1347 retval = Uwrite((
char *)buffer+totalwritten,1,stilltowrite,f);
1348 if ( retval < 0 )
return(retval);
1349 if ( retval == 0 )
return(totalwritten);
1350 totalwritten += retval;
1355 return(totalwritten);
1359WRITEFILE WriteFile = &WriteFileToFile;
1373void SeekFile(
int handle,
POSITION *offset,
int origin)
1376 RWLOCKR(AM.handlelock);
1377 f = filelist[handle];
1378 UNRWLOCK(AM.handlelock);
1382 if ( origin == SEEK_SET ) {
1383 Useek(f,BASEPOSITION(*offset),origin);
1384 SETBASEPOSITION(*offset,(Utell(f)));
1387 else if ( origin == SEEK_END ) {
1390 SETBASEPOSITION(*offset,(Utell(f)));
1398LONG TellFile(
int handle)
1401 TELLFILE(handle,&pos);
1405 return(BASEPOSITION(pos));
1408void TELLFILE(
int handle,
POSITION *position)
1411 RWLOCKR(AM.handlelock);
1412 f = filelist[handle];
1413 UNRWLOCK(AM.handlelock);
1414 SETBASEPOSITION(*position,(Utell(f)));
1422void FlushFile(
int handle)
1425 RWLOCKR(AM.handlelock);
1426 f = filelist[handle];
1427 UNRWLOCK(AM.handlelock);
1436int GetPosFile(
int handle, fpos_t *pospointer)
1439 RWLOCKR(AM.handlelock);
1440 f = filelist[handle];
1441 UNRWLOCK(AM.handlelock);
1442 return(Ugetpos(f,pospointer));
1450int SetPosFile(
int handle, fpos_t *pospointer)
1453 RWLOCKR(AM.handlelock);
1454 f = filelist[handle];
1455 UNRWLOCK(AM.handlelock);
1456 return(Usetpos(f,(fpos_t *)pospointer));
1470void SynchFile(
int handle)
1473 if ( handle >= 0 ) {
1474 RWLOCKR(AM.handlelock);
1475 f = filelist[handle];
1476 UNRWLOCK(AM.handlelock);
1492void TruncateFile(
int handle)
1495 if ( handle >= 0 ) {
1496 RWLOCKR(AM.handlelock);
1497 f = filelist[handle];
1498 UNRWLOCK(AM.handlelock);
1512int GetChannel(
char *name,
int mode)
1517 for ( i = 0; i < NumOutputChannels; i++ ) {
1518 if ( channels[i].name == 0 )
continue;
1519 if ( StrCmp((UBYTE *)name,(UBYTE *)(channels[i].name)) == 0 )
return(channels[i].handle);
1522 MesPrint(
"&File %s in print statement is not open",name);
1523 MesPrint(
" You should open it first with a #write or #append instruction");
1526 for ( i = 0; i < NumOutputChannels; i++ ) {
1527 if ( channels[i].name == 0 )
break;
1529 if ( i < NumOutputChannels ) { ch = &(channels[i]); }
1530 else { ch = (
CHANNEL *)FromList(&AC.ChannelList); }
1531 ch->
name = (
char *)strDup1((UBYTE *)name,
"name of channel");
1532 ch->
handle = CreateFile(name);
1533 RWLOCKR(AM.handlelock);
1534 f = filelist[ch->
handle];
1535 UNRWLOCK(AM.handlelock);
1548int GetAppendChannel(
char *name)
1553 for ( i = 0; i < NumOutputChannels; i++ ) {
1554 if ( channels[i].name == 0 )
continue;
1555 if ( StrCmp((UBYTE *)name,(UBYTE *)(channels[i].name)) == 0 )
return(channels[i].handle);
1557 for ( i = 0; i < NumOutputChannels; i++ ) {
1558 if ( channels[i].name == 0 )
break;
1560 if ( i < NumOutputChannels ) { ch = &(channels[i]); }
1561 else { ch = (
CHANNEL *)FromList(&AC.ChannelList); }
1562 ch->
name = (
char *)strDup1((UBYTE *)name,
"name of channel");
1563 ch->
handle = OpenAddFile(name);
1564 RWLOCKR(AM.handlelock);
1565 f = filelist[ch->
handle];
1566 UNRWLOCK(AM.handlelock);
1578int CloseChannel(
char *name)
1581 for ( i = 0; i < NumOutputChannels; i++ ) {
1582 if ( channels[i].name == 0 )
continue;
1583 if ( channels[i].name[0] == 0 )
continue;
1584 if ( StrCmp((UBYTE *)name,(UBYTE *)(channels[i].name)) == 0 ) {
1585 CloseFile(channels[i].handle);
1586 M_free(channels[i].name,
"CloseChannel");
1587 channels[i].name = 0;
1612void UpdateMaxSize(
void)
1620 if ( PF.me != MASTER )
return;
1623 if ( AM.PrintTotalSize ) {
1628 scr = AB[0]->R.Fscr;
1632 for ( i = 0; i <=2; i++ ) {
1633 if ( scr[i].handle < 0 ) {
1634 SETBASEPOSITION(position,(scr[i].POfull-scr[i].PObuffer)*
sizeof(WORD));
1637 position = scr[i].filesize;
1639 ADD2POS(sumsize,position);
1648 for ( j = 0; j < AM.totalnumberofthreads; j++ ) {
1650 if ( AT.SS && AT.SS->file.handle >= 0 ) {
1651 position = AT.SS->file.filesize;
1657 ADD2POS(sumsize,position);
1659 if ( AR.FoStage4[0].handle >= 0 ) {
1660 position = AR.FoStage4[0].filesize;
1661 ADD2POS(sumsize,position);
1666 if ( AT.SS && AT.SS->file.handle >= 0 ) {
1667 position = AT.SS->file.filesize;
1668 ADD2POS(sumsize,position);
1670 if ( AR.FoStage4[0].handle >= 0 ) {
1671 position = AR.FoStage4[0].filesize;
1672 ADD2POS(sumsize,position);
1678 ADD2POS(sumsize,AC.StoreFileSize);
1682 if ( ISLESSPOS(AS.MaxExprSize,sumsize) ) {
1684 LOCK(AS.MaxExprSizeLock);
1685 if ( ISLESSPOS(AS.MaxExprSize,sumsize) ) AS.MaxExprSize = sumsize;
1686 UNLOCK(AS.MaxExprSizeLock);
1688 AS.MaxExprSize = sumsize;
1702int StrCmp(UBYTE *s1, UBYTE *s2)
1704 while ( *s1 && *s1 == *s2 ) { s1++; s2++; }
1705 return((
int)*s1-(
int)*s2);
1713int StrICmp(UBYTE *s1, UBYTE *s2)
1715 while ( *s1 && tolower(*s1) == tolower(*s2) ) { s1++; s2++; }
1716 return((
int)tolower(*s1)-(
int)tolower(*s2));
1724int StrHICmp(UBYTE *s1, UBYTE *s2)
1726 while ( *s1 && tolower(*s1) == *s2 ) { s1++; s2++; }
1727 return((
int)tolower(*s1)-(
int)(*s2));
1735int StrICont(UBYTE *s1, UBYTE *s2)
1737 while ( *s1 && tolower(*s1) == tolower(*s2) ) { s1++; s2++; }
1738 if ( *s1 == 0 )
return(0);
1739 return((
int)tolower(*s1)-(
int)tolower(*s2));
1747int CmpArray(WORD *t1, WORD *t2, WORD n)
1750 for ( i = 0; i < n; i++ ) {
1751 if ( ( x = (
int)(t1[i]-t2[i]) ) != 0 )
return(x);
1761int ConWord(UBYTE *s1, UBYTE *s2)
1763 while ( *s1 && ( tolower(*s1) == tolower(*s2) ) ) { s1++; s2++; }
1764 if ( *s1 == 0 )
return(1);
1776 while ( *s ) { s++; i++; }
1785void NumToStr(UBYTE *s, LONG x)
1790 if ( x < 0 ) { *s++ =
'-'; xx = -x; }
1793 *t++ = xx % 10 +
'0';
1796 while ( t > str ) *s++ = *--t;
1809void WriteString(
int type, UBYTE *str,
int num)
1813 if ( num > 0 && str[num-1] == 0 ) { num--; }
1814 else if ( num <= 0 || str[num-1] != LINEFEED ) {
1815 AddLineFeed(str,num);
1818 if(type == EXTERNALCHANNELOUT){
1819 if(WriteFile(0,str,num) != num) error = 1;
1822 if ( AM.silent == 0 || type == ERROROUT ) {
1823 if ( type == INPUTOUT ) {
1824 if ( !AM.FileOnlyFlag && WriteFile(AM.StdOut,(UBYTE *)
" ",4) != 4 ) error = 1;
1825 if ( AC.LogHandle >= 0 && WriteFile(AC.LogHandle,(UBYTE *)
" ",4) != 4 ) error = 1;
1827 if ( !AM.FileOnlyFlag && WriteFile(AM.StdOut,str,num) != num ) error = 1;
1828 if ( AC.LogHandle >= 0 && WriteFile(AC.LogHandle,str,num) != num ) error = 1;
1830 if ( error ) Terminate(-1);
1843void WriteUnfinString(
int type, UBYTE *str,
int num)
1848 if(type == EXTERNALCHANNELOUT){
1849 if(WriteFile(0,str,num) != num) error = 1;
1852 if ( AM.silent == 0 || type == ERROROUT ) {
1853 if ( type == INPUTOUT ) {
1854 if ( !AM.FileOnlyFlag && WriteFile(AM.StdOut,(UBYTE *)
" ",4) != 4 ) error = 1;
1855 if ( AC.LogHandle >= 0 && WriteFile(AC.LogHandle,(UBYTE *)
" ",4) != 4 ) error = 1;
1857 if ( !AM.FileOnlyFlag && WriteFile(AM.StdOut,str,num) != num ) error = 1;
1858 if ( AC.LogHandle >= 0 && WriteFile(AC.LogHandle,str,num) != num ) error = 1;
1860 if ( error ) Terminate(-1);
1868UBYTE *AddToString(UBYTE *outstring, UBYTE *extrastring,
int par)
1870 UBYTE *s = extrastring, *t, *newstring;
1872 while ( *s ) { s++; }
1874 if ( outstring == 0 ) {
1876 t = outstring = (UBYTE *)Malloc1(n+1,
"AddToString");
1885 t = newstring = (UBYTE *)Malloc1(n+nn+2,
"AddToString");
1888 if ( par == 1 ) *t++ =
',';
1892 M_free(outstring,
"AddToString");
1906UBYTE *strDup1(UBYTE *instring,
char *ifwrong)
1908 UBYTE *s = instring, *to;
1910 to = s = (UBYTE *)Malloc1((s-instring)+1,ifwrong);
1911 while ( *instring ) *to++ = *instring++;
1935 while ( ( c = (UBYTE)(FG.cTable[*s]) ) == 0 || c == 1 ) s++;
1958 while ( *s && ( c = (UBYTE)(FG.cTable[*s]) ) != 0 && c != 1 ) s++;
1979 if ( *s ==
',' && level == 0 )
return(s);
1980 if ( *s ==
'(' ) level++;
1981 else if ( *s ==
')' ) { level--;
if ( level < 0 ) level = 0; }
1982 else if ( *s ==
'[' ) {
1985 else if ( *s ==
'{' ) {
2003WORD ReadSnum(UBYTE **p)
2008 if ( FG.cTable[*s] == 1 ) {
2010 x = ( x << 3 ) + ( x << 1 ) + ( *s++ -
'0' );
2011 if ( x > MAXPOSITIVE )
return(-1);
2012 }
while ( FG.cTable[*s] == 1 );
2027UBYTE *NumCopy(WORD y, UBYTE *to)
2037 do { *s++ = (UBYTE)((x % 10)+
'0'); i++; }
while ( ( x /= 10 ) != 0 );
2041 i = to[j]; to[j] = s[-j]; s[-j] = (UBYTE)i; j--;
2054char *LongCopy(LONG y,
char *to)
2064 do { *s++ = (x % 10)+
'0'; i++; }
while ( ( x /= 10 ) != 0 );
2068 i = to[j]; to[j] = s[-j]; s[-j] = (char)i; j--;
2081char *LongLongCopy(off_t *y,
char *to)
2094 if ( x < 0 ) { x = -x; *to++ =
'-'; }
2096 do { *s++ = (x % 10)+
'0'; i++; }
while ( ( x /= 10 ) != 0 );
2100 i = to[j]; to[j] = s[-j]; s[-j] = (char)i; j--;
2112#if defined(ANSI) || defined(mBSD)
2114static char notime[] =
"";
2117UBYTE *MakeDate(
void)
2119#if defined(ANSI) || defined(mBSD)
2122 return((UBYTE *)ctime(&tp));
2124 return((UBYTE *)notime);
2137 case 0:
return(set->bit_0);
2138 case 1:
return(set->bit_1);
2139 case 2:
return(set->bit_2);
2140 case 3:
return(set->bit_3);
2141 case 4:
return(set->bit_4);
2142 case 5:
return(set->bit_5);
2143 case 6:
return(set->bit_6);
2144 case 7:
return(set->bit_7);
2158 case 0: set->bit_0=1;
break;
2159 case 1: set->bit_1=1;
break;
2160 case 2: set->bit_2=1;
break;
2161 case 3: set->bit_3=1;
break;
2162 case 4: set->bit_4=1;
break;
2163 case 5: set->bit_5=1;
break;
2164 case 6: set->bit_6=1;
break;
2165 case 7: set->bit_7=1;
break;
2179 case 0: set->bit_0=0;
break;
2180 case 1: set->bit_1=0;
break;
2181 case 2: set->bit_2=0;
break;
2182 case 3: set->bit_3=0;
break;
2183 case 4: set->bit_4=0;
break;
2184 case 5: set->bit_5=0;
break;
2185 case 6: set->bit_6=0;
break;
2186 case 7: set->bit_7=0;
break;
2203 case 0: set->bit_0=(set1->bit_0&&(!set2->bit_0));
break;
2204 case 1: set->bit_1=(set1->bit_1&&(!set2->bit_1));
break;
2205 case 2: set->bit_2=(set1->bit_2&&(!set2->bit_2));
break;
2206 case 3: set->bit_3=(set1->bit_3&&(!set2->bit_3));
break;
2207 case 4: set->bit_4=(set1->bit_4&&(!set2->bit_4));
break;
2208 case 5: set->bit_5=(set1->bit_5&&(!set2->bit_5));
break;
2209 case 6: set->bit_6=(set1->bit_6&&(!set2->bit_6));
break;
2210 case 7: set->bit_7=(set1->bit_7&&(!set2->bit_7));
break;
2211 case 8: set++;set1++;set2++;
2225 if ( mprotectInit() ) exit(0);
2243void *Malloc1(LONG size,
const char *messageifwrong)
2251 MesPrint(
"%wAsking for 0 bytes in Malloc1");
2257 if ( ( size & 7 ) != 0 ) { size = size - ( size&7 ) + 8; }
2261 mem = (
void *)M_alloc(size);
2264 MLOCK(ErrorMessageLock);
2266 MesPrint(
"Attempted to allocate %l bytes.", size);
2267 MesPrint(
"@No memory while allocating %s", (UBYTE *)messageifwrong);
2269 MUNLOCK(ErrorMessageLock);
2276 mallocsizes[nummalloclist] = size;
2277 mallocstrings[nummalloclist] = (
char *)messageifwrong;
2278 malloclist[nummalloclist++] = mem;
2279 if ( AC.MemDebugFlag && filelist ) MesPrint(
"%wMem1 at 0x%x: %l bytes. %s",mem,size,messageifwrong);
2281 int i = nummalloclist-1;
2282 while ( --i >= 0 ) {
2283 if ( (
char *)mem < (((
char *)malloclist[i]) + mallocsizes[i])
2284 && (
char *)(malloclist[i]) < ((
char *)mem + size) ) {
2285 if ( filelist ) MesPrint(
"This memory overlaps with the block at 0x%x"
2291#ifdef MALLOCDEBUGOUTPUT
2292 printf (
"Malloc1: %s, allocated %li bytes at %.8lx\n",messageifwrong,size,(
unsigned long)mem);
2298 for ( i = 0; i < (int)BANNER; i++ ) { *t++ = FILLVALUE; *--u = FILLVALUE; }
2319void M_free(
void *x,
const char *where)
2322 char *t = (
char *)x;
2325 x = (
void *)(((
char *)x)-BANNER);
2327 if ( AC.MemDebugFlag ) MesPrint(
"%wFreeing 0x%x: %s",x,where);
2329 for ( i = nummalloclist-1; i >= 0; i-- ) {
2330 if ( x == malloclist[i] ) {
2331 size = mallocsizes[i];
2332 for ( j = i+1; j < nummalloclist; j++ ) {
2333 malloclist[j-1] = malloclist[j];
2334 mallocsizes[j-1] = mallocsizes[j];
2335 mallocstrings[j-1] = mallocstrings[j];
2342 unsigned int xx = ((ULONG)x);
2343 printf(
"Error returning non-allocated address: 0x%x from %s\n"
2350 for ( k = 0, j = 0; k < (int)BANNER; k++ ) {
2351 if ( *--t != FILLVALUE ) j++;
2354 LONG *tt = (LONG *)x;
2355 MesPrint(
"%w!!!!! Banner has been written in !!!!!: %x %x %x %x",
2356 tt[0],tt[1],tt[2],tt[3]);
2359 for ( k = 0, j = 0; k < (int)BANNER; k++ ) {
2360 if ( *--t != FILLVALUE ) j++;
2363 LONG *tt = (LONG *)x;
2364 MesPrint(
"%w!!!!! Tail has been written in !!!!!: %x %x %x %x",
2365 tt[0],tt[1],tt[2],tt[3]);
2378#ifdef MALLOCDEBUGOUTPUT
2379 printf (
"M_free: %s, memory freed at %.8lx\n",where,(
unsigned long)x);
2384 mprotectFree((
void *)x);
2398void M_check1() { MesPrint(
"Checking Malloc"); M_check(); }
2402 int i,j,k,error = 0;
2405 for ( i = 0; i < nummalloclist; i++ ) {
2406 t = (
char *)(malloclist[i]);
2407 for ( k = 0, j = 0; k < (int)BANNER; k++ ) {
2408 if ( *t++ != FILLVALUE ) j++;
2411 tt = (LONG *)(malloclist[i]);
2412 MesPrint(
"%w!!!!! Banner %d (%s) has been written in !!!!!: %x %x %x %x",
2413 i,mallocstrings[i],tt[0],tt[1],tt[2],tt[3]);
2414 tt[0] = tt[1] = tt[2] = tt[3] = 0;
2417 t = (
char *)(malloclist[i]) + mallocsizes[i];
2418 for ( k = 0, j = 0; k < (int)BANNER; k++ ) {
2419 if ( *--t != FILLVALUE ) j++;
2423 MesPrint(
"%w!!!!! Tail %d (%s) has been written in !!!!!: %x %x %x %x",
2424 i,mallocstrings[i],tt[0],tt[1],tt[2],tt[3]);
2425 tt[0] = tt[1] = tt[2] = tt[3] = 0;
2428 if ( ( mallocstrings[i][0] ==
' ' ) || ( mallocstrings[i][0] ==
'#' ) ) {
2429 MesPrint(
"%w!!!!! Funny mallocstring");
2444 MesPrint(
"We have the following memory allocations left:");
2445 for ( i = 0; i < nummalloclist; i++ ) {
2446 MesPrint(
"0x%x: %l bytes. number %d: '%s'",malloclist[i],mallocsizes[i],i,mallocstrings[i]);
2452void M_check1(
void) {}
2453void M_print(
void) {}
2483#define TERMMEMSTARTNUM 16
2484#define TERMEXTRAWORDS 10
2486void TermMallocAddMemory(PHEAD0)
2491 else extra = AT.TermMemMax;
2492 if ( AT.TermMemHeap ) M_free(AT.TermMemHeap,
"TermMalloc");
2493 newbufs = (WORD *)Malloc1(extra*(AM.MaxTer+TERMEXTRAWORDS*
sizeof(WORD)),
"TermMalloc");
2494 AT.TermMemHeap = (WORD **)Malloc1((extra+AT.TermMemMax)*
sizeof(WORD *),
"TermMalloc");
2495 for ( i = 0; i < extra; i++ ) {
2496 AT.TermMemHeap[i] = newbufs + i*(AM.MaxTer/
sizeof(WORD)+TERMEXTRAWORDS);
2498#ifdef TERMMALLOCDEBUG
2499 DebugHeap2 = (WORD **)Malloc1((extra+AT.TermMemMax)*
sizeof(WORD *),
"TermMalloc");
2500 for ( i = 0; i < AT.TermMemMax; i++ ) { DebugHeap2[i] = DebugHeap1[i]; }
2501 for ( i = 0; i < extra; i++ ) {
2502 DebugHeap2[i+AT.TermMemMax] = newbufs + i*(AM.MaxTer/
sizeof(WORD)+TERMEXTRAWORDS);
2504 if ( DebugHeap1 ) M_free(DebugHeap1,
"TermMalloc");
2505 DebugHeap1 = DebugHeap2;
2507 AT.TermMemTop = extra;
2508 AT.TermMemMax += extra;
2509#ifdef TERMMALLOCDEBUG
2510 MesPrint(
"AT.TermMemMax is now %l",AT.TermMemMax);
2516WORD *TermMalloc2(PHEAD
char *text)
2518 if ( AT.TermMemTop <= 0 ) TermMallocAddMemory(BHEAD0);
2520#ifdef TERMMALLOCDEBUG
2521 MesPrint(
"TermMalloc: %s, %d",text,(AT.TermMemMax-AT.TermMemTop));
2524#ifdef MALLOCDEBUGOUTPUT
2525 MesPrint(
"TermMalloc: %s, %l/%l (%x)",text,AT.TermMemTop,AT.TermMemMax,AT.TermMemHeap[AT.TermMemTop-1]);
2529 return(AT.TermMemHeap[--AT.TermMemTop]);
2532void TermFree2(PHEAD WORD *TermMem,
char *text)
2534#ifdef TERMMALLOCDEBUG
2538 for ( i = 0; i < AT.TermMemMax; i++ ) {
2539 if ( TermMem == DebugHeap1[i] )
break;
2541 if ( i >= AT.TermMemMax ) {
2542 MesPrint(
" ERROR: TermFree called with an address not given by TermMalloc.");
2547 AT.TermMemHeap[AT.TermMemTop++] = TermMem;
2549#ifdef TERMMALLOCDEBUG
2550 MesPrint(
"TermFree: %s, %d",text,(AT.TermMemMax-AT.TermMemTop));
2552#ifdef MALLOCDEBUGOUTPUT
2553 MesPrint(
"TermFree: %s, %l/%l (%x)",text,AT.TermMemTop,AT.TermMemMax,TermMem);
2583#define NUMBERMEMSTARTNUM 16
2584#define NUMBEREXTRAWORDS 10L
2586#ifdef TERMMALLOCDEBUG
2587UWORD **DebugHeap3, **DebugHeap4;
2590void NumberMallocAddMemory(PHEAD0)
2596 else extra = AT.NumberMemMax;
2597 if ( AT.NumberMemHeap ) M_free(AT.NumberMemHeap,
"NumberMalloc");
2598 newbufs = (UWORD *)Malloc1(extra*(AM.MaxTal+NUMBEREXTRAWORDS)*
sizeof(UWORD),
"NumberMalloc");
2599 AT.NumberMemHeap = (UWORD **)Malloc1((extra+AT.NumberMemMax)*
sizeof(UWORD *),
"NumberMalloc");
2600 for ( i = 0; i < extra; i++ ) {
2601 AT.NumberMemHeap[i] = newbufs + i*(LONG)(AM.MaxTal+NUMBEREXTRAWORDS);
2603#ifdef TERMMALLOCDEBUG
2604 DebugHeap4 = (UWORD **)Malloc1((extra+AT.NumberMemMax)*
sizeof(WORD *),
"NumberMalloc");
2605 for ( i = 0; i < AT.NumberMemMax; i++ ) { DebugHeap4[i] = DebugHeap3[i]; }
2606 for ( i = 0; i < extra; i++ ) {
2607 DebugHeap4[i+AT.NumberMemMax] = newbufs + i*(LONG)(AM.MaxTal+NUMBEREXTRAWORDS);
2609 if ( DebugHeap3 ) M_free(DebugHeap3,
"NumberMalloc");
2610 DebugHeap3 = DebugHeap4;
2612 AT.NumberMemTop = extra;
2613 AT.NumberMemMax += extra;
2621UWORD *NumberMalloc2(PHEAD
char *text)
2623 if ( AT.NumberMemTop <= 0 ) NumberMallocAddMemory(BHEAD text);
2625#ifdef MALLOCDEBUGOUTPUT
2626 if ( (AT.NumberMemMax-AT.NumberMemTop) > 10 )
2627 MesPrint(
"NumberMalloc: %s, %l/%l (%x)",text,AT.NumberMemTop,AT.NumberMemMax,AT.NumberMemHeap[AT.NumberMemTop-1]);
2631 return(AT.NumberMemHeap[--AT.NumberMemTop]);
2634void NumberFree2(PHEAD UWORD *NumberMem,
char *text)
2636#ifdef TERMMALLOCDEBUG
2638 for ( i = 0; i < AT.NumberMemMax; i++ ) {
2639 if ( NumberMem == DebugHeap3[i] )
break;
2641 if ( i >= AT.NumberMemMax ) {
2642 MesPrint(
" ERROR: NumberFree called with an address not given by NumberMalloc.");
2647 AT.NumberMemHeap[AT.NumberMemTop++] = NumberMem;
2649#ifdef MALLOCDEBUGOUTPUT
2650 if ( (AT.NumberMemMax-AT.NumberMemTop) > 10 )
2651 MesPrint(
"NumberFree: %s, %l/%l (%x)",text,AT.NumberMemTop,AT.NumberMemMax,NumberMem);
2664void CacheNumberMallocAddMemory(PHEAD0)
2670 else extra = AT.CacheNumberMemMax;
2671 if ( AT.CacheNumberMemHeap ) M_free(AT.CacheNumberMemHeap,
"NumberMalloc");
2672 newbufs = (UWORD *)Malloc1(extra*(AM.MaxTal+NUMBEREXTRAWORDS)*
sizeof(UWORD),
"CacheNumberMalloc");
2673 AT.CacheNumberMemHeap = (UWORD **)Malloc1((extra+AT.NumberMemMax)*
sizeof(UWORD *),
"CacheNumberMalloc");
2674 for ( i = 0; i < extra; i++ ) {
2675 AT.CacheNumberMemHeap[i] = newbufs + i*(LONG)(AM.MaxTal+NUMBEREXTRAWORDS);
2677 AT.CacheNumberMemTop = extra;
2678 AT.CacheNumberMemMax += extra;
2683UWORD *CacheNumberMalloc2(PHEAD
char *text)
2685 if ( AT.CacheNumberMemTop <= 0 ) CacheNumberMallocAddMemory(BHEAD0);
2687#ifdef MALLOCDEBUGOUTPUT
2688 MesPrint(
"NumberMalloc: %s, %l/%l (%x)",text,AT.NumberMemTop,AT.NumberMemMax,AT.NumberMemHeap[AT.NumberMemTop-1]);
2692 return(AT.CacheNumberMemHeap[--AT.CacheNumberMemTop]);
2695void CacheNumberFree2(PHEAD UWORD *NumberMem,
char *text)
2698 AT.CacheNumberMemHeap[AT.CacheNumberMemTop++] = NumberMem;
2700#ifdef MALLOCDEBUGOUTPUT
2701 MesPrint(
"NumberFree: %s, %l/%l (%x)",text,AT.NumberMemTop,AT.NumberMemMax,NumberMem);
2716void *FromList(
LIST *L)
2725 i = ( L->
num * L->
size ) /
sizeof(
int);
2726 old = (
int *)L->
lijst; newL = (
int *)newlist;
2727 while ( --i >= 0 ) *newL++ = *old++;
2728 if ( L->
lijst ) M_free(L->
lijst,
"L->lijst FromList");
2742void *From0List(
LIST *L)
2750 i = ( L->
num * L->
size ) /
sizeof(
int);
2751 old = (
int *)(L->
lijst); newL = (
int *)newlist;
2752 while ( --i >= 0 ) *newL++ = *old++;
2753 i = ( L->
maxnum - L->
num ) /
sizeof(
int);
2754 while ( --i >= 0 ) *newL++ = 0;
2755 if ( L->
lijst ) M_free(L->
lijst,
"L->lijst From0List");
2771void *FromVarList(
LIST *L)
2777 else if ( L->
lijst ) {
2779 if ( L == &(AP.DollarList) ) {
2780 if ( L->
maxnum > MAXDOLLARVARIABLES ) L->
maxnum = MAXDOLLARVARIABLES;
2781 if ( L->
num >= MAXDOLLARVARIABLES ) {
2782 MesPrint(
"!!!More than %l objects in list of $-variables",
2783 MAXDOLLARVARIABLES);
2788 if ( L->
maxnum > MAXVARIABLES ) L->
maxnum = MAXVARIABLES;
2789 if ( L->
num >= MAXVARIABLES ) {
2790 MesPrint(
"!!!More than %l objects in list of variables",
2798 i = ( L->
num * L->
size ) /
sizeof(
int);
2799 old = (
int *)(L->
lijst); newL = (
int *)newlist;
2800 while ( --i >= 0 ) *newL++ = *old++;
2801 if ( L->
lijst ) M_free(L->
lijst,
"L->lijst from VarList");
2805 return( ((
char *)(L->
lijst)) + L->
size * ((L->
num)++) );
2813int DoubleList(
void ***lijst,
int *oldsize,
int objectsize,
char *nameoftype)
2816 LONG i, newsize, fullsize;
2818 static LONG maxlistsize = (LONG)(MAXPOSITIVE);
2819 if ( *lijst == 0 ) {
2820 if ( *oldsize > 0 ) newsize = *oldsize;
2823 else newsize = *oldsize * 2;
2824 if ( newsize > maxlistsize ) {
2825 if ( *oldsize == maxlistsize ) {
2826 MesPrint(
"No memory for extra space in %s",nameoftype);
2829 newsize = maxlistsize;
2831 fullsize = ( newsize * objectsize +
sizeof(
void *)-1 ) & (-
sizeof(
void *));
2832 newlist = (
void **)Malloc1(fullsize,nameoftype);
2834 to = newlist; from = *lijst; i = (*oldsize * objectsize)/
sizeof(
void *);
2841 while ( --i >= 0 ) *to++ = *from++;
2843 if ( *lijst ) M_free(*lijst,
"DoubleLList");
2865int DoubleLList(
void ***lijst, LONG *oldsize,
int objectsize,
char *nameoftype)
2868 LONG i, newsize, fullsize;
2870 static LONG maxlistsize = (LONG)(MAXLONG);
2871 if ( *lijst == 0 ) {
2872 if ( *oldsize > 0 ) newsize = *oldsize;
2875 else newsize = *oldsize * 2;
2876 if ( newsize > maxlistsize ) {
2877 if ( *oldsize == maxlistsize ) {
2878 MesPrint(
"No memory for extra space in %s",nameoftype);
2881 newsize = maxlistsize;
2883 fullsize = ( newsize * objectsize +
sizeof(
void *)-1 ) & (-
sizeof(
void *));
2884 newlist = (
void **)Malloc1(fullsize,nameoftype);
2886 to = newlist; from = *lijst; i = (*oldsize * objectsize)/
sizeof(
void *);
2893 while ( --i >= 0 ) *to++ = *from++;
2895 if ( *lijst ) M_free(*lijst,
"DoubleLList");
2906#define DODOUBLE(x) { x *s, *t, *u; if ( *start ) { \
2907 oldsize = *(x **)stop - *(x **)start; newsize = 2*oldsize; \
2908 t = u = (x *)Malloc1(newsize*sizeof(x),text); s = *(x **)start; \
2909 for ( i = 0; i < oldsize; i++ ) {*t++ = *s++;} M_free(*start,"double"); } \
2910 else { newsize = 100; u = (x *)Malloc1(newsize*sizeof(x),text); } \
2911 *start = (void *)u; *stop = (void *)(u+newsize); }
2913void DoubleBuffer(
void **start,
void **stop,
int size,
char *text)
2915 LONG oldsize, newsize, i;
2916 if ( size ==
sizeof(
char) ) DODOUBLE(
char)
2917 else if ( size ==
sizeof(
short) ) DODOUBLE(
short)
2918 else if ( size ==
sizeof(
int) ) DODOUBLE(
int)
2919 else if ( size ==
sizeof(LONG) ) DODOUBLE(LONG)
2920 else if ( size %
sizeof(
int) == 0 ) DODOUBLE(
int)
2922 MesPrint(
"---Cannot handle doubling buffers of size %d",size);
2932#define DOEXPAND(x) { x *newbuffer, *t, *m; \
2933 t = newbuffer = (x *)Malloc1((newsize+2)*type,"ExpandBuffer"); \
2934 if ( *buffer ) { m = (x *)*buffer; i = *oldsize; \
2935 while ( --i >= 0 ) {*t++ = *m++;} M_free(*buffer,"ExpandBuffer"); \
2936 } *buffer = newbuffer; *oldsize = newsize; }
2938void ExpandBuffer(
void **buffer, LONG *oldsize,
int type)
2941 if ( *oldsize <= 0 ) { newsize = 100; }
2942 else newsize = 2*(*oldsize);
2943 if ( type ==
sizeof(
char) ) DOEXPAND(
char)
2944 else if ( type ==
sizeof(
short) ) DOEXPAND(
short)
2945 else if ( type ==
sizeof(
int) ) DOEXPAND(
int)
2946 else if ( type ==
sizeof(LONG) ) DOEXPAND(LONG)
2949 MesPrint(
"---Cannot handle expanding buffers with objects of size %d",type);
2962LONG iexp(LONG x,
int p)
2967 if ( x == 0 )
return(0);
2968 if ( p == 0 )
return(1);
2969 sign = x < 0 ? -1 : 1;
2970 if ( sign < 0 && ( p & 1 ) == 0 ) sign = 1;
2972 if ( ux == 1 )
return(sign);
2973 if ( p < 0 )
return(0);
2976 if ( ( p & 1 ) != 0 ) y *= ux;
2980 if ( sign < 0 ) y = -y;
2981 return ULongToLong(y);
2993void ToGeneral(WORD *r, WORD *m, WORD par)
2997 else { m[1] = 0; m += ARGHEAD + 1; }
3001 if ( j >= FUNCTION ) { *m++ = j; *m++ = FUNHEAD; FILLFUN(m) }
3004 case SYMBOL: *m++ = j; *m++ = 4; *m++ = *r++; *m++ = 1;
break;
3006 if ( *r > 0 ) { *m++ = *r; *m++ = 1; *m++ = 3; }
3007 else if ( *r == 0 ) { m--; }
3008 else { *m++ = -*r; *m++ = 1; *m++ = -3; }
3015 *m++ = INDEX; *m++ = 3; *m++ = *r++;
3019 *m++ = 1; *m++ = 1; *m++ = k;
3022 if ( !par ) mm[ARGHEAD] = *mm-ARGHEAD;
3037int ToFast(WORD *r, WORD *m)
3040 if ( *r == ARGHEAD ) { *m++ = -SNUMBER; *m++ = 0;
return(1); }
3041 if ( *r != r[ARGHEAD]+ARGHEAD )
return(0);
3044 if ( r[2] != 1 || r[1] <= 0 )
return(0);
3045 *m++ = -SNUMBER; *m = ( r[3] < 0 ) ? -r[1] : r[1];
return(1);
3048 if ( r[i-1] != 1 || r[i-2] != 1 )
return(0);
3050 if ( r[i] == -3 && r[2] == *r-4 && r[2] == 3 && r[1] == INDEX
3051 && r[3] < MINSPEC ) {}
3054 else if ( r[2] != *r - 4 )
return(0);
3056 if ( *r >= FUNCTION ) {
3057 if ( r[1] <= FUNHEAD ) { *m++ = -*r;
return(1); }
3059 else if ( *r == SYMBOL ) {
3060 if ( r[1] == 4 && r[3] == 1 )
3061 { *m++ = -SYMBOL; *m++ = r[2];
return(1); }
3063 else if ( *r == INDEX ) {
3065 if ( r[2] >= MINSPEC ) {
3066 if ( r[2] >= 0 && r[2] < AM.OffsetIndex ) *m++ = -SNUMBER;
3070 if ( r[5] == -3 ) *m++ = -MINVECTOR;
3071 else *m++ = -VECTOR;
3090WORD ToPolyFunGeneral(PHEAD WORD *term)
3092 WORD *t = term+1, *tt, *to, *to1, *termout, *tstop, *tnext;
3093 WORD numarg, i, change = 0;
3094 tstop = term + *term; tstop -= ABS(tstop[-1]);
3095 termout = to = AT.WorkPointer;
3097 while ( t < tstop ) {
3098 if ( *t == AR.PolyFun ) {
3099 tt = t+FUNHEAD; tnext = t + t[1];
3101 while ( tt < tnext ) { numarg++; NEXTARG(tt); }
3102 if ( numarg == 2 ) {
3105 i = FUNHEAD; NCOPY(to,t,i);
3106 while ( tt < tnext ) {
3108 i = *tt; NCOPY(to,tt,i);
3110 else if ( *tt == -SYMBOL ) {
3111 to1[1] += 6+ARGHEAD; to1[2] |= MUSTCLEANPRF; change++;
3112 *to++ = 8+ARGHEAD; *to++ = 0; FILLARG(to);
3113 *to++ = 8; *to++ = SYMBOL; *to++ = 4; *to++ = tt[1];
3114 *to++ = 1; *to++ = 1; *to++ = 1; *to++ = 3;
3117 else if ( *tt == -SNUMBER ) {
3119 to1[1] += 2+ARGHEAD; to1[2] |= MUSTCLEANPRF; change++;
3120 *to++ = 4+ARGHEAD; *to++ = 0; FILLARG(to);
3121 *to++ = 4; *to++ = tt[1]; *to++ = 1; *to++ = 3;
3124 else if ( tt[1] < 0 ) {
3125 to1[1] += 2+ARGHEAD; to1[2] |= MUSTCLEANPRF; change++;
3126 *to++ = 4+ARGHEAD; *to++ = 0; FILLARG(to);
3127 *to++ = 4; *to++ = -tt[1]; *to++ = 1; *to++ = -3;
3131 MLOCK(ErrorMessageLock);
3132 MesPrint(
"Internal error: Zero in PolyRatFun");
3133 MUNLOCK(ErrorMessageLock);
3142 i = t[1]; NCOPY(to,t,i)
3146 while ( t < tt ) *to++ = *t++;
3147 *termout = to - termout;
3148 t = term; i = *termout; tt = termout;
3150 AT.WorkPointer = term + *term;
3165int IsLikeVector(WORD *arg)
3167 WORD *sstop, *t, *tstop;
3169 if ( *arg == -VECTOR || *arg == -INDEX )
return(1);
3170 if ( *arg == -SNUMBER && arg[1] >= 0 && arg[1] < AM.OffsetIndex )
3174 sstop = arg + *arg; arg += ARGHEAD;
3175 while ( arg < sstop ) {
3177 tstop = t - ABS(t[-1]);
3179 while ( arg < tstop ) {
3180 if ( *arg == INDEX )
return(1);
3193int AreArgsEqual(WORD *arg1, WORD *arg2)
3196 if ( *arg2 != *arg1 )
return(0);
3199 while ( --i > 0 ) {
if ( arg1[i] != arg2[i] )
return(0); }
3202 else if ( *arg1 <= -FUNCTION )
return(1);
3203 else if ( arg1[1] == arg2[1] )
return(1);
3212int CompareArgs(WORD *arg1, WORD *arg2)
3216 if ( *arg2 < 0 )
return(-1);
3217 i1 = *arg1-ARGHEAD; arg1 += ARGHEAD;
3218 i2 = *arg2-ARGHEAD; arg2 += ARGHEAD;
3219 while ( i1 > 0 && i2 > 0 ) {
3220 if ( *arg1 != *arg2 )
return((
int)(*arg1)-(
int)(*arg2));
3221 i1--; i2--; arg1++; arg2++;
3225 else if ( *arg2 > 0 )
return(1);
3227 if ( *arg1 != *arg2 ) {
3228 if ( *arg1 < *arg2 )
return(-1);
3231 if ( *arg1 <= -FUNCTION )
return(0);
3232 return((
int)(arg1[1])-(
int)(arg2[1]));
3243int CompArg(WORD *s1, WORD *s2)
3246 WORD *st1, *st2, x[7];
3250 if ( *s1 <= -FUNCTION && *s2 <= -FUNCTION ) {
3251 if ( *s1 > *s2 )
return(-1);
3252 if ( *s1 < *s2 )
return(1);
3255 if ( *s1 > *s2 )
return(1);
3256 if ( *s1 < *s2 )
return(-1);
3257 if ( *s1 <= -FUNCTION )
return(0);
3259 if ( *s1 > *s2 )
return(1);
3260 if ( *s1 < *s2 )
return(-1);
3263 x[1] = AT.comsym[3];
3264 x[2] = AT.comnum[1];
3265 x[3] = AT.comnum[3];
3266 x[4] = AT.comind[3];
3267 x[5] = AT.comind[6];
3268 x[6] = AT.comfun[1];
3269 if ( *s1 == -SYMBOL ) {
3270 AT.comsym[3] = s1[1];
3271 st1 = AT.comsym+8; s1 = AT.comsym;
3273 else if ( *s1 == -SNUMBER ) {
3275 AT.comnum[1] = -s1[1]; AT.comnum[3] = -3;
3278 AT.comnum[1] = s1[1]; AT.comnum[3] = 3;
3283 else if ( *s1 == -INDEX || *s1 == -VECTOR ) {
3284 AT.comind[3] = s1[1]; AT.comind[6] = 3;
3285 st1 = AT.comind+7; s1 = AT.comind;
3287 else if ( *s1 == -MINVECTOR ) {
3288 AT.comind[3] = s1[1]; AT.comind[6] = -3;
3289 st1 = AT.comind+7; s1 = AT.comind;
3291 else if ( *s1 <= -FUNCTION ) {
3292 AT.comfun[1] = -*s1;
3293 st1 = AT.comfun+FUNHEAD+4; s1 = AT.comfun;
3299 else if ( *s1 == -ARGWILD ) {
3302 else {
goto argerror; }
3303 st2 = s2 + *s2; s2 += ARGHEAD;
3306 else if ( *s2 < 0 ) {
3307 x[1] = AT.comsym[3];
3308 x[2] = AT.comnum[1];
3309 x[3] = AT.comnum[3];
3310 x[4] = AT.comind[3];
3311 x[5] = AT.comind[6];
3312 x[6] = AT.comfun[1];
3313 if ( *s2 == -SYMBOL ) {
3314 AT.comsym[3] = s2[1];
3315 st2 = AT.comsym+8; s2 = AT.comsym;
3317 else if ( *s2 == -SNUMBER ) {
3319 AT.comnum[1] = -s2[1]; AT.comnum[3] = -3;
3322 else if ( s2[1] == 0 ) {
3323 st2 = AT.comnum+4; s2 = st2;
3326 AT.comnum[1] = s2[1]; AT.comnum[3] = 3;
3331 else if ( *s2 == -INDEX || *s2 == -VECTOR ) {
3332 AT.comind[3] = s2[1]; AT.comind[6] = 3;
3333 st2 = AT.comind+7; s2 = AT.comind;
3335 else if ( *s2 == -MINVECTOR ) {
3336 AT.comind[3] = s2[1]; AT.comind[6] = -3;
3337 st2 = AT.comind+7; s2 = AT.comind;
3339 else if ( *s2 <= -FUNCTION ) {
3340 AT.comfun[1] = -*s2;
3341 st2 = AT.comfun+FUNHEAD+4; s2 = AT.comfun;
3347 else if ( *s2 == -ARGWILD ) {
3350 else {
goto argerror; }
3351 st1 = s1 + *s1; s1 += ARGHEAD;
3355 x[1] = AT.comsym[3];
3356 x[2] = AT.comnum[1];
3357 x[3] = AT.comnum[3];
3358 x[4] = AT.comind[3];
3359 x[5] = AT.comind[6];
3360 x[6] = AT.comfun[1];
3361 st1 = s1 + *s1; st2 = s2 + *s2;
3362 s1 += ARGHEAD; s2 += ARGHEAD;
3364 while ( s1 < st1 && s2 < st2 ) {
3365 if ( ( k = CompareTerms(BHEAD s1,s2,(WORD)2) ) != 0 ) {
3366 AT.comsym[3] = x[1];
3367 AT.comnum[1] = x[2];
3368 AT.comnum[3] = x[3];
3369 AT.comind[3] = x[4];
3370 AT.comind[6] = x[5];
3371 AT.comfun[1] = x[6];
3374 s1 += *s1; s2 += *s2;
3376 AT.comsym[3] = x[1];
3377 AT.comnum[1] = x[2];
3378 AT.comnum[3] = x[3];
3379 AT.comind[3] = x[4];
3380 AT.comind[6] = x[5];
3381 AT.comfun[1] = x[6];
3382 if ( s1 < st1 )
return(1);
3383 if ( s2 < st2 )
return(-1);
3388 MesPrint(
"Illegal type of short function argument in Normalize");
3389 Terminate(-1);
return(0);
3397#ifdef HAVE_CLOCK_GETTIME
3400#ifdef HAVE_GETTIMEOFDAY
3401#include <sys/time.h>
3403#include <sys/timeb.h>
3419#ifdef HAVE_CLOCK_GETTIME
3421 clock_gettime(CLOCK_MONOTONIC, &ts);
3424 return(((LONG)(ts.tv_sec)-AM.OldSecTime)*100 +
3425 ((LONG)(ts.tv_nsec / 1000000)-AM.OldMilliTime)/10);
3428 AM.OldSecTime = (LONG)(ts.tv_sec);
3429 AM.OldMilliTime = (LONG)(ts.tv_nsec / 1000000);
3433#ifdef HAVE_GETTIMEOFDAY
3436 gettimeofday(&t, NULL);
3437 sec = (LONG)t.tv_sec;
3438 msec = (LONG)(t.tv_usec/1000);
3440 return (sec-AM.OldSecTime)*100 + (msec-AM.OldMilliTime)/10;
3443 AM.OldSecTime = sec;
3444 AM.OldMilliTime = msec;
3452 return(((LONG)(tp.time)-AM.OldSecTime)*100 +
3453 ((LONG)(tp.millitm)-AM.OldMilliTime)/10);
3456 AM.OldSecTime = (LONG)(tp.time);
3457 AM.OldMilliTime = (LONG)(tp.millitm);
3469LONG TimeChildren(WORD par)
3471 if ( par )
return(Timer(1)-AM.OldChildTime);
3472 AM.OldChildTime = Timer(1);
3490 if ( par )
return(Timer(0)-AR.OldTime);
3491 AR.OldTime = Timer(0);
3504 static int initialized = 0;
3505 static HANDLE hProcess;
3506 FILETIME ftCreate, ftExit, ftKernel, ftUser;
3509 if ( !initialized ) {
3510 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId());
3512 if ( GetProcessTimes(hProcess, &ftCreate, &ftExit, &ftKernel, &ftUser) ) {
3513 PFILETIME pftKernel = &ftKernel;
3514 PFILETIME pftUser = &ftUser;
3515 __int64 t = *(__int64 *)pftKernel + *(__int64 *)pftUser;
3516 return (LONG)(t / 10000);
3522 FILETIME ftCreate, ftExit, ftKernel, ftUser;
3525 hThread = OpenThread(THREAD_QUERY_INFORMATION, FALSE, GetCurrentThreadId());
3527 if ( GetThreadTimes(hThread, &ftCreate, &ftExit, &ftKernel, &ftUser) ) {
3528 PFILETIME pftKernel = &ftKernel;
3529 PFILETIME pftUser = &ftUser;
3530 __int64 t = *(__int64 *)pftKernel + *(__int64 *)pftUser;
3531 lResult = (LONG)(t / 10000);
3533 CloseHandle(hThread);
3540#include <sys/time.h>
3541#include <sys/resource.h>
3542#ifdef WITHPOSIXCLOCK
3551#ifdef WITHPOSIXCLOCK
3570 if ( clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t) ) {
3571 MesPrint(
"Error in getting timing information");
3573 return (LONG)t.tv_sec * 1000 + (LONG)t.tv_nsec / 1000000;
3577 struct rusage rusage;
3579 getrusage(RUSAGE_CHILDREN,&rusage);
3580 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3581 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3584 getrusage(RUSAGE_SELF,&rusage);
3585 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3586 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3593#include <sys/time.h>
3594#include <sys/resource.h>
3598 struct rusage rusage;
3600 getrusage(RUSAGE_CHILDREN,&rusage);
3601 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3602 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3605 getrusage(RUSAGE_SELF,&rusage);
3606 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3607 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3612#include <sys/time.h>
3613#include <sys/resource.h>
3617 struct rusage rusage;
3619 getrusage(RUSAGE_CHILDREN,&rusage);
3620 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3621 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3624 getrusage(RUSAGE_SELF,&rusage);
3625 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3626 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3642#include <sys/time.h>
3643#include <sys/resource.h>
3644 struct rusage rusage;
3646 getrusage(RUSAGE_CHILDREN,&rusage);
3647 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3648 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3651 getrusage(RUSAGE_SELF,&rusage);
3652 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3653 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3657 clock_t tikken = clock();
3658 return((LONG)tikken/1000);
3660 clock_t t, tikken = clock();
3661 t = tikken % CLK_TCK;
3664 tikken += (t*1000)/CLK_TCK;
3672void times(tbuffer_t *buffer);
3678 if ( par == 1 ) {
return(0); }
3681 return(buffer.proc_user_time * 10);
3694#include <sys/types.h>
3695#include <sys/times.h>
3700#include <sys/time.h>
3701#include <sys/resource.h>
3709 if ( par == 1 ) {
return(0); }
3711 if ( ( AO.wrapnum & 1 ) != 0 ) t ^= 0x80000000;
3717 return(AO.wrap+(t/1000));
3722 ULONG a1, a2, a3, a4;
3723 if ( par == 1 ) {
return(0); }
3725 a1 = (ULONG)buffer.tms_utime;
3729 a2 = 1000*a2 + (a3 >> 16);
3734 ret = (LONG)((a4 << 16) + a3 / CLK_TCK);
3740 struct timezone tzp;
3741 if ( par == 1 ) {
return(0); }
3742 gettimeofday(&tp,&tzp); */
3743 return(tp.tv_sec*1000+tp.tv_usec/1000);
3745 struct rusage rusage;
3747 getrusage(RUSAGE_CHILDREN,&rusage);
3748 return((rusage.ru_utime.tv_sec+rusage.ru_stime.tv_sec)*1000
3749 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3752 getrusage(RUSAGE_SELF,&rusage);
3753 return((rusage.ru_utime.tv_sec+rusage.ru_stime.tv_sec)*1000
3754 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3800 int errorcode = 0, coeffsize;
3801 WORD *t, *tt, *tstop, *endterm, *targ, *targstop, *funstop, *argterm;
3802 endterm = term + *term;
3803 coeffsize = ABS(endterm[-1]);
3804 if ( coeffsize >= *term ) {
3805 MLOCK(ErrorMessageLock);
3806 MesPrint(
"TestTerm: Internal inconsistency in term. Coefficient too big.");
3807 MUNLOCK(ErrorMessageLock);
3811 if ( ( coeffsize < 3 ) || ( ( coeffsize & 1 ) != 1 ) ) {
3812 MLOCK(ErrorMessageLock);
3813 MesPrint(
"TestTerm: Internal inconsistency in term. Wrong size coefficient.");
3814 MUNLOCK(ErrorMessageLock);
3819 tstop = endterm - coeffsize;
3820 while ( t < tstop ) {
3831 MLOCK(ErrorMessageLock);
3832 MesPrint(
"TestTerm: Internal inconsistency in term. L or S number");
3833 MUNLOCK(ErrorMessageLock);
3839 case DOLLAREXPRESSION:
3852 MLOCK(ErrorMessageLock);
3853 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal subterm.");
3854 MUNLOCK(ErrorMessageLock);
3862 MLOCK(ErrorMessageLock);
3863 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal subterm number.");
3864 MUNLOCK(ErrorMessageLock);
3871 if ( *t-FUNCTION >= NumFunctions ) {
3872 MLOCK(ErrorMessageLock);
3873 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal function number");
3874 MUNLOCK(ErrorMessageLock);
3879 if ( funstop > tstop )
goto subtermsize;
3881 MLOCK(ErrorMessageLock);
3882 MesPrint(
"TestTerm: Internal inconsistency in term. Dirty flag nonzero.");
3883 MUNLOCK(ErrorMessageLock);
3888 if ( targ > funstop ) {
3889 MLOCK(ErrorMessageLock);
3890 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal function size.");
3891 MUNLOCK(ErrorMessageLock);
3895 if ( functions[*t-FUNCTION].spec >= TENSORFUNCTION ) {
3898 while ( targ < funstop ) {
3900 if ( *targ <= -(FUNCTION+NumFunctions) ) {
3901 MLOCK(ErrorMessageLock);
3902 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal function number in argument.");
3903 MUNLOCK(ErrorMessageLock);
3907 if ( *targ <= -FUNCTION ) { targ++; }
3909 if ( ( *targ != -SYMBOL ) && ( *targ != -VECTOR )
3910 && ( *targ != -MINVECTOR )
3911 && ( *targ != -SNUMBER )
3912 && ( *targ != -ARGWILD )
3913 && ( *targ != -INDEX ) ) {
3914 MLOCK(ErrorMessageLock);
3915 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal object in argument.");
3916 MUNLOCK(ErrorMessageLock);
3923 else if ( ( *targ < ARGHEAD ) || ( targ+*targ > funstop ) ) {
3924 MLOCK(ErrorMessageLock);
3925 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal size of argument.");
3926 MUNLOCK(ErrorMessageLock);
3930 else if ( targ[1] != 0 ) {
3931 MLOCK(ErrorMessageLock);
3932 MesPrint(
"TestTerm: Internal inconsistency in term. Dirty flag in argument.");
3933 MUNLOCK(ErrorMessageLock);
3938 targstop = targ + *targ;
3939 argterm = targ + ARGHEAD;
3940 while ( argterm < targstop ) {
3941 if ( ( *argterm < 4 ) || ( argterm + *argterm > targstop ) ) {
3942 MLOCK(ErrorMessageLock);
3943 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal termsize in argument.");
3944 MUNLOCK(ErrorMessageLock);
3949 MLOCK(ErrorMessageLock);
3950 MesPrint(
"TestTerm: Internal inconsistency in term. Called from TestTerm.");
3951 MUNLOCK(ErrorMessageLock);
3955 argterm += *argterm;
3966 MLOCK(ErrorMessageLock);
3967 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal subterm size.");
3968 MUNLOCK(ErrorMessageLock);
3984int DistrN(
int n,
int *cpl,
int ncpl,
int *scratch)
3995 if ( scratch[0] == -2 ) { scratch[0] = 0;
return(1); }
3998 if ( scratch[0] == ncpl-1 ) {
4001 else if ( scratch[0] == -2 ) {
4002 for ( i = 0; i < n; i++ ) scratch[i] = 0;
4008 if ( scratch[j] < ncpl )
break;
4012 while ( j < n ) { scratch[j] = scratch[j-1]; j++; }
4014 for ( i = 0; i < ncpl; i++ ) cpl[i] = 0;
4015 for ( i = 0; i < n; i++ ) { cpl[scratch[i]]++; }
int PutPreVar(UBYTE *, UBYTE *, UBYTE *, int)
int PF_Bcast(void *buffer, int count)
LONG PF_WriteFileToFile(int handle, UBYTE *buffer, LONG size)
LONG PF_BroadcastNumber(LONG x)
struct bit_field * one_byte
struct bit_field set_of_char[32]