From 3fcc5fdf5ec6e9f9c8064fb5cf36e9d3b135235f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tapani=20P=C3=A4lli?= Date: Mon, 12 Feb 2018 14:00:09 +0200 Subject: [PATCH] glsl: free memory for temporary glsl_type variables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Otherwise it is only freed at the very end during atexit(), this will cause memory consumption with servers using our compiler. Signed-off-by: Tapani Pälli Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=104884 --- src/compiler/glsl_types.cpp | 46 +++++++++++++++++++++++++++++++-------------- src/compiler/glsl_types.h | 9 +++++---- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/compiler/glsl_types.cpp b/src/compiler/glsl_types.cpp index 3cc5eb0495..144321f70a 100644 --- a/src/compiler/glsl_types.cpp +++ b/src/compiler/glsl_types.cpp @@ -100,7 +100,7 @@ glsl_type::glsl_type(GLenum gl_type, glsl_base_type base_type, } glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields, - const char *name) : + const char *name, void *ralloc_ctx) : gl_type(0), base_type(GLSL_TYPE_STRUCT), sampled_type(GLSL_TYPE_VOID), sampler_dimensionality(0), sampler_shadow(0), sampler_array(0), @@ -109,13 +109,14 @@ glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields, length(num_fields) { unsigned int i; + void *mem_ctx = ralloc_ctx ? ralloc_ctx : this->mem_ctx; mtx_lock(&glsl_type::mem_mutex); init_ralloc_type_ctx(); assert(name != NULL); - this->name = ralloc_strdup(this->mem_ctx, name); - this->fields.structure = ralloc_array(this->mem_ctx, + this->name = ralloc_strdup(mem_ctx, name); + this->fields.structure = ralloc_array(mem_ctx, glsl_struct_field, length); for (i = 0; i < length; i++) { @@ -129,7 +130,7 @@ glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields, glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields, enum glsl_interface_packing packing, - bool row_major, const char *name) : + bool row_major, const char *name, void *ralloc_ctx) : gl_type(0), base_type(GLSL_TYPE_INTERFACE), sampled_type(GLSL_TYPE_VOID), sampler_dimensionality(0), sampler_shadow(0), sampler_array(0), @@ -139,13 +140,14 @@ glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields, length(num_fields) { unsigned int i; + void *mem_ctx = ralloc_ctx ? ralloc_ctx : this->mem_ctx; mtx_lock(&glsl_type::mem_mutex); init_ralloc_type_ctx(); assert(name != NULL); - this->name = ralloc_strdup(this->mem_ctx, name); - this->fields.structure = rzalloc_array(this->mem_ctx, + this->name = ralloc_strdup(mem_ctx, name); + this->fields.structure = rzalloc_array(mem_ctx, glsl_struct_field, length); for (i = 0; i < length; i++) { this->fields.structure[i] = fields[i]; @@ -157,7 +159,8 @@ glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields, } glsl_type::glsl_type(const glsl_type *return_type, - const glsl_function_param *params, unsigned num_params) : + const glsl_function_param *params, unsigned num_params, + void *ralloc_ctx) : gl_type(0), base_type(GLSL_TYPE_FUNCTION), sampled_type(GLSL_TYPE_VOID), sampler_dimensionality(0), sampler_shadow(0), sampler_array(0), @@ -166,12 +169,13 @@ glsl_type::glsl_type(const glsl_type *return_type, length(num_params) { unsigned int i; + void *mem_ctx = ralloc_ctx ? ralloc_ctx : this->mem_ctx; mtx_lock(&glsl_type::mem_mutex); init_ralloc_type_ctx(); - this->fields.parameters = rzalloc_array(this->mem_ctx, + this->fields.parameters = rzalloc_array(mem_ctx, glsl_function_param, num_params + 1); /* We store the return type as the first parameter */ @@ -189,7 +193,7 @@ glsl_type::glsl_type(const glsl_type *return_type, mtx_unlock(&glsl_type::mem_mutex); } -glsl_type::glsl_type(const char *subroutine_name) : +glsl_type::glsl_type(const char *subroutine_name, void *ralloc_ctx) : gl_type(0), base_type(GLSL_TYPE_SUBROUTINE), sampled_type(GLSL_TYPE_VOID), sampler_dimensionality(0), sampler_shadow(0), sampler_array(0), @@ -197,11 +201,12 @@ glsl_type::glsl_type(const char *subroutine_name) : vector_elements(1), matrix_columns(1), length(0) { + void *mem_ctx = ralloc_ctx ? ralloc_ctx : this->mem_ctx; mtx_lock(&glsl_type::mem_mutex); init_ralloc_type_ctx(); assert(subroutine_name != NULL); - this->name = ralloc_strdup(this->mem_ctx, subroutine_name); + this->name = ralloc_strdup(mem_ctx, subroutine_name); mtx_unlock(&glsl_type::mem_mutex); } @@ -1108,7 +1113,8 @@ glsl_type::get_record_instance(const glsl_struct_field *fields, unsigned num_fields, const char *name) { - const glsl_type key(fields, num_fields, name); + void *local = ralloc_context(NULL); + const glsl_type key(fields, num_fields, name, local); mtx_lock(&glsl_type::hash_mutex); @@ -1131,6 +1137,8 @@ glsl_type::get_record_instance(const glsl_struct_field *fields, mtx_unlock(&glsl_type::hash_mutex); + ralloc_free(local); + return (glsl_type *) entry->data; } @@ -1142,7 +1150,9 @@ glsl_type::get_interface_instance(const glsl_struct_field *fields, bool row_major, const char *block_name) { - const glsl_type key(fields, num_fields, packing, row_major, block_name); + void *local = ralloc_context(NULL); + const glsl_type key(fields, num_fields, packing, row_major, block_name, + local); mtx_lock(&glsl_type::hash_mutex); @@ -1166,13 +1176,16 @@ glsl_type::get_interface_instance(const glsl_struct_field *fields, mtx_unlock(&glsl_type::hash_mutex); + ralloc_free(local); + return (glsl_type *) entry->data; } const glsl_type * glsl_type::get_subroutine_instance(const char *subroutine_name) { - const glsl_type key(subroutine_name); + void *local = ralloc_context(NULL); + const glsl_type key(subroutine_name, local); mtx_lock(&glsl_type::hash_mutex); @@ -1194,6 +1207,8 @@ glsl_type::get_subroutine_instance(const char *subroutine_name) mtx_unlock(&glsl_type::hash_mutex); + ralloc_free(local); + return (glsl_type *) entry->data; } @@ -1225,7 +1240,8 @@ glsl_type::get_function_instance(const glsl_type *return_type, const glsl_function_param *params, unsigned num_params) { - const glsl_type key(return_type, params, num_params); + void *local = ralloc_context(NULL); + const glsl_type key(return_type, params, num_params, local); mtx_lock(&glsl_type::hash_mutex); @@ -1248,6 +1264,8 @@ glsl_type::get_function_instance(const glsl_type *return_type, mtx_unlock(&glsl_type::hash_mutex); + ralloc_free(local); + return t; } diff --git a/src/compiler/glsl_types.h b/src/compiler/glsl_types.h index ab0b263764..3a75e49c89 100644 --- a/src/compiler/glsl_types.h +++ b/src/compiler/glsl_types.h @@ -899,22 +899,23 @@ private: /** Constructor for record types */ glsl_type(const glsl_struct_field *fields, unsigned num_fields, - const char *name); + const char *name, void *ralloc_ctx = NULL); /** Constructor for interface types */ glsl_type(const glsl_struct_field *fields, unsigned num_fields, enum glsl_interface_packing packing, - bool row_major, const char *name); + bool row_major, const char *name, void *ralloc_ctx = NULL); /** Constructor for interface types */ glsl_type(const glsl_type *return_type, - const glsl_function_param *params, unsigned num_params); + const glsl_function_param *params, unsigned num_params, + void *ralloc_ctx = NULL); /** Constructor for array types */ glsl_type(const glsl_type *array, unsigned length); /** Constructor for subroutine types */ - glsl_type(const char *name); + glsl_type(const char *name, void *ralloc_ctx = NULL); /** Hash table containing the known array types. */ static struct hash_table *array_types; -- 2.14.3