From 1660a2954797e056caba319c5d6c70b0d4be22fe Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Wed, 23 Jun 2010 18:11:51 -0700 Subject: exec_node: Add new talloc-based new() And fix all callers to use the tallbac-based new for exec_node construction. We make ready use of talloc_parent in order to get valid, (and appropriate) talloc owners for everything we construct without having to add new 'ctx' parameters up and down all the call trees. This closes the majority of the memory leaks in the glsl-orangebook-ch06-bump.frag test: total heap usage: 55,623 allocs, 42,672 frees (was 14,533 frees) Now 76.7% leak-free. Woo-hoo! --- ir_variable.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ir_variable.cpp') diff --git a/ir_variable.cpp b/ir_variable.cpp index efebe9199f..fabd856591 100644 --- a/ir_variable.cpp +++ b/ir_variable.cpp @@ -35,7 +35,7 @@ add_variable(const char *name, enum ir_variable_mode mode, int slot, const glsl_type *type, exec_list *instructions, glsl_symbol_table *symtab) { - ir_variable *var = new ir_variable(type, name); + ir_variable *var = new(symtab) ir_variable(type, name); var->mode = mode; switch (var->mode) { -- cgit v1.2.3 From 12c411504ca86341f8b96c349c15413ee198cc71 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Fri, 18 Jun 2010 17:52:59 -0700 Subject: Close memory leaks in glsl_type (constructor and get_array_instance) Add a talloc ctx to both get_array_instance and the glsl_type constructor in order to be able to call talloc_size instead of malloc. This fix now makes glsl-orangebook-ch06-bump.frag 99.99% leak free: total heap usage: 55,623 allocs, 55,615 Only 8 missing frees now. --- ast_function.cpp | 3 ++- ast_to_hir.cpp | 5 +++-- glsl_types.cpp | 11 ++++++----- glsl_types.h | 5 +++-- ir_reader.cpp | 2 +- ir_variable.cpp | 17 ++++++++++------- 6 files changed, 25 insertions(+), 18 deletions(-) (limited to 'ir_variable.cpp') diff --git a/ast_function.cpp b/ast_function.cpp index 9550d4d2f0..761af00b95 100644 --- a/ast_function.cpp +++ b/ast_function.cpp @@ -279,7 +279,8 @@ process_array_constructor(exec_list *instructions, if (constructor_type->length == 0) { constructor_type = - glsl_type::get_array_instance(constructor_type->element_type(), + glsl_type::get_array_instance(state, + constructor_type->element_type(), parameter_count); assert(constructor_type != NULL); assert(constructor_type->length == parameter_count); diff --git a/ast_to_hir.cpp b/ast_to_hir.cpp index eafc9e8114..38e344f0d2 100644 --- a/ast_to_hir.cpp +++ b/ast_to_hir.cpp @@ -506,7 +506,8 @@ do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state, var->max_array_access); } - var->type = glsl_type::get_array_instance(lhs->type->element_type(), + var->type = glsl_type::get_array_instance(state, + lhs->type->element_type(), rhs->type->array_size()); } } @@ -1409,7 +1410,7 @@ process_array_type(const glsl_type *base, ast_node *array_size, } } - return glsl_type::get_array_instance(base, length); + return glsl_type::get_array_instance(state, base, length); } diff --git a/glsl_types.cpp b/glsl_types.cpp index 7dcb4a4caf..d1b9dc6412 100644 --- a/glsl_types.cpp +++ b/glsl_types.cpp @@ -574,7 +574,7 @@ _mesa_glsl_initialize_constructors(exec_list *instructions, } -glsl_type::glsl_type(const glsl_type *array, unsigned length) : +glsl_type::glsl_type(void *ctx, const glsl_type *array, unsigned length) : base_type(GLSL_TYPE_ARRAY), sampler_dimensionality(0), sampler_shadow(0), sampler_array(0), sampler_type(0), @@ -588,7 +588,7 @@ glsl_type::glsl_type(const glsl_type *array, unsigned length) : * NUL. */ const unsigned name_length = strlen(array->name) + 10 + 3; - char *const n = (char *) malloc(name_length); + char *const n = (char *) talloc_size(ctx, name_length); if (length == 0) snprintf(n, name_length, "%s[]", array->name); @@ -691,9 +691,10 @@ glsl_type::array_key_hash(const void *a) const glsl_type * -glsl_type::get_array_instance(const glsl_type *base, unsigned array_size) +glsl_type::get_array_instance(void *ctx, const glsl_type *base, + unsigned array_size) { - const glsl_type key(base, array_size); + const glsl_type key(ctx, base, array_size); if (array_types == NULL) { array_types = hash_table_ctor(64, array_key_hash, array_key_compare); @@ -701,7 +702,7 @@ glsl_type::get_array_instance(const glsl_type *base, unsigned array_size) const glsl_type *t = (glsl_type *) hash_table_find(array_types, & key); if (t == NULL) { - t = new glsl_type(base, array_size); + t = new glsl_type(ctx, base, array_size); hash_table_insert(array_types, (void *) t, t); } diff --git a/glsl_types.h b/glsl_types.h index 939c173fd4..baec763c42 100644 --- a/glsl_types.h +++ b/glsl_types.h @@ -198,7 +198,8 @@ struct glsl_type { /** * Get the instance of an array type */ - static const glsl_type *get_array_instance(const glsl_type *base, + static const glsl_type *get_array_instance(void *ctx, + const glsl_type *base, unsigned elements); /** @@ -387,7 +388,7 @@ private: /** * Constructor for array types */ - glsl_type(const glsl_type *array, unsigned length); + glsl_type(void *ctx, const glsl_type *array, unsigned length); /** Hash table containing the known array types. */ static struct hash_table *array_types; diff --git a/ir_reader.cpp b/ir_reader.cpp index d6985c4981..7383c42cbc 100644 --- a/ir_reader.cpp +++ b/ir_reader.cpp @@ -138,7 +138,7 @@ read_type(_mesa_glsl_parse_state *st, s_expression *expr) return NULL; } - return glsl_type::get_array_instance(base_type, size->value()); + return glsl_type::get_array_instance(st, base_type, size->value()); } else if (strcmp(type_sym->value(), "struct") == 0) { assert(false); // FINISHME } else { diff --git a/ir_variable.cpp b/ir_variable.cpp index fabd856591..15a4a92f62 100644 --- a/ir_variable.cpp +++ b/ir_variable.cpp @@ -104,7 +104,7 @@ generate_110_uniforms(exec_list *instructions, * FINISHME: for now. */ const glsl_type *const mat4_array_type = - glsl_type::get_array_instance(glsl_type::mat4_type, 4); + glsl_type::get_array_instance(symtab, glsl_type::mat4_type, 4); add_variable("gl_TextureMatrix", ir_var_uniform, -1, mat4_array_type, instructions, symtab); @@ -122,7 +122,8 @@ generate_110_uniforms(exec_list *instructions, * FINISHME: at least 8, so hard-code 8 for now. */ const glsl_type *const light_source_array_type = - glsl_type::get_array_instance(symtab->get_type("gl_LightSourceParameters"), 8); + glsl_type::get_array_instance(symtab, + symtab->get_type("gl_LightSourceParameters"), 8); add_variable("gl_LightSource", ir_var_uniform, -1, light_source_array_type, instructions, symtab); @@ -157,7 +158,7 @@ generate_110_vs_variables(exec_list *instructions, * FINISHME: for now. */ const glsl_type *const vec4_array_type = - glsl_type::get_array_instance(glsl_type::vec4_type, 4); + glsl_type::get_array_instance(symtab, glsl_type::vec4_type, 4); add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type, instructions, symtab); @@ -179,6 +180,7 @@ static void generate_130_vs_variables(exec_list *instructions, glsl_symbol_table *symtab) { + void *ctx = symtab; generate_120_vs_variables(instructions, symtab); for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) { @@ -190,7 +192,7 @@ generate_130_vs_variables(exec_list *instructions, * FINISHME: the value of GL_MAX_CLIP_DISTANCES. */ const glsl_type *const clip_distance_array_type = - glsl_type::get_array_instance(glsl_type::float_type, 8); + glsl_type::get_array_instance(ctx, glsl_type::float_type, 8); /* FINISHME: gl_ClipDistance needs a real location assigned. */ add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type, @@ -240,7 +242,7 @@ generate_110_fs_variables(exec_list *instructions, * FINISHME: for now. */ const glsl_type *const vec4_array_type = - glsl_type::get_array_instance(glsl_type::vec4_type, 4); + glsl_type::get_array_instance(symtab, glsl_type::vec4_type, 4); add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type, instructions, symtab); @@ -256,7 +258,7 @@ generate_ARB_draw_buffers_fs_variables(exec_list *instructions, * FINISHME: at least 1, so hard-code 1 for now. */ const glsl_type *const vec4_array_type = - glsl_type::get_array_instance(glsl_type::vec4_type, 1); + glsl_type::get_array_instance(symtab, glsl_type::vec4_type, 1); ir_variable *const fd = add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0, @@ -279,13 +281,14 @@ static void generate_130_fs_variables(exec_list *instructions, glsl_symbol_table *symtab) { + void *ctx = symtab; generate_120_fs_variables(instructions, symtab); /* FINISHME: The size of this array is implementation dependent based on * FINISHME: the value of GL_MAX_CLIP_DISTANCES. */ const glsl_type *const clip_distance_array_type = - glsl_type::get_array_instance(glsl_type::float_type, 8); + glsl_type::get_array_instance(ctx, glsl_type::float_type, 8); /* FINISHME: gl_ClipDistance needs a real location assigned. */ add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type, -- cgit v1.2.3