Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit802fe92

Browse files
committed
cube: pure parser and reentrant scanner
Use the flex %option reentrant and the bison option %pure-parser tomake the generated scanner and parser pure, reentrant, andthread-safe.Make the generated scanner use palloc() etc. instead of malloc() etc.Previously, we only used palloc() for the buffer, but flex would stilluse malloc() for its internal structures. As a result, there could besome small memory leaks in case of uncaught errors. (We do catchnormal syntax errors as soft errors.) Now, all the memory is underpalloc() control, so there are no more such issues.Simplify flex scan buffer management: Instead of constructing thebuffer from pieces and then using yy_scan_buffer(), we can just useyy_scan_string(), which does the same thing internally. (Actually, weuse yy_scan_bytes() here because we already have the length.)The previous code was necessary because we allocated the buffer withpalloc() and the rest of the state was handled by malloc(). But thisis no longer the case; everything is under palloc() now.(We could even get rid of the yylex_destroy() call and just let thememory context cleanup handle everything. But for now, we preservethe existing behavior.)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
1 parent477728b commit802fe92

File tree

4 files changed

+74
-52
lines changed

4 files changed

+74
-52
lines changed

‎contrib/cube/cube.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,14 @@ cube_in(PG_FUNCTION_ARGS)
120120
char*str=PG_GETARG_CSTRING(0);
121121
NDBOX*result;
122122
Sizescanbuflen;
123+
yyscan_tscanner;
123124

124-
cube_scanner_init(str,&scanbuflen);
125+
cube_scanner_init(str,&scanbuflen,&scanner);
125126

126-
cube_yyparse(&result,scanbuflen,fcinfo->context);
127+
cube_yyparse(&result,scanbuflen,fcinfo->context,scanner);
127128

128129
/* We might as well run this even on failure. */
129-
cube_scanner_finish();
130+
cube_scanner_finish(scanner);
130131

131132
PG_RETURN_NDBOX_P(result);
132133
}

‎contrib/cube/cubedata.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,21 @@ typedef struct NDBOX
5959
#defineCubeKNNDistanceEuclid17/* <-> */
6060
#defineCubeKNNDistanceChebyshev18/* <=> */
6161

62+
/* for cubescan.l and cubeparse.y */
63+
/* All grammar constructs return strings */
64+
#defineYYSTYPE char *
65+
typedefvoid*yyscan_t;
66+
6267
/* in cubescan.l */
63-
externintcube_yylex(void);
68+
externintcube_yylex(YYSTYPE*yylval_param,yyscan_tyyscanner);
6469
externvoidcube_yyerror(NDBOX**result,Sizescanbuflen,
6570
structNode*escontext,
71+
yyscan_tyyscanner,
6672
constchar*message);
67-
externvoidcube_scanner_init(constchar*str,Size*scanbuflen);
68-
externvoidcube_scanner_finish(void);
73+
externvoidcube_scanner_init(constchar*str,Size*scanbuflen,yyscan_t*yyscannerp);
74+
externvoidcube_scanner_finish(yyscan_tyyscanner);
6975

7076
/* in cubeparse.y */
7177
externintcube_yyparse(NDBOX**result,Sizescanbuflen,
72-
structNode*escontext);
78+
structNode*escontext,
79+
yyscan_tyyscanner);

‎contrib/cube/cubeparse.y

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,11 @@
77
#include"postgres.h"
88

99
#include"cubedata.h"
10+
#include"cubeparse.h"/* must be after cubedata.h for YYSTYPE and NDBOX*/
1011
#include"nodes/miscnodes.h"
1112
#include"utils/float.h"
1213
#include"varatt.h"
1314

14-
/* All grammar constructs return strings*/
15-
#defineYYSTYPEchar *
16-
17-
#include"cubeparse.h"
18-
19-
/* silence -Wmissing-variable-declarations*/
20-
externint cube_yychar;
21-
externint cube_yynerrs;
22-
2315
/*
2416
* Bison doesn't allocate anything that needs to live across parser calls,
2517
* so we can easily have it use palloc instead of malloc. This prevents
@@ -40,6 +32,9 @@ static bool write_point_as_box(int dim, char *str,
4032
%parse-param {NDBOX **result}
4133
%parse-param {Size scanbuflen}
4234
%parse-param {structNode *escontext}
35+
%parse-param {yyscan_t yyscanner}
36+
%lex-param {yyscan_t yyscanner}
37+
%pure-parser
4338
%expect0
4439
%name-prefix="cube_yy"
4540

@@ -75,6 +70,8 @@ box: O_BRACKET paren_list COMMA paren_list C_BRACKET
7570

7671
if (!write_box(dim, $2, $4, result, escontext))
7772
YYABORT;
73+
74+
(void) yynerrs;/* suppress compiler warning*/
7875
}
7976

8077
| paren_list COMMA paren_list

‎contrib/cube/cubescan.l

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@
66

77
#include "postgres.h"
88

9-
/*
10-
* NB: include cubeparse.h only AFTER defining YYSTYPE (to match cubeparse.y)
11-
* and cubedata.h for NDBOX.
12-
*/
139
#include "cubedata.h"
14-
#define YYSTYPE char *
15-
#include "cubeparse.h"
10+
#include "cubeparse.h"/* must be after cubedata.h for YYSTYPE and NDBOX */
1611
}
1712

1813
%{
@@ -30,18 +25,19 @@ fprintf_to_ereport(const char *fmt, const char *msg)
3025
{
3126
ereport(ERROR, (errmsg_internal("%s", msg)));
3227
}
33-
34-
/* Handles to the buffer that the lexer uses internally*/
35-
static YY_BUFFER_STATE scanbufhandle;
36-
staticchar *scanbuf;
3728
%}
3829

30+
%optionreentrant
31+
%optionbison-bridge
3932
%option8bit
4033
%optionnever-interactive
4134
%optionnodefault
4235
%optionnoinput
4336
%optionnounput
4437
%optionnoyywrap
38+
%optionnoyyalloc
39+
%optionnoyyrealloc
40+
%optionnoyyfree
4541
%optionwarn
4642
%optionprefix="cube_yy"
4743

@@ -55,14 +51,14 @@ NaN [nN][aA][nN]
5551

5652
%%
5753

58-
{float}cube_yylval = yytext;return CUBEFLOAT;
59-
{infinity}cube_yylval = yytext;return CUBEFLOAT;
60-
{NaN}cube_yylval = yytext;return CUBEFLOAT;
61-
\[cube_yylval ="(";return O_BRACKET;
62-
\]cube_yylval =")";return C_BRACKET;
63-
\(cube_yylval ="(";return O_PAREN;
64-
\)cube_yylval =")";return C_PAREN;
65-
\,cube_yylval =",";return COMMA;
54+
{float}*yylval = yytext;return CUBEFLOAT;
55+
{infinity}*yylval = yytext;return CUBEFLOAT;
56+
{NaN}*yylval = yytext;return CUBEFLOAT;
57+
\[*yylval ="(";return O_BRACKET;
58+
\]*yylval =")";return C_BRACKET;
59+
\(*yylval ="(";return O_PAREN;
60+
\)*yylval =")";return C_PAREN;
61+
\,*yylval =",";return COMMA;
6662
[\t\n\r\f\v]+/* discard spaces */
6763
.return yytext[0];/* alert parser of the garbage*/
6864

@@ -74,8 +70,11 @@ NaN [nN][aA][nN]
7470
void
7571
cube_yyerror(NDBOX **result, Size scanbuflen,
7672
structNode *escontext,
73+
yyscan_t yyscanner,
7774
constchar *message)
7875
{
76+
structyyguts_t * yyg = (structyyguts_t *) yyscanner;/* needed for yytext macro */
77+
7978
if (*yytext == YY_END_OF_BUFFER_CHAR)
8079
{
8180
errsave(escontext,
@@ -99,35 +98,53 @@ cube_yyerror(NDBOX **result, Size scanbuflen,
9998
* Called before any actual parsing is done
10099
*/
101100
void
102-
cube_scanner_init(constchar *str, Size *scanbuflen)
101+
cube_scanner_init(constchar *str, Size *scanbuflen,yyscan_t *yyscannerp)
103102
{
104103
Sizeslen =strlen(str);
104+
yyscan_tyyscanner;
105105

106-
/*
107-
* Might be left over after ereport()
108-
*/
109-
if (YY_CURRENT_BUFFER)
110-
yy_delete_buffer(YY_CURRENT_BUFFER);
106+
if (yylex_init(yyscannerp) !=0)
107+
elog(ERROR,"yylex_init() failed: %m");
111108

112-
/*
113-
* Make a scan buffer with special termination needed by flex.
114-
*/
115-
*scanbuflen = slen;
116-
scanbuf =palloc(slen +2);
117-
memcpy(scanbuf, str, slen);
118-
scanbuf[slen] = scanbuf[slen +1] = YY_END_OF_BUFFER_CHAR;
119-
scanbufhandle =yy_scan_buffer(scanbuf, slen +2);
109+
yyscanner = *yyscannerp;
120110

121-
BEGIN(INITIAL);
111+
yy_scan_bytes(str, slen, yyscanner);
112+
*scanbuflen = slen;
122113
}
123114

124115

125116
/*
126117
* Called after parsing is done to clean up after cube_scanner_init()
127118
*/
128119
void
129-
cube_scanner_finish(void)
120+
cube_scanner_finish(yyscan_t yyscanner)
121+
{
122+
yylex_destroy(yyscanner);
123+
}
124+
125+
/*
126+
* Interface functions to make flex use palloc() instead of malloc().
127+
* It'd be better to make these static, but flex insists otherwise.
128+
*/
129+
130+
void *
131+
yyalloc(yy_size_t size,yyscan_t yyscanner)
132+
{
133+
returnpalloc(size);
134+
}
135+
136+
void *
137+
yyrealloc(void *ptr,yy_size_t size,yyscan_t yyscanner)
138+
{
139+
if (ptr)
140+
returnrepalloc(ptr, size);
141+
else
142+
returnpalloc(size);
143+
}
144+
145+
void
146+
yyfree(void *ptr,yyscan_t yyscanner)
130147
{
131-
yy_delete_buffer(scanbufhandle);
132-
pfree(scanbuf);
148+
if (ptr)
149+
pfree(ptr);
133150
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp