summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Worth <cworth@cworth.org>2010-05-12 12:45:33 -0700
committerCarl Worth <cworth@cworth.org>2010-05-12 12:47:29 -0700
commit5070a20cd1e65d52856bd74558f9a34f8dca114f (patch)
treea8cd5b178480c7b670b0a8e63ccd15dff190c7b3
parent33cc400714f379ef13e876b4aedd0de8cb5d033d (diff)
Convert lexer to talloc and add xtalloc wrappers.
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.
-rw-r--r--Makefile2
-rw-r--r--glcpp-lex.l7
-rw-r--r--glcpp-parse.y39
-rw-r--r--glcpp.h12
-rw-r--r--xtalloc.c52
5 files changed, 78 insertions, 34 deletions
diff --git a/Makefile b/Makefile
index 83519328bf..7233150a80 100644
--- 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
diff --git a/glcpp-lex.l b/glcpp-lex.l
index f1a3560779..ec91538a73 100644
--- a/glcpp-lex.l
+++ b/glcpp-lex.l
@@ -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;
}
diff --git a/glcpp-parse.y b/glcpp-parse.y
index eae96efb30..1a7ec4970d 100644
--- a/glcpp-parse.y
+++ b/glcpp-parse.y
@@ -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 6fea9333e8..8472570ccb 100644
--- 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
index 0000000000..849e12d349
--- /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;
+}