mirror of
https://github.com/postgres/postgres.git
synced 2025-06-26 12:21:12 +03:00
syncrep parser: pure parser and reentrant scanner
Use the flex %option reentrant and the bison option %pure-parser to make the generated scanner and parser pure, reentrant, and thread-safe. Make the generated scanner use palloc() etc. instead of malloc() etc. Previously, we only used palloc() for the buffer, but flex would still use malloc() for its internal structures. Now, all the memory is under palloc() control. Simplify flex scan buffer management: Instead of constructing the buffer from pieces and then using yy_scan_buffer(), we can just use yy_scan_string(), which does the same thing internally. The previous code was necessary because we allocated the buffer with palloc() and the rest of the state was handled by malloc(). But this is no longer the case; everything is under palloc() now. Use flex yyextra to handle context information, instead of global variables. This complements the other changes to make the scanner reentrant. Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi> Reviewed-by: Andreas Karlsson <andreas@proxel.se> Discussion: https://www.postgresql.org/message-id/flat/eb6faeac-2a8a-4b69-9189-c33c520e5b7b@eisentraut.org
This commit is contained in:
@ -11,7 +11,7 @@ GETTEXT_TRIGGERS = $(BACKEND_COMMON_GETTEXT_TRIGGERS) \
|
|||||||
parser_yyerror \
|
parser_yyerror \
|
||||||
replication_yyerror:2 \
|
replication_yyerror:2 \
|
||||||
scanner_yyerror \
|
scanner_yyerror \
|
||||||
syncrep_yyerror \
|
syncrep_yyerror:2 \
|
||||||
report_invalid_record:2 \
|
report_invalid_record:2 \
|
||||||
ereport_startup_progress \
|
ereport_startup_progress \
|
||||||
json_token_error:2 \
|
json_token_error:2 \
|
||||||
|
@ -992,6 +992,7 @@ check_synchronous_standby_names(char **newval, void **extra, GucSource source)
|
|||||||
{
|
{
|
||||||
if (*newval != NULL && (*newval)[0] != '\0')
|
if (*newval != NULL && (*newval)[0] != '\0')
|
||||||
{
|
{
|
||||||
|
yyscan_t scanner;
|
||||||
int parse_rc;
|
int parse_rc;
|
||||||
SyncRepConfigData *pconf;
|
SyncRepConfigData *pconf;
|
||||||
|
|
||||||
@ -1000,9 +1001,9 @@ check_synchronous_standby_names(char **newval, void **extra, GucSource source)
|
|||||||
syncrep_parse_error_msg = NULL;
|
syncrep_parse_error_msg = NULL;
|
||||||
|
|
||||||
/* Parse the synchronous_standby_names string */
|
/* Parse the synchronous_standby_names string */
|
||||||
syncrep_scanner_init(*newval);
|
syncrep_scanner_init(*newval, &scanner);
|
||||||
parse_rc = syncrep_yyparse();
|
parse_rc = syncrep_yyparse(scanner);
|
||||||
syncrep_scanner_finish();
|
syncrep_scanner_finish(scanner);
|
||||||
|
|
||||||
if (parse_rc != 0 || syncrep_parse_result == NULL)
|
if (parse_rc != 0 || syncrep_parse_result == NULL)
|
||||||
{
|
{
|
||||||
|
@ -26,10 +26,6 @@ char *syncrep_parse_error_msg;
|
|||||||
static SyncRepConfigData *create_syncrep_config(const char *num_sync,
|
static SyncRepConfigData *create_syncrep_config(const char *num_sync,
|
||||||
List *members, uint8 syncrep_method);
|
List *members, uint8 syncrep_method);
|
||||||
|
|
||||||
/* silence -Wmissing-variable-declarations */
|
|
||||||
extern int syncrep_yychar;
|
|
||||||
extern int syncrep_yynerrs;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Bison doesn't allocate anything that needs to live across parser calls,
|
* Bison doesn't allocate anything that needs to live across parser calls,
|
||||||
* so we can easily have it use palloc instead of malloc. This prevents
|
* so we can easily have it use palloc instead of malloc. This prevents
|
||||||
@ -40,6 +36,9 @@ extern int syncrep_yynerrs;
|
|||||||
|
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
%parse-param {yyscan_t yyscanner}
|
||||||
|
%lex-param {yyscan_t yyscanner}
|
||||||
|
%pure-parser
|
||||||
%expect 0
|
%expect 0
|
||||||
%name-prefix="syncrep_yy"
|
%name-prefix="syncrep_yy"
|
||||||
|
|
||||||
@ -60,7 +59,10 @@ extern int syncrep_yynerrs;
|
|||||||
|
|
||||||
%%
|
%%
|
||||||
result:
|
result:
|
||||||
standby_config { syncrep_parse_result = $1; }
|
standby_config {
|
||||||
|
syncrep_parse_result = $1;
|
||||||
|
(void) yynerrs; /* suppress compiler warning */
|
||||||
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
standby_config:
|
standby_config:
|
||||||
|
@ -37,21 +37,27 @@ fprintf_to_ereport(const char *fmt, const char *msg)
|
|||||||
ereport(ERROR, (errmsg_internal("%s", msg)));
|
ereport(ERROR, (errmsg_internal("%s", msg)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Handles to the buffer that the lexer uses internally */
|
struct syncrep_yy_extra_type
|
||||||
static YY_BUFFER_STATE scanbufhandle;
|
{
|
||||||
|
StringInfoData xdbuf;
|
||||||
static StringInfoData xdbuf;
|
};
|
||||||
|
#define YY_EXTRA_TYPE struct syncrep_yy_extra_type *
|
||||||
|
|
||||||
/* LCOV_EXCL_START */
|
/* LCOV_EXCL_START */
|
||||||
|
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
%option reentrant
|
||||||
|
%option bison-bridge
|
||||||
%option 8bit
|
%option 8bit
|
||||||
%option never-interactive
|
%option never-interactive
|
||||||
%option nodefault
|
%option nodefault
|
||||||
%option noinput
|
%option noinput
|
||||||
%option nounput
|
%option nounput
|
||||||
%option noyywrap
|
%option noyywrap
|
||||||
|
%option noyyalloc
|
||||||
|
%option noyyrealloc
|
||||||
|
%option noyyfree
|
||||||
%option warn
|
%option warn
|
||||||
%option prefix="syncrep_yy"
|
%option prefix="syncrep_yy"
|
||||||
|
|
||||||
@ -82,38 +88,38 @@ xdinside [^"]+
|
|||||||
[Ff][Ii][Rr][Ss][Tt] { return FIRST; }
|
[Ff][Ii][Rr][Ss][Tt] { return FIRST; }
|
||||||
|
|
||||||
{xdstart} {
|
{xdstart} {
|
||||||
initStringInfo(&xdbuf);
|
initStringInfo(&yyextra->xdbuf);
|
||||||
BEGIN(xd);
|
BEGIN(xd);
|
||||||
}
|
}
|
||||||
<xd>{xddouble} {
|
<xd>{xddouble} {
|
||||||
appendStringInfoChar(&xdbuf, '"');
|
appendStringInfoChar(&yyextra->xdbuf, '"');
|
||||||
}
|
}
|
||||||
<xd>{xdinside} {
|
<xd>{xdinside} {
|
||||||
appendStringInfoString(&xdbuf, yytext);
|
appendStringInfoString(&yyextra->xdbuf, yytext);
|
||||||
}
|
}
|
||||||
<xd>{xdstop} {
|
<xd>{xdstop} {
|
||||||
syncrep_yylval.str = xdbuf.data;
|
yylval->str = yyextra->xdbuf.data;
|
||||||
xdbuf.data = NULL;
|
yyextra->xdbuf.data = NULL;
|
||||||
BEGIN(INITIAL);
|
BEGIN(INITIAL);
|
||||||
return NAME;
|
return NAME;
|
||||||
}
|
}
|
||||||
<xd><<EOF>> {
|
<xd><<EOF>> {
|
||||||
syncrep_yyerror("unterminated quoted identifier");
|
syncrep_yyerror(yyscanner, "unterminated quoted identifier");
|
||||||
return JUNK;
|
return JUNK;
|
||||||
}
|
}
|
||||||
|
|
||||||
{identifier} {
|
{identifier} {
|
||||||
syncrep_yylval.str = pstrdup(yytext);
|
yylval->str = pstrdup(yytext);
|
||||||
return NAME;
|
return NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
{digit}+ {
|
{digit}+ {
|
||||||
syncrep_yylval.str = pstrdup(yytext);
|
yylval->str = pstrdup(yytext);
|
||||||
return NUM;
|
return NUM;
|
||||||
}
|
}
|
||||||
|
|
||||||
"*" {
|
"*" {
|
||||||
syncrep_yylval.str = "*";
|
yylval->str = "*";
|
||||||
return NAME;
|
return NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,10 +132,16 @@ xdinside [^"]+
|
|||||||
|
|
||||||
/* LCOV_EXCL_STOP */
|
/* LCOV_EXCL_STOP */
|
||||||
|
|
||||||
|
/* see scan.l */
|
||||||
|
#undef yyextra
|
||||||
|
#define yyextra (((struct yyguts_t *) yyscanner)->yyextra_r)
|
||||||
|
|
||||||
/* Needs to be here for access to yytext */
|
/* Needs to be here for access to yytext */
|
||||||
void
|
void
|
||||||
syncrep_yyerror(const char *message)
|
syncrep_yyerror(yyscan_t yyscanner, const char *message)
|
||||||
{
|
{
|
||||||
|
struct yyguts_t * yyg = (struct yyguts_t *) yyscanner; /* needed for yytext macro */
|
||||||
|
|
||||||
/* report only the first error in a parse operation */
|
/* report only the first error in a parse operation */
|
||||||
if (syncrep_parse_error_msg)
|
if (syncrep_parse_error_msg)
|
||||||
return;
|
return;
|
||||||
@ -142,32 +154,51 @@ syncrep_yyerror(const char *message)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
syncrep_scanner_init(const char *str)
|
syncrep_scanner_init(const char *str, yyscan_t *yyscannerp)
|
||||||
{
|
{
|
||||||
Size slen = strlen(str);
|
yyscan_t yyscanner;
|
||||||
char *scanbuf;
|
struct syncrep_yy_extra_type *yyext = palloc0_object(struct syncrep_yy_extra_type);
|
||||||
|
|
||||||
/*
|
if (yylex_init(yyscannerp) != 0)
|
||||||
* Might be left over after ereport()
|
elog(ERROR, "yylex_init() failed: %m");
|
||||||
*/
|
|
||||||
if (YY_CURRENT_BUFFER)
|
|
||||||
yy_delete_buffer(YY_CURRENT_BUFFER);
|
|
||||||
|
|
||||||
/*
|
yyscanner = *yyscannerp;
|
||||||
* Make a scan buffer with special termination needed by flex.
|
|
||||||
*/
|
|
||||||
scanbuf = (char *) palloc(slen + 2);
|
|
||||||
memcpy(scanbuf, str, slen);
|
|
||||||
scanbuf[slen] = scanbuf[slen + 1] = YY_END_OF_BUFFER_CHAR;
|
|
||||||
scanbufhandle = yy_scan_buffer(scanbuf, slen + 2);
|
|
||||||
|
|
||||||
/* Make sure we start in proper state */
|
yyset_extra(yyext, yyscanner);
|
||||||
BEGIN(INITIAL);
|
|
||||||
|
yy_scan_string(str, yyscanner);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
syncrep_scanner_finish(void)
|
syncrep_scanner_finish(yyscan_t yyscanner)
|
||||||
{
|
{
|
||||||
yy_delete_buffer(scanbufhandle);
|
pfree(yyextra);
|
||||||
scanbufhandle = NULL;
|
yylex_destroy(yyscanner);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Interface functions to make flex use palloc() instead of malloc().
|
||||||
|
* It'd be better to make these static, but flex insists otherwise.
|
||||||
|
*/
|
||||||
|
|
||||||
|
void *
|
||||||
|
yyalloc(yy_size_t size, yyscan_t yyscanner)
|
||||||
|
{
|
||||||
|
return palloc(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
yyrealloc(void *ptr, yy_size_t size, yyscan_t yyscanner)
|
||||||
|
{
|
||||||
|
if (ptr)
|
||||||
|
return repalloc(ptr, size);
|
||||||
|
else
|
||||||
|
return palloc(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
yyfree(void *ptr, yyscan_t yyscanner)
|
||||||
|
{
|
||||||
|
if (ptr)
|
||||||
|
pfree(ptr);
|
||||||
}
|
}
|
||||||
|
@ -100,10 +100,15 @@ extern void SyncRepUpdateSyncStandbysDefined(void);
|
|||||||
* Internal functions for parsing synchronous_standby_names grammar,
|
* Internal functions for parsing synchronous_standby_names grammar,
|
||||||
* in syncrep_gram.y and syncrep_scanner.l
|
* in syncrep_gram.y and syncrep_scanner.l
|
||||||
*/
|
*/
|
||||||
extern int syncrep_yyparse(void);
|
union YYSTYPE;
|
||||||
extern int syncrep_yylex(void);
|
#ifndef YY_TYPEDEF_YY_SCANNER_T
|
||||||
extern void syncrep_yyerror(const char *str);
|
#define YY_TYPEDEF_YY_SCANNER_T
|
||||||
extern void syncrep_scanner_init(const char *str);
|
typedef void *yyscan_t;
|
||||||
extern void syncrep_scanner_finish(void);
|
#endif
|
||||||
|
extern int syncrep_yyparse(yyscan_t yyscanner);
|
||||||
|
extern int syncrep_yylex(union YYSTYPE *yylval_param, yyscan_t yyscanner);
|
||||||
|
extern void syncrep_yyerror(yyscan_t yyscanner, const char *str);
|
||||||
|
extern void syncrep_scanner_init(const char *str, yyscan_t *yyscannerp);
|
||||||
|
extern void syncrep_scanner_finish(yyscan_t yyscanner);
|
||||||
|
|
||||||
#endif /* _SYNCREP_H */
|
#endif /* _SYNCREP_H */
|
||||||
|
Reference in New Issue
Block a user