FORM v5.0.0-35-g6318119
Macros | Functions | Variables
tools.c File Reference
#include "form3.h"
#include <sys/timeb.h>
Include dependency graph for tools.c:

Go to the source code of this file.

Macros

#define FILLVALUE   0
 
#define COPYFILEBUFSIZE   40960L
 
#define TERMMEMSTARTNUM   16
 
#define TERMEXTRAWORDS   10
 
#define NUMBERMEMSTARTNUM   16
 
#define NUMBEREXTRAWORDS   10L
 
#define DODOUBLE(x)
 
#define DOEXPAND(x)
 

Functions

UBYTE * LoadInputFile (UBYTE *filename, int type)
 
UBYTE ReadFromStream (STREAM *stream)
 
UBYTE GetFromStream (STREAM *stream)
 
UBYTE LookInStream (STREAM *stream)
 
STREAMOpenStream (UBYTE *name, int type, int prevarmode, int raiselow)
 
int LocateFile (UBYTE **name, int type)
 
STREAMCloseStream (STREAM *stream)
 
STREAMCreateStream (UBYTE *where)
 
LONG GetStreamPosition (STREAM *stream)
 
void PositionStream (STREAM *stream, LONG position)
 
int ReverseStatements (STREAM *stream)
 
void StartFiles (void)
 
int OpenFile (char *name)
 
int OpenAddFile (char *name)
 
int ReOpenFile (char *name)
 
int CreateFile (char *name)
 
int CreateLogFile (char *name)
 
void CloseFile (int handle)
 
int CopyFile (char *source, char *dest)
 
int CreateHandle (void)
 
LONG ReadFile (int handle, UBYTE *buffer, LONG size)
 
LONG ReadPosFile (PHEAD FILEHANDLE *fi, UBYTE *buffer, LONG size, POSITION *pos)
 
LONG WriteFileToFile (int handle, UBYTE *buffer, LONG size)
 
void SeekFile (int handle, POSITION *offset, int origin)
 
LONG TellFile (int handle)
 
void TELLFILE (int handle, POSITION *position)
 
void FlushFile (int handle)
 
int GetPosFile (int handle, fpos_t *pospointer)
 
int SetPosFile (int handle, fpos_t *pospointer)
 
void SynchFile (int handle)
 
void TruncateFile (int handle)
 
int GetChannel (char *name, int mode)
 
int GetAppendChannel (char *name)
 
int CloseChannel (char *name)
 
void UpdateMaxSize (void)
 
int StrCmp (UBYTE *s1, UBYTE *s2)
 
int StrICmp (UBYTE *s1, UBYTE *s2)
 
int StrHICmp (UBYTE *s1, UBYTE *s2)
 
int StrICont (UBYTE *s1, UBYTE *s2)
 
int CmpArray (WORD *t1, WORD *t2, WORD n)
 
int ConWord (UBYTE *s1, UBYTE *s2)
 
int StrLen (UBYTE *s)
 
void NumToStr (UBYTE *s, LONG x)
 
void WriteString (int type, UBYTE *str, int num)
 
void WriteUnfinString (int type, UBYTE *str, int num)
 
UBYTE * AddToString (UBYTE *outstring, UBYTE *extrastring, int par)
 
UBYTE * strDup1 (UBYTE *instring, char *ifwrong)
 
UBYTE * EndOfToken (UBYTE *s)
 
UBYTE * ToToken (UBYTE *s)
 
UBYTE * SkipField (UBYTE *s, int level)
 
WORD ReadSnum (UBYTE **p)
 
UBYTE * NumCopy (WORD y, UBYTE *to)
 
char * LongCopy (LONG y, char *to)
 
char * LongLongCopy (off_t *y, char *to)
 
UBYTE * MakeDate (void)
 
int set_in (UBYTE ch, set_of_char set)
 
one_byte set_set (UBYTE ch, set_of_char set)
 
one_byte set_del (UBYTE ch, set_of_char set)
 
one_byte set_sub (set_of_char set, set_of_char set1, set_of_char set2)
 
void iniTools (void)
 
void * Malloc1 (LONG size, const char *messageifwrong)
 
void M_free (void *x, const char *where)
 
void M_check1 (void)
 
void M_print (void)
 
void TermMallocAddMemory (PHEAD0)
 
void NumberMallocAddMemory (PHEAD0)
 
void CacheNumberMallocAddMemory (PHEAD0)
 
void * FromList (LIST *L)
 
void * From0List (LIST *L)
 
void * FromVarList (LIST *L)
 
int DoubleList (void ***lijst, int *oldsize, int objectsize, char *nameoftype)
 
int DoubleLList (void ***lijst, LONG *oldsize, int objectsize, char *nameoftype)
 
void DoubleBuffer (void **start, void **stop, int size, char *text)
 
void ExpandBuffer (void **buffer, LONG *oldsize, int type)
 
LONG iexp (LONG x, int p)
 
void ToGeneral (WORD *r, WORD *m, WORD par)
 
int ToFast (WORD *r, WORD *m)
 
WORD ToPolyFunGeneral (PHEAD WORD *term)
 
int IsLikeVector (WORD *arg)
 
int AreArgsEqual (WORD *arg1, WORD *arg2)
 
int CompareArgs (WORD *arg1, WORD *arg2)
 
int CompArg (WORD *s1, WORD *s2)
 
LONG TimeWallClock (WORD par)
 
LONG TimeChildren (WORD par)
 
LONG TimeCPU (WORD par)
 
int Crash (void)
 
int TestTerm (WORD *term)
 
int DistrN (int n, int *cpl, int ncpl, int *scratch)
 

Variables

FILES ** filelist
 
int numinfilelist = 0
 
int filelistsize = 0
 
WRITEFILE WriteFile = &WriteFileToFile
 

Detailed Description

Low level routines for many types of task. There are routines for manipulating the input system (streams and files) routines for string manipulation, the memory allocation interface, and the clock. The last is the most sensitive to ports. In the past nearly every port to another OS or computer gave trouble. Nowadays it is slightly better but the poor POSIX compliance of LINUX again gave problems for the multithreaded version.

Definition in file tools.c.

Macro Definition Documentation

◆ FILLVALUE

#define FILLVALUE   0

Definition at line 49 of file tools.c.

◆ TERMMEMSTARTNUM

#define TERMMEMSTARTNUM   16

Provides memory for one term (or one small polynomial) This means that the memory is limited to a buffer of size AM.MaxTer plus a few extra words. In parallel versions, each worker has its own memory pool.

The way we use the memory is by: term = TermMalloc(BHEAD0); and later we free it by TermFree(BHEAD term);

Layout: We have a list of available pointers to buffers: AT.TermMemHeap Its size is AT.TermMemMax We take from the top (indicated by AT.TermMemTop). When we run out of buffers we assign new ones (doubling the amount) and we have to extend the AT.TermMemHeap array. Important: There is no checking that the returned memory is legal, ie is memory that was handed out earlier.

Definition at line 2483 of file tools.c.

◆ TERMEXTRAWORDS

#define TERMEXTRAWORDS   10

Definition at line 2484 of file tools.c.

◆ NUMBERMEMSTARTNUM

#define NUMBERMEMSTARTNUM   16

Provides memory for one Long number This means that the memory is limited to a buffer of size AM.MaxTal In parallel versions, each worker has its own memory pool.

The way we use the memory is by: num = NumberMalloc(BHEAD0); Number = AT.NumberMemHeap[num]; and later we free it by NumberFree(BHEAD num);

Layout: We have a list of available pointers to buffers: AT.NumberMemHeap Its size is AT.NumberMemMax We take from the top (indicated by AT.NumberMemTop). When we run out of buffers we assign new ones (doubling the amount) and we have to extend the AT.NumberMemHeap array. Important: There is no checking on the returned memory!!!!

Definition at line 2583 of file tools.c.

◆ NUMBEREXTRAWORDS

#define NUMBEREXTRAWORDS   10L

Definition at line 2584 of file tools.c.

◆ DODOUBLE

#define DODOUBLE (   x)
Value:
{ x *s, *t, *u; if ( *start ) { \
oldsize = *(x **)stop - *(x **)start; newsize = 2*oldsize; \
t = u = (x *)Malloc1(newsize*sizeof(x),text); s = *(x **)start; \
for ( i = 0; i < oldsize; i++ ) {*t++ = *s++;} M_free(*start,"double"); } \
else { newsize = 100; u = (x *)Malloc1(newsize*sizeof(x),text); } \
*start = (void *)u; *stop = (void *)(u+newsize); }

Definition at line 2906 of file tools.c.

◆ DOEXPAND

#define DOEXPAND (   x)
Value:
{ x *newbuffer, *t, *m; \
t = newbuffer = (x *)Malloc1((newsize+2)*type,"ExpandBuffer"); \
if ( *buffer ) { m = (x *)*buffer; i = *oldsize; \
while ( --i >= 0 ) {*t++ = *m++;} M_free(*buffer,"ExpandBuffer"); \
} *buffer = newbuffer; *oldsize = newsize; }

Definition at line 2932 of file tools.c.

Function Documentation

◆ LoadInputFile()

UBYTE * LoadInputFile ( UBYTE *  filename,
int  type 
)

Definition at line 112 of file tools.c.

◆ ReadFromStream()

UBYTE ReadFromStream ( STREAM stream)

Definition at line 151 of file tools.c.

◆ GetFromStream()

UBYTE GetFromStream ( STREAM stream)

Definition at line 286 of file tools.c.

◆ LookInStream()

UBYTE LookInStream ( STREAM stream)

Definition at line 309 of file tools.c.

◆ OpenStream()

STREAM * OpenStream ( UBYTE *  name,
int  type,
int  prevarmode,
int  raiselow 
)

Definition at line 321 of file tools.c.

◆ LocateFile()

int LocateFile ( UBYTE **  name,
int  type 
)

Definition at line 576 of file tools.c.

◆ CloseStream()

STREAM * CloseStream ( STREAM stream)

Definition at line 663 of file tools.c.

◆ CreateStream()

STREAM * CreateStream ( UBYTE *  where)

Definition at line 784 of file tools.c.

◆ GetStreamPosition()

LONG GetStreamPosition ( STREAM stream)

Definition at line 815 of file tools.c.

◆ PositionStream()

void PositionStream ( STREAM stream,
LONG  position 
)

Definition at line 825 of file tools.c.

◆ ReverseStatements()

int ReverseStatements ( STREAM stream)

Definition at line 857 of file tools.c.

◆ StartFiles()

void StartFiles ( void  )

Definition at line 958 of file tools.c.

◆ OpenFile()

int OpenFile ( char *  name)

Definition at line 985 of file tools.c.

◆ OpenAddFile()

int OpenAddFile ( char *  name)

Definition at line 1004 of file tools.c.

◆ ReOpenFile()

int ReOpenFile ( char *  name)

Definition at line 1025 of file tools.c.

◆ CreateFile()

int CreateFile ( char *  name)

Definition at line 1045 of file tools.c.

◆ CreateLogFile()

int CreateLogFile ( char *  name)

Definition at line 1062 of file tools.c.

◆ CloseFile()

void CloseFile ( int  handle)

Definition at line 1080 of file tools.c.

◆ CopyFile()

int CopyFile ( char *  source,
char *  dest 
)

Copies a file with name *source to a file named *dest. The involved files must not be open. Returns non-zero if an error occurred. Uses if possible the combined large and small sorting buffers as cache.

Definition at line 1103 of file tools.c.

Referenced by DoRecovery().

◆ CreateHandle()

int CreateHandle ( void  )

Definition at line 1164 of file tools.c.

◆ ReadFile()

LONG ReadFile ( int  handle,
UBYTE *  buffer,
LONG  size 
)

Definition at line 1219 of file tools.c.

◆ ReadPosFile()

LONG ReadPosFile ( PHEAD FILEHANDLE fi,
UBYTE *  buffer,
LONG  size,
POSITION pos 
)

Definition at line 1269 of file tools.c.

◆ WriteFileToFile()

LONG WriteFileToFile ( int  handle,
UBYTE *  buffer,
LONG  size 
)

Definition at line 1335 of file tools.c.

◆ SeekFile()

void SeekFile ( int  handle,
POSITION offset,
int  origin 
)

Definition at line 1373 of file tools.c.

◆ TellFile()

LONG TellFile ( int  handle)

Definition at line 1398 of file tools.c.

◆ TELLFILE()

void TELLFILE ( int  handle,
POSITION position 
)

Definition at line 1408 of file tools.c.

◆ FlushFile()

void FlushFile ( int  handle)

Definition at line 1422 of file tools.c.

◆ GetPosFile()

int GetPosFile ( int  handle,
fpos_t *  pospointer 
)

Definition at line 1436 of file tools.c.

◆ SetPosFile()

int SetPosFile ( int  handle,
fpos_t *  pospointer 
)

Definition at line 1450 of file tools.c.

◆ SynchFile()

void SynchFile ( int  handle)

Definition at line 1470 of file tools.c.

◆ TruncateFile()

void TruncateFile ( int  handle)

Definition at line 1492 of file tools.c.

◆ GetChannel()

int GetChannel ( char *  name,
int  mode 
)

Definition at line 1512 of file tools.c.

◆ GetAppendChannel()

int GetAppendChannel ( char *  name)

Definition at line 1548 of file tools.c.

◆ CloseChannel()

int CloseChannel ( char *  name)

Definition at line 1578 of file tools.c.

◆ UpdateMaxSize()

void UpdateMaxSize ( void  )

Definition at line 1612 of file tools.c.

◆ StrCmp()

int StrCmp ( UBYTE *  s1,
UBYTE *  s2 
)

Definition at line 1702 of file tools.c.

◆ StrICmp()

int StrICmp ( UBYTE *  s1,
UBYTE *  s2 
)

Definition at line 1713 of file tools.c.

◆ StrHICmp()

int StrHICmp ( UBYTE *  s1,
UBYTE *  s2 
)

Definition at line 1724 of file tools.c.

◆ StrICont()

int StrICont ( UBYTE *  s1,
UBYTE *  s2 
)

Definition at line 1735 of file tools.c.

◆ CmpArray()

int CmpArray ( WORD *  t1,
WORD *  t2,
WORD  n 
)

Definition at line 1747 of file tools.c.

◆ ConWord()

int ConWord ( UBYTE *  s1,
UBYTE *  s2 
)

Definition at line 1761 of file tools.c.

◆ StrLen()

int StrLen ( UBYTE *  s)

Definition at line 1773 of file tools.c.

◆ NumToStr()

void NumToStr ( UBYTE *  s,
LONG  x 
)

Definition at line 1785 of file tools.c.

◆ WriteString()

void WriteString ( int  type,
UBYTE *  str,
int  num 
)

Definition at line 1809 of file tools.c.

◆ WriteUnfinString()

void WriteUnfinString ( int  type,
UBYTE *  str,
int  num 
)

Definition at line 1843 of file tools.c.

◆ AddToString()

UBYTE * AddToString ( UBYTE *  outstring,
UBYTE *  extrastring,
int  par 
)

Definition at line 1868 of file tools.c.

◆ strDup1()

UBYTE * strDup1 ( UBYTE *  instring,
char *  ifwrong 
)

Definition at line 1906 of file tools.c.

◆ EndOfToken()

UBYTE * EndOfToken ( UBYTE *  s)

Skips over alphanumeric characters to find the end of the token.

Note
This function does not handle formal names (e.g., [x+a]). To handle them, use SkipAName instead.
Parameters
[in]sPointer to a null-terminated input buffer.
Returns
Pointer to the first non-alphanumeric character (i.e., the character immediately after the token), or the null terminator if the token reaches the end of the string.

Definition at line 1932 of file tools.c.

◆ ToToken()

UBYTE * ToToken ( UBYTE *  s)

Skips over non-alphanumeric characters to find the start of a token.

Note
This function does not handle formal names (e.g., [x+a]). To handle them, consider simply skipping whitespace, e.g., by using SkipSpaces.
Parameters
[in]sPointer to a null-terminated input buffer.
Returns
Pointer to the first alphanumeric character (i.e., the start of the token), or the null terminator if none is found.

Definition at line 1955 of file tools.c.

◆ SkipField()

UBYTE * SkipField ( UBYTE *  s,
int  level 
)

Skips from s to the end of a declaration field (e.g., x, 1+2*[x+a], or f({x,[x+a]},1)).

Parameters
[in]sPointer to a null-terminated input buffer.
[in]levelNumber of parentheses that still have to be closed.
Returns
Pointer to the character after the field, which is either a comma at level 0 or the null terminator.

Definition at line 1976 of file tools.c.

◆ ReadSnum()

WORD ReadSnum ( UBYTE **  p)

Definition at line 2003 of file tools.c.

◆ NumCopy()

UBYTE * NumCopy ( WORD  y,
UBYTE *  to 
)

Definition at line 2027 of file tools.c.

◆ LongCopy()

char * LongCopy ( LONG  y,
char *  to 
)

Definition at line 2054 of file tools.c.

◆ LongLongCopy()

char * LongLongCopy ( off_t *  y,
char *  to 
)

Definition at line 2081 of file tools.c.

◆ MakeDate()

UBYTE * MakeDate ( void  )

Definition at line 2117 of file tools.c.

◆ set_in()

int set_in ( UBYTE  ch,
set_of_char  set 
)

Definition at line 2133 of file tools.c.

◆ set_set()

one_byte set_set ( UBYTE  ch,
set_of_char  set 
)

Definition at line 2153 of file tools.c.

◆ set_del()

one_byte set_del ( UBYTE  ch,
set_of_char  set 
)

Definition at line 2174 of file tools.c.

◆ set_sub()

one_byte set_sub ( set_of_char  set,
set_of_char  set1,
set_of_char  set2 
)

Definition at line 2196 of file tools.c.

◆ iniTools()

void iniTools ( void  )

Definition at line 2222 of file tools.c.

◆ Malloc1()

void * Malloc1 ( LONG  size,
const char *  messageifwrong 
)

Definition at line 2243 of file tools.c.

◆ M_free()

void M_free ( void *  x,
const char *  where 
)

Definition at line 2319 of file tools.c.

◆ M_check1()

void M_check1 ( void  )

Definition at line 2452 of file tools.c.

◆ M_print()

void M_print ( void  )

Definition at line 2453 of file tools.c.

◆ TermMallocAddMemory()

void TermMallocAddMemory ( PHEAD0  )

Definition at line 2486 of file tools.c.

◆ NumberMallocAddMemory()

void NumberMallocAddMemory ( PHEAD0  )

Definition at line 2590 of file tools.c.

◆ CacheNumberMallocAddMemory()

void CacheNumberMallocAddMemory ( PHEAD0  )

Definition at line 2664 of file tools.c.

◆ FromList()

void * FromList ( LIST L)

Definition at line 2716 of file tools.c.

◆ From0List()

void * From0List ( LIST L)

Definition at line 2742 of file tools.c.

◆ FromVarList()

void * FromVarList ( LIST L)

Definition at line 2771 of file tools.c.

◆ DoubleList()

int DoubleList ( void ***  lijst,
int *  oldsize,
int  objectsize,
char *  nameoftype 
)

Definition at line 2813 of file tools.c.

◆ DoubleLList()

int DoubleLList ( void ***  lijst,
LONG *  oldsize,
int  objectsize,
char *  nameoftype 
)

Definition at line 2865 of file tools.c.

◆ DoubleBuffer()

void DoubleBuffer ( void **  start,
void **  stop,
int  size,
char *  text 
)

Definition at line 2913 of file tools.c.

◆ ExpandBuffer()

void ExpandBuffer ( void **  buffer,
LONG *  oldsize,
int  type 
)

Definition at line 2938 of file tools.c.

◆ iexp()

LONG iexp ( LONG  x,
int  p 
)

Definition at line 2962 of file tools.c.

◆ ToGeneral()

void ToGeneral ( WORD *  r,
WORD *  m,
WORD  par 
)

Definition at line 2993 of file tools.c.

◆ ToFast()

int ToFast ( WORD *  r,
WORD *  m 
)

Definition at line 3037 of file tools.c.

◆ ToPolyFunGeneral()

WORD ToPolyFunGeneral ( PHEAD WORD *  term)

Definition at line 3090 of file tools.c.

◆ IsLikeVector()

int IsLikeVector ( WORD *  arg)

Definition at line 3165 of file tools.c.

◆ AreArgsEqual()

int AreArgsEqual ( WORD *  arg1,
WORD *  arg2 
)

Definition at line 3193 of file tools.c.

◆ CompareArgs()

int CompareArgs ( WORD *  arg1,
WORD *  arg2 
)

Definition at line 3212 of file tools.c.

◆ CompArg()

int CompArg ( WORD *  s1,
WORD *  s2 
)

Definition at line 3243 of file tools.c.

◆ TimeWallClock()

LONG TimeWallClock ( WORD  par)

Returns the wall-clock time.

Parameters
parIf zero, the wall-clock time will be reset to 0.
Returns
The wall-clock time in centiseconds.

Definition at line 3413 of file tools.c.

Referenced by DoCheckpoint(), DoRecovery(), find_Horner_MCTS(), optimize_expression_given_Horner(), optimize_greedy(), and WriteStats().

◆ TimeChildren()

LONG TimeChildren ( WORD  par)

Definition at line 3469 of file tools.c.

◆ TimeCPU()

LONG TimeCPU ( WORD  par)

Returns the CPU time.

Parameters
parIf zero, the CPU time will be reset to 0.
Returns
The CPU time in milliseconds.

Definition at line 3487 of file tools.c.

Referenced by PF_GetSlaveTimes(), PF_Processor(), and WriteStats().

◆ Crash()

int Crash ( void  )

Definition at line 3770 of file tools.c.

◆ TestTerm()

int TestTerm ( WORD *  term)

Tests the consistency of the term. Returns 0 when the term is OK. Any nonzero value is trouble. In the current version the testing isn't 100% complete. For instance, we don't check the validity of the symbols nor do we check the range of their powers. Etc. This should be extended when the need is there.

Parameters
termthe term to be tested

Definition at line 3798 of file tools.c.

References TestTerm().

Referenced by TestTerm().

Here is the call graph for this function:

◆ DistrN()

int DistrN ( int  n,
int *  cpl,
int  ncpl,
int *  scratch 
)

Definition at line 3984 of file tools.c.

Variable Documentation

◆ filelist

FILES** filelist

Definition at line 65 of file tools.c.

◆ numinfilelist

int numinfilelist = 0

Definition at line 66 of file tools.c.

◆ filelistsize

int filelistsize = 0

Definition at line 67 of file tools.c.

◆ WriteFile

WRITEFILE WriteFile = &WriteFileToFile

Definition at line 1359 of file tools.c.