From 7c37fc6a8b97da8c2953e0ec347cee0228076a25 Mon Sep 17 00:00:00 2001 From: Henri Verbeet Date: Tue, 5 Aug 2025 21:17:28 +0200 Subject: [PATCH] vkd3d-shader: Introduce struct vkd3d_shader_source_list. --- libs/vkd3d-shader/hlsl.c | 20 +++++++------- libs/vkd3d-shader/hlsl.h | 3 +-- libs/vkd3d-shader/hlsl.y | 24 +++++++---------- libs/vkd3d-shader/vkd3d_shader_main.c | 33 ++++++++++++++++++++++++ libs/vkd3d-shader/vkd3d_shader_private.h | 10 +++++++ 5 files changed, 63 insertions(+), 27 deletions(-) diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 319907227..dd9573877 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -4908,15 +4908,15 @@ static bool hlsl_ctx_init(struct hlsl_ctx *ctx, const struct vkd3d_shader_compil ctx->message_context = message_context; - if (!(ctx->source_files = hlsl_alloc(ctx, sizeof(*ctx->source_files)))) - return false; - if (!(ctx->source_files[0] = hlsl_strdup(ctx, compile_info->source_name ? compile_info->source_name : ""))) + vkd3d_shader_source_list_init(&ctx->source_files); + if (!vkd3d_shader_source_list_append(&ctx->source_files, + compile_info->source_name ? compile_info->source_name : "")) { - vkd3d_free(ctx->source_files); + vkd3d_shader_source_list_cleanup(&ctx->source_files); return false; } - ctx->source_files_count = 1; - ctx->location.source_name = ctx->source_files[0]; + + ctx->location.source_name = ctx->source_files.sources[0]; ctx->location.line = ctx->location.column = 1; vkd3d_string_buffer_cache_init(&ctx->string_buffers); @@ -4924,8 +4924,8 @@ static bool hlsl_ctx_init(struct hlsl_ctx *ctx, const struct vkd3d_shader_compil if (!(ctx->dummy_scope = hlsl_new_scope(ctx, NULL))) { - vkd3d_free((void *)ctx->source_files[0]); - vkd3d_free(ctx->source_files); + vkd3d_string_buffer_cache_cleanup(&ctx->string_buffers); + vkd3d_shader_source_list_cleanup(&ctx->source_files); return false; } hlsl_push_scope(ctx); @@ -5010,10 +5010,8 @@ static void hlsl_ctx_cleanup(struct hlsl_ctx *ctx) struct hlsl_type *type, *next_type; unsigned int i; - for (i = 0; i < ctx->source_files_count; ++i) - vkd3d_free((void *)ctx->source_files[i]); - vkd3d_free(ctx->source_files); vkd3d_string_buffer_cache_cleanup(&ctx->string_buffers); + vkd3d_shader_source_list_cleanup(&ctx->source_files); rb_destroy(&ctx->functions, free_function_rb, NULL); diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 34c63cd33..49cf2b8a9 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -1093,8 +1093,7 @@ struct hlsl_ctx { const struct hlsl_profile_info *profile; - const char **source_files; - unsigned int source_files_count; + struct vkd3d_shader_source_list source_files; /* Current location being read in the HLSL source, updated while parsing. */ struct vkd3d_shader_location location; /* Stores the logging messages and logging configuration. */ diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 66582e884..b37214911 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -7190,23 +7190,19 @@ declaration_statement_list: preproc_directive: PRE_LINE STRING { - const char **new_array = NULL; - - ctx->location.line = $1; if (strcmp($2, ctx->location.source_name)) - new_array = hlsl_realloc(ctx, ctx->source_files, - sizeof(*ctx->source_files) * (ctx->source_files_count + 1)); - - if (new_array) { - ctx->source_files = new_array; - ctx->source_files[ctx->source_files_count++] = $2; - ctx->location.source_name = $2; - } - else - { - vkd3d_free($2); + if (!vkd3d_shader_source_list_append(&ctx->source_files, $2)) + { + ctx->result = VKD3D_ERROR_OUT_OF_MEMORY; + } + else + { + ctx->location.line = $1; + ctx->location.source_name = ctx->source_files.sources[ctx->source_files.count - 1]; + } } + vkd3d_free($2); } struct_declaration_without_vars: diff --git a/libs/vkd3d-shader/vkd3d_shader_main.c b/libs/vkd3d-shader/vkd3d_shader_main.c index 6ef16e302..4f03dff15 100644 --- a/libs/vkd3d-shader/vkd3d_shader_main.c +++ b/libs/vkd3d-shader/vkd3d_shader_main.c @@ -57,6 +57,39 @@ uint32_t vkd3d_parse_integer(const char *s) return ret; } +bool vkd3d_shader_source_list_append(struct vkd3d_shader_source_list *l, const char *source) +{ + char *s; + + if (!(s = vkd3d_strdup(source))) + return false; + + if (!vkd3d_array_reserve((void **)&l->sources, &l->capacity, l->count + 1, sizeof(*l->sources))) + { + vkd3d_free(s); + return false; + } + l->sources[l->count++] = s; + + return true; +} + +void vkd3d_shader_source_list_cleanup(struct vkd3d_shader_source_list *l) +{ + size_t i; + + for (i = 0; i < l->count; ++i) + { + vkd3d_free((void *)l->sources[i]); + } + vkd3d_free(l->sources); +} + +void vkd3d_shader_source_list_init(struct vkd3d_shader_source_list *l) +{ + memset(l, 0, sizeof(*l)); +} + void vkd3d_string_buffer_init(struct vkd3d_string_buffer *buffer) { buffer->buffer_size = 16; diff --git a/libs/vkd3d-shader/vkd3d_shader_private.h b/libs/vkd3d-shader/vkd3d_shader_private.h index 9acc45b59..669fa8eea 100644 --- a/libs/vkd3d-shader/vkd3d_shader_private.h +++ b/libs/vkd3d-shader/vkd3d_shader_private.h @@ -937,6 +937,16 @@ enum vkd3d_shader_type struct vkd3d_shader_message_context; +struct vkd3d_shader_source_list +{ + const char **sources; + size_t capacity, count; +}; + +bool vkd3d_shader_source_list_append(struct vkd3d_shader_source_list *l, const char *source); +void vkd3d_shader_source_list_cleanup(struct vkd3d_shader_source_list *l); +void vkd3d_shader_source_list_init(struct vkd3d_shader_source_list *l); + struct vkd3d_shader_version { enum vkd3d_shader_type type;