OSDN Git Service

Convert lexer to talloc and add xtalloc wrappers.
authorCarl Worth <cworth@cworth.org>
Wed, 12 May 2010 19:45:33 +0000 (12:45 -0700)
committerCarl Worth <cworth@cworth.org>
Wed, 12 May 2010 19:47:29 +0000 (12:47 -0700)
The lexer was previously using strdup (expecting the parser to free),
but is now more consistent, easier to use, and slightly more efficent
by using talloc along with the parser.

Also, we add xtalloc and xtalloc_strdup wrappers around talloc and
talloc_strdup to put all of the out-of-memory-checking code in one
place.

Makefile
glcpp-lex.l
glcpp-parse.y
glcpp.h
xtalloc.c [new file with mode: 0644]

index 8351932..7233150 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ CFLAGS = -g
 # all the warnings enabled.
 override CFLAGS += -Wall -Wextra -Wwrite-strings -Wswitch-enum -Wno-unused
 
-glcpp: glcpp.o glcpp-lex.o glcpp-parse.o hash_table.o
+glcpp: glcpp.o glcpp-lex.o glcpp-parse.o hash_table.o xtalloc.o
        gcc -o $@ -ltalloc $^
 
 %.c %.h: %.y
index f1a3560..ec91538 100644 (file)
@@ -30,6 +30,7 @@
 %}
 
 %option reentrant noyywrap
+%option extra-type="glcpp_parser_t *"
 
 %x ST_DEFINE
 %x ST_DEFVAL
@@ -50,12 +51,12 @@ TOKEN               {NONSPACE}+
 }
 
 <ST_DEFINE>{IDENTIFIER}        {
-       yylval.str = strdup (yytext);
+       yylval.str = xtalloc_strdup (yyextra, yytext);
        return IDENTIFIER;
 }
 
 <ST_DEFINE>{TOKEN} {
-       yylval.str = strdup (yytext);
+       yylval.str = xtalloc_strdup (yyextra, yytext);
        return TOKEN;
 }
 
@@ -68,7 +69,7 @@ TOKEN         {NONSPACE}+
 
        /* Anything we don't specifically recognize is a stream of tokens */
 {NONSPACE}+ {
-       yylval.str = strdup (yytext);
+       yylval.str = xtalloc_strdup (yyextra, yytext);
        return TOKEN;
 }
 
index eae96ef..1a7ec49 100644 (file)
@@ -71,21 +71,20 @@ input:
 content:
        token {
                _print_resolved_token (parser, $1);
-               free ($1);
+               talloc_free ($1);
        }
 |      directive
 |      content token {
                _print_resolved_token (parser, $2);
-               free ($2);
+               talloc_free ($2);
        }
 |      content directive
 ;
 
 directive:
        DEFINE IDENTIFIER replacement_list NEWLINE {
-               char *key = talloc_strdup ($3, $2);
-               free ($2);
-               hash_table_insert (parser->defines, $3, key);
+               talloc_steal ($3, $2);
+               hash_table_insert (parser->defines, $3, $2);
                printf ("\n");
        }
 ;
@@ -97,7 +96,7 @@ replacement_list:
 
 |      replacement_list token {
                _list_append ($1, $2);
-               free ($2);
+               talloc_free ($2);
                $$ = $1;
        }
 ;
@@ -114,12 +113,7 @@ _list_create (void *ctx)
 {
        list_t *list;
 
-       list = talloc (ctx, list_t);
-       if (list == NULL) {
-               fprintf (stderr, "Out of memory.\n");
-               exit (1);
-       }
-
+       list = xtalloc (ctx, list_t);
        list->head = NULL;
        list->tail = NULL;
 
@@ -131,17 +125,8 @@ _list_append (list_t *list, const char *str)
 {
        node_t *node;
 
-       node = talloc (list, node_t);
-       if (node == NULL) {
-               fprintf (stderr, "Out of memory.\n");
-               exit (1);
-       }
-
-       node->str = talloc_strdup (node, str);
-       if (node->str == NULL) {
-               fprintf (stderr, "Out of memory.\n");
-               exit (1);
-       }
+       node = xtalloc (list, node_t);
+       node->str = xtalloc_strdup (node, str);
                
        node->next = NULL;
 
@@ -165,13 +150,9 @@ glcpp_parser_create (void)
 {
        glcpp_parser_t *parser;
 
-       parser = talloc (NULL, glcpp_parser_t);
-       if (parser == NULL) {
-               fprintf (stderr, "Out of memory.\n");
-               exit (1);
-       }
+       parser = xtalloc (NULL, glcpp_parser_t);
 
-       yylex_init (&parser->scanner);
+       yylex_init_extra (parser, &parser->scanner);
        parser->defines = hash_table_ctor (32, hash_table_string_hash,
                                           hash_table_string_compare);
 
diff --git a/glcpp.h b/glcpp.h
index 6fea933..8472570 100644 (file)
--- a/glcpp.h
+++ b/glcpp.h
@@ -55,7 +55,7 @@ glcpp_parser_destroy (glcpp_parser_t *parser);
 /* Generated by glcpp-lex.l to glcpp-lex.c */
 
 int
-yylex_init (yyscan_t *scanner);
+yylex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner);
 
 int
 yylex (yyscan_t scanner);
@@ -68,4 +68,14 @@ yylex_destroy (yyscan_t scanner);
 int
 yyparse (glcpp_parser_t *parser);
 
+/* xtalloc - wrappers around talloc to check for out-of-memory */
+
+#define xtalloc(ctx, type) (type *)xtalloc_named_const(ctx, sizeof(type), #type)
+
+void *
+xtalloc_named_const (const void *context, size_t size, const char *name);
+
+char *
+xtalloc_strdup (const void *t, const char *p);
+
 #endif
diff --git a/xtalloc.c b/xtalloc.c
new file mode 100644 (file)
index 0000000..849e12d
--- /dev/null
+++ b/xtalloc.c
@@ -0,0 +1,52 @@
+/*
+ * Copyright © 2010 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <talloc.h>
+
+void *
+xtalloc_named_const (const void *context, size_t size, const char *name)
+{
+       void *ret;
+
+       ret = talloc_named_const (context, size, name);
+       if (ret == NULL) {
+               fprintf (stderr, "Out of memory.\n");
+               exit (1);
+       }
+
+       return ret;
+}
+
+char *
+xtalloc_strdup (const void *t, const char *p)
+{
+       char *ret;
+
+       ret = talloc_strdup (t, p);
+       if (ret == NULL) {
+               fprintf (stderr, "Out of memory.\n");
+               exit (1);
+       }
+
+       return ret;
+}