From b3bd77da56ce8aa225ee91565e4d1e640685728c Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Wed, 23 Jun 2010 19:04:45 -0700 Subject: [PATCH] glsl_type: Add a talloc-based new And hook it up at the two sites it's called. Note that with this change we still don't use glsl_type* objects as talloc contexts, (see things like get_array_instance that accept both a talloc 'ctx' as well as a glsl_type*). The reason for this is that the code is still using many instance of glsl_type objects not created with new. This closes 3 leaks in the glsl-orangebook-ch06-bump.frag test: total heap usage: 55,623 allocs, 55,618 Leaving only 5 leaks to go. --- ast_to_hir.cpp | 3 ++- glsl_types.cpp | 2 +- glsl_types.h | 23 +++++++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/ast_to_hir.cpp b/ast_to_hir.cpp index 38e344f0d25..ddd4b732662 100644 --- a/ast_to_hir.cpp +++ b/ast_to_hir.cpp @@ -2354,6 +2354,7 @@ ir_rvalue * ast_struct_specifier::hir(exec_list *instructions, struct _mesa_glsl_parse_state *state) { + void *ctx = talloc_parent(state); unsigned decl_count = 0; /* Make an initial pass over the list of structure fields to determine how @@ -2416,7 +2417,7 @@ ast_struct_specifier::hir(exec_list *instructions, name = this->name; } - glsl_type *t = new glsl_type(fields, decl_count, name); + glsl_type *t = new(ctx) glsl_type(fields, decl_count, name); YYLTYPE loc = this->get_location(); if (!state->symbols->add_type(name, t)) { diff --git a/glsl_types.cpp b/glsl_types.cpp index d1b9dc64121..bef267fa6be 100644 --- a/glsl_types.cpp +++ b/glsl_types.cpp @@ -702,7 +702,7 @@ glsl_type::get_array_instance(void *ctx, const glsl_type *base, const glsl_type *t = (glsl_type *) hash_table_find(array_types, & key); if (t == NULL) { - t = new glsl_type(ctx, base, array_size); + t = new(ctx) 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 baec763c42f..39e6ac970aa 100644 --- a/glsl_types.h +++ b/glsl_types.h @@ -29,6 +29,10 @@ #include #include +extern "C" { +#include +} + #define GLSL_TYPE_UINT 0 #define GLSL_TYPE_INT 1 #define GLSL_TYPE_FLOAT 2 @@ -61,6 +65,25 @@ struct glsl_type { * and \c GLSL_TYPE_UINT are valid. */ + /* Callers of this talloc-based new need not call delete. It's + * easier to just talloc_free 'ctx' (or any of its ancestors). */ + static void* operator new(size_t size, void *ctx) + { + void *type; + + type = talloc_size(ctx, size); + assert(type != NULL); + + return type; + } + + /* If the user *does* call delete, that's OK, we will just + * talloc_free in that case. */ + static void operator delete(void *type) + { + talloc_free(type); + } + /** * \name Vector and matrix element counts * -- 2.30.2