# 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
%}
%option reentrant noyywrap
+%option extra-type="glcpp_parser_t *"
%x ST_DEFINE
%x ST_DEFVAL
}
<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;
}
/* Anything we don't specifically recognize is a stream of tokens */
{NONSPACE}+ {
- yylval.str = strdup (yytext);
+ yylval.str = xtalloc_strdup (yyextra, yytext);
return TOKEN;
}
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");
}
;
| replacement_list token {
_list_append ($1, $2);
- free ($2);
+ talloc_free ($2);
$$ = $1;
}
;
{
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;
{
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;
{
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);
/* 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);
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
--- /dev/null
+/*
+ * 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;
+}