Scanner/parser portability changes.
authorMarko Kreen <markokr@gmail.com>
Fri, 27 Jun 2008 08:35:33 +0000 (08:35 +0000)
committerMarko Kreen <markokr@gmail.com>
Fri, 27 Jun 2008 08:35:33 +0000 (08:35 +0000)
- Avoid use of alloca in parser, instead redefine malloc/free to palloc/pfree.
- Previously exception from palloc() confused scanner as invalid pointer
  could stay around.  Now forcibly reinitialize everything.
- Remove debug malloc wrappers from scanner.

src/parser.y
src/scanner.l

index 152867e9e0b34e9e5fe3380445782ba9385d0f16..e82b44991e524b4cc9a81988bd6c38f99d467eb7 100644 (file)
@@ -24,7 +24,9 @@
 void plproxy_yy_scan_bytes(const char *bytes, int len);
 
 /* avoid permanent allocations */
-#define YYSTACK_USE_ALLOCA 1
+#define malloc palloc
+#define free pfree
+
 /* remove unused code */
 #define YY_LOCATION_PRINT(File, Loc) (0)
 #define YY_(x) (x)
@@ -162,6 +164,9 @@ void plproxy_run_parser(ProxyFunction *func, const char *body, int len)
        /* By default expect RUN ON ANY; */
        xfunc->run_type = R_ANY;
 
+       /* reinitialize scanner */
+       plproxy_yylex_startup();
+
        /* setup scanner */
        plproxy_yy_scan_bytes(body, len);
 
@@ -177,7 +182,7 @@ void plproxy_run_parser(ProxyFunction *func, const char *body, int len)
                        yyerror("CLUSTER statement missing");
        }
 
-       /* reinitialize scanner */
+       /* release scanner resources */
        plproxy_yylex_destroy();
 
        /* copy hash data if needed */
index 0ad2f4604a8264f798f75c42d31c717446b02508..33ff8151a0db951b81cbe1adb474a8f7dc0c9973 100644 (file)
@@ -39,40 +39,17 @@ int plproxy_yylex_destroy(void);
 #define yylval plproxy_yylval
 
 /*
- * Allocate in CurrentMemoryContext.  That means plproxy_yylex_destroy()
- * must be called before SPI_finish().
+ * Allocate in CurrentMemoryContext.
  *
  * If we want to support flex 2.5.4, we cannot use
  * options noyyalloc, noyyrealloc, noyyfree.
  *
  * Thus such need to hack malloc() et al.
  */
-#undef FLXMALLOCLOG
 
-static inline void *my_malloc(const char *pfx, int len) {
-       void *p = palloc(len);
-#ifdef FLXMALLOCLOG
-       elog(NOTICE, "%s:%s(%d) = %p", pfx, __FUNCTION__, len, p);
-#endif
-       return p;
-}
-static inline void *my_realloc(const char *pfx, void *old, int len) {
-       void *p = repalloc(old, len);
-#ifdef FLXMALLOCLOG
-       elog(NOTICE, "%s:%s(%p, %d) = %p", pfx, __FUNCTION__, old, len, p);
-#endif
-       return p;
-}
-static inline void my_free(const char *pfx, void *p) {
-       pfree(p);
-#ifdef FLXMALLOCLOG
-       elog(NOTICE, "%s:%s(%p)", pfx, __FUNCTION__, p);
-#endif
-}
-
-#define malloc(x)  my_malloc(__FILE__, x)
-#define realloc(x, y)  my_realloc(__FILE__, x, y)
-#define free(x)  my_free(__FILE__, x)
+#define malloc palloc
+#define realloc repalloc
+#define free(p) do { if (p) pfree(p); } while (0)
 
 
 /*
@@ -86,6 +63,17 @@ static inline void my_free(const char *pfx, void *p) {
 #endif
 #define FLXVER ((YY_FLEX_MAJOR_VERSION*1000 + YY_FLEX_MINOR_VERSION)*1000 + YY_FLEX_SUBMINOR_VERSION)
 
+void plproxy_yylex_startup(void)
+{
+       /* there may be stale pointers around, drop them */
+#if FLXVER < 2005031
+       (YY_CURRENT_BUFFER) = NULL;
+#else
+       (yy_buffer_stack) = NULL;
+#endif
+       plproxy_yylex_destroy();
+}
+
 /*
  * compat stuff for older flex
  */