Skip to content

Commit

Permalink
kernel: remove output stream stack
Browse files Browse the repository at this point in the history
Instead of pre-allocating a fixed number of TypOutputFile instances on each
thread, we allocate the required storage dynamically on the stack. This
arguably makes it easier to reason about the global state of GAP.

It also enables future simplifications and improvements, e.g. to fix issues
were switching between stdout and errout breaks tracking of the output state,
which leads to GAP expecting resp. inserting linebreaks in the wrong spots.
  • Loading branch information
fingolfin committed Oct 16, 2020
1 parent 56dce07 commit e62d395
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 145 deletions.
10 changes: 10 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,14 @@ typedef const struct init_info StructInitInfo;
typedef struct TypInputFile TypInputFile;


/****************************************************************************
**
*T TypOutputFile . . . . . . . . . . structure of an open output file, local
**
** This is a forward declaration so that TypOutputFiles can be used in
** header files. The actual declaration is in io.h.
*/
typedef struct TypOutputFile TypOutputFile;


#endif // GAP_COMMON_H
14 changes: 5 additions & 9 deletions src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -5190,21 +5190,17 @@ static void CompFunc(Obj func)

/****************************************************************************
**
*F CompileFunc( <output>, <func>, <name>, <magic1>, <magic2> ) . . . compile
*F CompileFunc( <filename>, <func>, <name>, <magic1>, <magic2> ) . . compile
*/
Int CompileFunc (
Obj output,
Obj func,
Obj name,
Int magic1,
Obj magic2 )
Int CompileFunc(Obj filename, Obj func, Obj name, Int magic1, Obj magic2)
{
Int i; /* loop variable */
UInt col;
UInt compFunctionsNr;

/* open the output file */
if (!OpenOutput(CONST_CSTR_STRING(output), FALSE)) {
TypOutputFile output = { 0 };
if (!OpenOutput(&output, CONST_CSTR_STRING(filename), FALSE)) {
return 0;
}
col = SyNrCols;
Expand Down Expand Up @@ -5371,7 +5367,7 @@ Int CompileFunc (

/* close the output file */
SyNrCols = col;
CloseOutput();
CloseOutput(&output);

return compFunctionsNr;
}
Expand Down
17 changes: 9 additions & 8 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ static Obj IsOutputStream;
** ERROR_OUTPUT global variable defined in
** error.g, or "*errout*" otherwise
*/
UInt OpenErrorOutput( void )
UInt OpenErrorOutput(TypOutputFile * output)
{
/* Try to print the output to stream. Use *errout* as a fallback. */
UInt ret = 0;

if (ERROR_OUTPUT != NULL) {
if (IsStringConv(ERROR_OUTPUT)) {
ret = OpenOutput(CONST_CSTR_STRING(ERROR_OUTPUT), FALSE);
ret = OpenOutput(output, CONST_CSTR_STRING(ERROR_OUTPUT), FALSE);
}
else {
if (CALL_1ARGS(IsOutputStream, ERROR_OUTPUT) == True) {
ret = OpenOutputStream(ERROR_OUTPUT);
ret = OpenOutputStream(output, ERROR_OUTPUT);
}
}
}
Expand All @@ -75,7 +75,7 @@ UInt OpenErrorOutput( void )
/* It may be we already tried and failed to open *errout* above but
* but this is an extreme case so it can't hurt to try again
* anyways */
ret = OpenOutput("*errout*", FALSE);
ret = OpenOutput(output, "*errout*", FALSE);
if (ret) {
Pr("failed to open error stream\n", 0, 0);
}
Expand Down Expand Up @@ -173,10 +173,11 @@ static Obj FuncPRINT_CURRENT_STATEMENT(Obj self, Obj stream, Obj context)

/* HACK: we want to redirect output */
/* Try to print the output to stream. Use *errout* as a fallback. */
TypOutputFile output = { 0 };
if ((IsStringConv(stream) &&
!OpenOutput(CONST_CSTR_STRING(stream), FALSE)) ||
(!IS_STRING(stream) && !OpenOutputStream(stream))) {
if (OpenOutput("*errout*", FALSE)) {
!OpenOutput(&output, CONST_CSTR_STRING(stream), FALSE)) ||
(!IS_STRING(stream) && !OpenOutputStream(&output, stream))) {
if (OpenOutput(&output, "*errout*", FALSE)) {
Pr("PRINT_CURRENT_STATEMENT: failed to open error stream\n", 0, 0);
}
else {
Expand Down Expand Up @@ -216,7 +217,7 @@ static Obj FuncPRINT_CURRENT_STATEMENT(Obj self, Obj stream, Obj context)
}

/* HACK: close the output again */
CloseOutput();
CloseOutput(&output);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Int RegisterBreakloopObserver(intfunc func);
** ERROR_OUTPUT global variable defined in
** error.g, or "*errout*" otherwise
*/
UInt OpenErrorOutput(void);
UInt OpenErrorOutput(TypOutputFile * output);

/****************************************************************************
**
Expand Down
7 changes: 4 additions & 3 deletions src/gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,14 @@ static Obj Shell(Obj context,
}

/* read-eval-print loop */
if (!OpenOutput(outFile, FALSE))
TypOutputFile output = { 0 };
if (!OpenOutput(&output, outFile, FALSE))
ErrorQuit("SHELL: can't open outfile %s",(Int)outFile,0);

TypInputFile input = { 0 };
if (!OpenInput(&input, inFile))
{
CloseOutput();
CloseOutput(&output);
ErrorQuit("SHELL: can't open infile %s",(Int)inFile,0);
}

Expand Down Expand Up @@ -317,7 +318,7 @@ static Obj Shell(Obj context,

SetPrintObjState(oldPrintObjState);
CloseInput(&input);
CloseOutput();
CloseOutput(&output);
STATE(ErrorLLevel) = oldErrorLLevel;
SetRecursionDepth(oldRecursionDepth);

Expand Down
Loading

0 comments on commit e62d395

Please sign in to comment.