From 47e17586924f6858a29b1bf81cbcba0dd0b21617 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marek=20Ol=C5=A1=C3=A1k?= Date: Fri, 7 Oct 2016 04:49:13 +0200 Subject: [PATCH] glcpp: use the linear allocator for most objects MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit v2: cosmetic changes Tested-by: Edmondo Tommasina (v1) Reviewed-by: Nicolai Hähnle (v1) --- src/compiler/glsl/glcpp/glcpp-lex.l | 2 +- src/compiler/glsl/glcpp/glcpp-parse.y | 206 +++++++++++--------------- src/compiler/glsl/glcpp/glcpp.h | 1 + 3 files changed, 91 insertions(+), 118 deletions(-) diff --git a/src/compiler/glsl/glcpp/glcpp-lex.l b/src/compiler/glsl/glcpp/glcpp-lex.l index d09441aac88..f4a68767010 100644 --- a/src/compiler/glsl/glcpp/glcpp-lex.l +++ b/src/compiler/glsl/glcpp/glcpp-lex.l @@ -101,7 +101,7 @@ void glcpp_set_column (int column_no , yyscan_t yyscanner); #define RETURN_STRING_TOKEN(token) \ do { \ if (! parser->skipping) { \ - yylval->str = ralloc_strdup (yyextra, yytext); \ + yylval->str = linear_strdup(yyextra->linalloc, yytext); \ RETURN_TOKEN_NEVER_SKIP (token); \ } \ } while(0) diff --git a/src/compiler/glsl/glcpp/glcpp-parse.y b/src/compiler/glsl/glcpp/glcpp-parse.y index 5bf0606d40e..b80ff044e93 100644 --- a/src/compiler/glsl/glcpp/glcpp-parse.y +++ b/src/compiler/glsl/glcpp/glcpp-parse.y @@ -49,10 +49,11 @@ _define_function_macro(glcpp_parser_t *parser, token_list_t *replacements); static string_list_t * -_string_list_create(void *ctx); +_string_list_create(glcpp_parser_t *parser); static void -_string_list_append_item(string_list_t *list, const char *str); +_string_list_append_item(glcpp_parser_t *parser, string_list_t *list, + const char *str); static int _string_list_contains(string_list_t *list, const char *member, int *index); @@ -67,10 +68,11 @@ static int _string_list_equal(string_list_t *a, string_list_t *b); static argument_list_t * -_argument_list_create(void *ctx); +_argument_list_create(glcpp_parser_t *parser); static void -_argument_list_append(argument_list_t *list, token_list_t *argument); +_argument_list_append(glcpp_parser_t *parser, argument_list_t *list, + token_list_t *argument); static int _argument_list_length(argument_list_t *list); @@ -78,18 +80,17 @@ _argument_list_length(argument_list_t *list); static token_list_t * _argument_list_member_at(argument_list_t *list, int index); -/* Note: This function ralloc_steal()s the str pointer. */ static token_t * -_token_create_str(void *ctx, int type, char *str); +_token_create_str(glcpp_parser_t *parser, int type, char *str); static token_t * -_token_create_ival(void *ctx, int type, int ival); +_token_create_ival(glcpp_parser_t *parser, int type, int ival); static token_list_t * -_token_list_create(void *ctx); +_token_list_create(glcpp_parser_t *parser); static void -_token_list_append(token_list_t *list, token_t *token); +_token_list_append(glcpp_parser_t *parser, token_list_t *list, token_t *token); static void _token_list_append_list(token_list_t *list, token_list_t *tail); @@ -209,7 +210,6 @@ line: | text_line { _glcpp_parser_print_expanded_token_list (parser, $1); ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n"); - ralloc_free ($1); } | expanded_line ; @@ -277,7 +277,6 @@ control_line: control_line_success: HASH_TOKEN DEFINE_TOKEN define | HASH_TOKEN UNDEF IDENTIFIER NEWLINE { - macro_t *macro; struct hash_entry *entry; /* Section 3.4 (Preprocessor) of the GLSL ES 3.00 spec says: @@ -312,11 +311,8 @@ control_line_success: entry = _mesa_hash_table_search (parser->defines, $3); if (entry) { - macro = entry->data; _mesa_hash_table_remove (parser->defines, entry); - ralloc_free (macro); } - ralloc_free ($3); } | HASH_TOKEN IF pp_tokens NEWLINE { /* Be careful to only evaluate the 'if' expression if @@ -353,7 +349,6 @@ control_line_success: struct hash_entry *entry = _mesa_hash_table_search(parser->defines, $3); macro_t *macro = entry ? entry->data : NULL; - ralloc_free ($3); _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL); } | HASH_TOKEN IFNDEF IDENTIFIER junk NEWLINE { @@ -478,7 +473,7 @@ expression: | IDENTIFIER { $$.value = 0; if (parser->is_gles) - $$.undefined_macro = ralloc_strdup (parser, $1); + $$.undefined_macro = linear_strdup(parser->linalloc, $1); else $$.undefined_macro = NULL; } @@ -650,13 +645,11 @@ expression: identifier_list: IDENTIFIER { $$ = _string_list_create (parser); - _string_list_append_item ($$, $1); - ralloc_steal ($$, $1); + _string_list_append_item (parser, $$, $1); } | identifier_list ',' IDENTIFIER { $$ = $1; - _string_list_append_item ($$, $3); - ralloc_steal ($$, $3); + _string_list_append_item (parser, $$, $3); } ; @@ -681,11 +674,11 @@ pp_tokens: preprocessing_token { parser->space_tokens = 1; $$ = _token_list_create (parser); - _token_list_append ($$, $1); + _token_list_append (parser, $$, $1); } | pp_tokens preprocessing_token { $$ = $1; - _token_list_append ($$, $2); + _token_list_append (parser, $$, $2); } ; @@ -755,11 +748,11 @@ operator: %% string_list_t * -_string_list_create(void *ctx) +_string_list_create(glcpp_parser_t *parser) { string_list_t *list; - list = ralloc (ctx, string_list_t); + list = linear_alloc_child(parser->linalloc, sizeof(string_list_t)); list->head = NULL; list->tail = NULL; @@ -767,12 +760,13 @@ _string_list_create(void *ctx) } void -_string_list_append_item(string_list_t *list, const char *str) +_string_list_append_item(glcpp_parser_t *parser, string_list_t *list, + const char *str) { string_node_t *node; - node = ralloc (list, string_node_t); - node->str = ralloc_strdup (node, str); + node = linear_alloc_child(parser->linalloc, sizeof(string_node_t)); + node->str = linear_strdup(parser->linalloc, str); node->next = NULL; @@ -865,11 +859,11 @@ _string_list_equal(string_list_t *a, string_list_t *b) } argument_list_t * -_argument_list_create(void *ctx) +_argument_list_create(glcpp_parser_t *parser) { argument_list_t *list; - list = ralloc (ctx, argument_list_t); + list = linear_alloc_child(parser->linalloc, sizeof(argument_list_t)); list->head = NULL; list->tail = NULL; @@ -877,11 +871,12 @@ _argument_list_create(void *ctx) } void -_argument_list_append(argument_list_t *list, token_list_t *argument) +_argument_list_append(glcpp_parser_t *parser, + argument_list_t *list, token_list_t *argument) { argument_node_t *node; - node = ralloc (list, argument_node_t); + node = linear_alloc_child(parser->linalloc, sizeof(argument_node_t)); node->argument = argument; node->next = NULL; @@ -932,27 +927,24 @@ _argument_list_member_at(argument_list_t *list, int index) return NULL; } -/* Note: This function ralloc_steal()s the str pointer. */ token_t * -_token_create_str(void *ctx, int type, char *str) +_token_create_str(glcpp_parser_t *parser, int type, char *str) { token_t *token; - token = ralloc (ctx, token_t); + token = linear_alloc_child(parser->linalloc, sizeof(token_t)); token->type = type; token->value.str = str; - ralloc_steal (token, str); - return token; } token_t * -_token_create_ival(void *ctx, int type, int ival) +_token_create_ival(glcpp_parser_t *parser, int type, int ival) { token_t *token; - token = ralloc (ctx, token_t); + token = linear_alloc_child(parser->linalloc, sizeof(token_t)); token->type = type; token->value.ival = ival; @@ -960,11 +952,11 @@ _token_create_ival(void *ctx, int type, int ival) } token_list_t * -_token_list_create(void *ctx) +_token_list_create(glcpp_parser_t *parser) { token_list_t *list; - list = ralloc (ctx, token_list_t); + list = linear_alloc_child(parser->linalloc, sizeof(token_list_t)); list->head = NULL; list->tail = NULL; list->non_space_tail = NULL; @@ -973,11 +965,11 @@ _token_list_create(void *ctx) } void -_token_list_append(token_list_t *list, token_t *token) +_token_list_append(glcpp_parser_t *parser, token_list_t *list, token_t *token) { token_node_t *node; - node = ralloc (list, token_node_t); + node = linear_alloc_child(parser->linalloc, sizeof(token_node_t)); node->token = token; node->next = NULL; @@ -1009,7 +1001,7 @@ _token_list_append_list(token_list_t *list, token_list_t *tail) } static token_list_t * -_token_list_copy(void *ctx, token_list_t *other) +_token_list_copy(glcpp_parser_t *parser, token_list_t *other) { token_list_t *copy; token_node_t *node; @@ -1017,11 +1009,11 @@ _token_list_copy(void *ctx, token_list_t *other) if (other == NULL) return NULL; - copy = _token_list_create (ctx); + copy = _token_list_create (parser); for (node = other->head; node; node = node->next) { - token_t *new_token = ralloc (copy, token_t); + token_t *new_token = linear_alloc_child(parser->linalloc, sizeof(token_t)); *new_token = *node->token; - _token_list_append (copy, new_token); + _token_list_append (parser, copy, new_token); } return copy; @@ -1030,18 +1022,9 @@ _token_list_copy(void *ctx, token_list_t *other) static void _token_list_trim_trailing_space(token_list_t *list) { - token_node_t *tail, *next; - if (list->non_space_tail) { - tail = list->non_space_tail->next; list->non_space_tail->next = NULL; list->tail = list->non_space_tail; - - while (tail) { - next = tail->next; - ralloc_free (tail); - tail = next; - } } } @@ -1184,9 +1167,9 @@ _token_print(char **out, size_t *len, token_t *token) } } -/* Return a new token (ralloc()ed off of 'token') formed by pasting - * 'token' and 'other'. Note that this function may return 'token' or - * 'other' directly rather than allocating anything new. +/* Return a new token formed by pasting 'token' and 'other'. Note that this + * function may return 'token' or 'other' directly rather than allocating + * anything new. * * Caution: Only very cursory error-checking is performed to see if * the final result is a valid single token. */ @@ -1208,31 +1191,31 @@ _token_paste(glcpp_parser_t *parser, token_t *token, token_t *other) switch (token->type) { case '<': if (other->type == '<') - combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT); + combined = _token_create_ival (parser, LEFT_SHIFT, LEFT_SHIFT); else if (other->type == '=') - combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL); + combined = _token_create_ival (parser, LESS_OR_EQUAL, LESS_OR_EQUAL); break; case '>': if (other->type == '>') - combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT); + combined = _token_create_ival (parser, RIGHT_SHIFT, RIGHT_SHIFT); else if (other->type == '=') - combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL); + combined = _token_create_ival (parser, GREATER_OR_EQUAL, GREATER_OR_EQUAL); break; case '=': if (other->type == '=') - combined = _token_create_ival (token, EQUAL, EQUAL); + combined = _token_create_ival (parser, EQUAL, EQUAL); break; case '!': if (other->type == '=') - combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL); + combined = _token_create_ival (parser, NOT_EQUAL, NOT_EQUAL); break; case '&': if (other->type == '&') - combined = _token_create_ival (token, AND, AND); + combined = _token_create_ival (parser, AND, AND); break; case '|': if (other->type == '|') - combined = _token_create_ival (token, OR, OR); + combined = _token_create_ival (parser, OR, OR); break; } @@ -1276,14 +1259,14 @@ _token_paste(glcpp_parser_t *parser, token_t *token, token_t *other) } if (token->type == INTEGER) - str = ralloc_asprintf (token, "%" PRIiMAX, token->value.ival); + str = linear_asprintf(parser->linalloc, "%" PRIiMAX, token->value.ival); else - str = ralloc_strdup (token, token->value.str); + str = linear_strdup(parser->linalloc, token->value.str); if (other->type == INTEGER) - ralloc_asprintf_append (&str, "%" PRIiMAX, other->value.ival); + linear_asprintf_append(parser->linalloc, &str, "%" PRIiMAX, other->value.ival); else - ralloc_strcat (&str, other->value.str); + linear_strcat(parser->linalloc, &str, other->value.str); /* New token is same type as original token, unless we * started with an integer, in which case we will be @@ -1292,7 +1275,7 @@ _token_paste(glcpp_parser_t *parser, token_t *token, token_t *other) if (combined_type == INTEGER) combined_type = INTEGER_STRING; - combined = _token_create_str (token, combined_type, str); + combined = _token_create_str (parser, combined_type, str); combined->location = token->location; return combined; } @@ -1335,7 +1318,7 @@ add_builtin_define(glcpp_parser_t *parser, const char *name, int value) tok = _token_create_ival (parser, INTEGER, value); list = _token_list_create(parser); - _token_list_append(list, tok); + _token_list_append(parser, list, tok); _define_object_macro(parser, NULL, name, list); } @@ -1349,6 +1332,7 @@ glcpp_parser_create(glcpp_extension_iterator extensions, void *state, gl_api api glcpp_lex_init_extra (parser, &parser->scanner); parser->defines = _mesa_hash_table_create(NULL, _mesa_key_hash_string, _mesa_key_string_equal); + parser->linalloc = linear_alloc_parent(parser, 0); parser->active = NULL; parser->lexing_directive = 0; parser->lexing_version_directive = 0; @@ -1427,7 +1411,8 @@ typedef enum function_status * Macro name is not followed by a balanced set of parentheses. */ static function_status_t -_arguments_parse(argument_list_t *arguments, token_node_t *node, +_arguments_parse(glcpp_parser_t *parser, + argument_list_t *arguments, token_node_t *node, token_node_t **last) { token_list_t *argument; @@ -1444,8 +1429,8 @@ _arguments_parse(argument_list_t *arguments, token_node_t *node, node = node->next; - argument = _token_list_create (arguments); - _argument_list_append (arguments, argument); + argument = _token_list_create (parser); + _argument_list_append (parser, arguments, argument); for (paren_count = 1; node; node = node->next) { if (node->token->type == '(') { @@ -1458,15 +1443,15 @@ _arguments_parse(argument_list_t *arguments, token_node_t *node, if (node->token->type == ',' && paren_count == 1) { _token_list_trim_trailing_space (argument); - argument = _token_list_create (arguments); - _argument_list_append (arguments, argument); + argument = _token_list_create (parser); + _argument_list_append (parser, arguments, argument); } else { if (argument->head == NULL) { /* Don't treat initial whitespace as part of the argument. */ if (node->token->type == SPACE) continue; } - _token_list_append (argument, node->token); + _token_list_append(parser, argument, node->token); } } @@ -1479,28 +1464,28 @@ _arguments_parse(argument_list_t *arguments, token_node_t *node, } static token_list_t * -_token_list_create_with_one_ival(void *ctx, int type, int ival) +_token_list_create_with_one_ival(glcpp_parser_t *parser, int type, int ival) { token_list_t *list; token_t *node; - list = _token_list_create(ctx); - node = _token_create_ival(list, type, ival); - _token_list_append(list, node); + list = _token_list_create(parser); + node = _token_create_ival(parser, type, ival); + _token_list_append(parser, list, node); return list; } static token_list_t * -_token_list_create_with_one_space(void *ctx) +_token_list_create_with_one_space(glcpp_parser_t *parser) { - return _token_list_create_with_one_ival(ctx, SPACE, SPACE); + return _token_list_create_with_one_ival(parser, SPACE, SPACE); } static token_list_t * -_token_list_create_with_one_integer(void *ctx, int ival) +_token_list_create_with_one_integer(glcpp_parser_t *parser, int ival) { - return _token_list_create_with_one_ival(ctx, INTEGER, ival); + return _token_list_create_with_one_ival(parser, INTEGER, ival); } /* Evaluate a DEFINED token node (based on subsequent tokens in the list). @@ -1604,8 +1589,8 @@ _glcpp_parser_evaluate_defined_in_list(glcpp_parser_t *parser, if (value == -1) goto NEXT; - replacement = ralloc (list, token_node_t); - replacement->token = _token_create_ival (list, INTEGER, value); + replacement = linear_alloc_child(parser->linalloc, sizeof(token_node_t)); + replacement->token = _token_create_ival (parser, INTEGER, value); /* Splice replacement node into list, replacing from "node" * through "last". */ @@ -1642,7 +1627,7 @@ _glcpp_parser_expand_and_lex_from(glcpp_parser_t *parser, int head_token_type, expanded = _token_list_create (parser); token = _token_create_ival (parser, head_token_type, head_token_type); - _token_list_append (expanded, token); + _token_list_append (parser, expanded, token); _glcpp_parser_expand_token_list (parser, list, mode); _token_list_append_list (expanded, list); glcpp_parser_lex_from (parser, expanded); @@ -1729,7 +1714,7 @@ _glcpp_parser_expand_function(glcpp_parser_t *parser, token_node_t *node, assert(macro->is_function); arguments = _argument_list_create(parser); - status = _arguments_parse(arguments, node, last); + status = _arguments_parse(parser, arguments, node, last); switch (status) { case FUNCTION_STATUS_SUCCESS: @@ -1743,7 +1728,6 @@ _glcpp_parser_expand_function(glcpp_parser_t *parser, token_node_t *node, /* Replace a macro defined as empty with a SPACE token. */ if (macro->replacements == NULL) { - ralloc_free(arguments); return _token_list_create_with_one_space(parser); } @@ -1760,7 +1744,7 @@ _glcpp_parser_expand_function(glcpp_parser_t *parser, token_node_t *node, } /* Perform argument substitution on the replacement list. */ - substituted = _token_list_create(arguments); + substituted = _token_list_create(parser); for (node = macro->replacements->head; node; node = node->next) { if (node->token->type == IDENTIFIER && @@ -1778,12 +1762,12 @@ _glcpp_parser_expand_function(glcpp_parser_t *parser, token_node_t *node, } else { token_t *new_token; - new_token = _token_create_ival(substituted, PLACEHOLDER, + new_token = _token_create_ival(parser, PLACEHOLDER, PLACEHOLDER); - _token_list_append(substituted, new_token); + _token_list_append(parser, substituted, new_token); } } else { - _token_list_append(substituted, node->token); + _token_list_append(parser, substituted, node->token); } } @@ -1857,10 +1841,10 @@ _glcpp_parser_expand_node(glcpp_parser_t *parser, token_node_t *node, token_list_t *expansion; token_t *final; - str = ralloc_strdup(parser, token->value.str); + str = linear_strdup(parser->linalloc, token->value.str); final = _token_create_str(parser, OTHER, str); expansion = _token_list_create(parser); - _token_list_append(expansion, final); + _token_list_append(parser, expansion, final); return expansion; } @@ -1892,8 +1876,8 @@ _parser_active_list_push(glcpp_parser_t *parser, const char *identifier, { active_list_t *node; - node = ralloc(parser->active, active_list_t); - node->identifier = ralloc_strdup(node, identifier); + node = linear_alloc_child(parser->linalloc, sizeof(active_list_t)); + node->identifier = linear_strdup(parser->linalloc, identifier); node->marker = marker; node->next = parser->active; @@ -1911,8 +1895,6 @@ _parser_active_list_pop(glcpp_parser_t *parser) } node = parser->active->next; - ralloc_free (parser->active); - parser->active = node; } @@ -2100,19 +2082,17 @@ _define_object_macro(glcpp_parser_t *parser, YYLTYPE *loc, if (loc != NULL) _check_for_reserved_macro_name(parser, loc, identifier); - macro = ralloc (parser, macro_t); + macro = linear_alloc_child(parser->linalloc, sizeof(macro_t)); macro->is_function = 0; macro->parameters = NULL; - macro->identifier = ralloc_strdup (macro, identifier); + macro->identifier = linear_strdup(parser->linalloc, identifier); macro->replacements = replacements; - ralloc_steal (macro, replacements); entry = _mesa_hash_table_search(parser->defines, identifier); previous = entry ? entry->data : NULL; if (previous) { if (_macro_equal (macro, previous)) { - ralloc_free (macro); return; } glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier); @@ -2137,20 +2117,17 @@ _define_function_macro(glcpp_parser_t *parser, YYLTYPE *loc, glcpp_error (loc, parser, "Duplicate macro parameter \"%s\"", dup); } - macro = ralloc (parser, macro_t); - ralloc_steal (macro, parameters); - ralloc_steal (macro, replacements); + macro = linear_alloc_child(parser->linalloc, sizeof(macro_t)); macro->is_function = 1; macro->parameters = parameters; - macro->identifier = ralloc_strdup (macro, identifier); + macro->identifier = linear_strdup(parser->linalloc, identifier); macro->replacements = replacements; entry = _mesa_hash_table_search(parser->defines, identifier); previous = entry ? entry->data : NULL; if (previous) { if (_macro_equal (macro, previous)) { - ralloc_free (macro); return; } glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier); @@ -2217,7 +2194,6 @@ glcpp_parser_lex(YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser) node = parser->lex_from_node; if (node == NULL) { - ralloc_free (parser->lex_from_list); parser->lex_from_list = NULL; return NEWLINE; } @@ -2243,16 +2219,13 @@ glcpp_parser_lex_from(glcpp_parser_t *parser, token_list_t *list) for (node = list->head; node; node = node->next) { if (node->token->type == SPACE) continue; - _token_list_append (parser->lex_from_list, node->token); + _token_list_append (parser, parser->lex_from_list, node->token); } - ralloc_free (list); - parser->lex_from_node = parser->lex_from_list->head; /* It's possible the list consisted of nothing but whitespace. */ if (parser->lex_from_node == NULL) { - ralloc_free (parser->lex_from_list); parser->lex_from_list = NULL; } } @@ -2267,7 +2240,7 @@ _glcpp_parser_skip_stack_push_if(glcpp_parser_t *parser, YYLTYPE *loc, if (parser->skip_stack) current = parser->skip_stack->type; - node = ralloc (parser, skip_node_t); + node = linear_alloc_child(parser->linalloc, sizeof(skip_node_t)); node->loc = *loc; if (current == SKIP_NO_SKIP) { @@ -2313,7 +2286,6 @@ _glcpp_parser_skip_stack_pop(glcpp_parser_t *parser, YYLTYPE *loc) node = parser->skip_stack; parser->skip_stack = node->next; - ralloc_free (node); } static void diff --git a/src/compiler/glsl/glcpp/glcpp.h b/src/compiler/glsl/glcpp/glcpp.h index cab4374c5e5..bb4ad67f28b 100644 --- a/src/compiler/glsl/glcpp/glcpp.h +++ b/src/compiler/glsl/glcpp/glcpp.h @@ -181,6 +181,7 @@ typedef void (*glcpp_extension_iterator)( bool es); struct glcpp_parser { + void *linalloc; yyscan_t scanner; struct hash_table *defines; active_list_t *active; -- 2.30.2