Hello. Attached patch removes all memory leaks which come from HSA-related source files.
Thanks, Martin gcc/ChangeLog: 2014-12-05 Martin Liska <mli...@suse.cz> * hsa-brig.c (brig_string_slot_hasher::remove): Memory free is added. * hsa-gen.c (hsa_deinit_data_for_cfun): Destructors are called for operands and instructions that need to deallocate a data. (hsa_alloc_reg_op): Object is added to list of items that are destructed. (hsa_alloc_code_list_op): Likewise. (hsa_alloc_call_insn): Likewise. (hsa_alloc_call_block_insn): Likewise. (wrap_hsa): Products of asprintf are freed. * hsa.h (struct hsa_op_reg): New destructor. (struct hsa_op_code_list): Likewise. (struct hsa_insn_call): Likewise. (struct hsa_insn_call_block): Likewise. --- gcc/hsa-brig.c | 1 + gcc/hsa-gen.c | 32 +++++++++++++++++++++++++++++++- gcc/hsa.h | 25 ++++++++++++++++++++----- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/gcc/hsa-brig.c b/gcc/hsa-brig.c index 61eadf9..3e6ed68 100644 --- a/gcc/hsa-brig.c +++ b/gcc/hsa-brig.c @@ -275,6 +275,7 @@ inline void brig_string_slot_hasher::remove (value_type *ds) { free (const_cast<char*> (ds->s)); + free (ds); } static hash_table<brig_string_slot_hasher> *brig_string_htab; diff --git a/gcc/hsa-gen.c b/gcc/hsa-gen.c index 5029d26..ac51eb0 100644 --- a/gcc/hsa-gen.c +++ b/gcc/hsa-gen.c @@ -84,6 +84,13 @@ static alloc_pool hsa_allocp_inst_call_block; static alloc_pool hsa_allocp_bb; static alloc_pool hsa_allocp_symbols; +/* Vectors with selected instructions and operands that need + a destruction. */ +static vec <hsa_op_code_list *> hsa_list_operand_code_list; +static vec <hsa_op_reg *> hsa_list_operand_reg; +static vec <hsa_insn_call_block *> hsa_list_insn_call_block; +static vec <hsa_insn_call *> hsa_list_insn_call; + /* Hash function to lookup a symbol for a decl. */ hash_table <hsa_free_symbol_hasher> *hsa_global_variable_symbols; @@ -194,6 +201,23 @@ hsa_deinit_data_for_cfun (void) FOR_EACH_BB_FN (bb, cfun) bb->aux = NULL; + for (unsigned int i = 0; i < hsa_list_operand_code_list.length (); i++) + hsa_list_operand_code_list[i]->~hsa_op_code_list (); + + for (unsigned int i = 0; i < hsa_list_operand_reg.length (); i++) + hsa_list_operand_reg[i]->~hsa_op_reg (); + + for (unsigned int i = 0; i < hsa_list_insn_call_block.length (); i++) + hsa_list_insn_call_block[i]->~hsa_insn_call_block (); + + for (unsigned int i = 0; i < hsa_list_insn_call.length (); i++) + hsa_list_insn_call[i]->~hsa_insn_call (); + + hsa_list_operand_code_list.release (); + hsa_list_operand_reg.release (); + hsa_list_insn_call_block.release (); + hsa_list_insn_call.release (); + free_alloc_pool (hsa_allocp_operand_address); free_alloc_pool (hsa_allocp_operand_immed); free_alloc_pool (hsa_allocp_operand_reg); @@ -572,11 +596,11 @@ hsa_alloc_reg_op (void) hsa_op_reg *hreg; hreg = (hsa_op_reg *) pool_alloc (hsa_allocp_operand_reg); + hsa_list_operand_reg.safe_push (hreg); memset (hreg, 0, sizeof (hsa_op_reg)); hreg->kind = BRIG_KIND_OPERAND_REG; /* TODO: Try removing later on. I suppose this is not necessary but I'd rather avoid surprises. */ - hreg->uses = vNULL; hreg->order = hsa_cfun.reg_count++; return hreg; } @@ -604,6 +628,7 @@ hsa_alloc_code_list_op (unsigned elements) { hsa_op_code_list *list; list = (hsa_op_code_list *) pool_alloc (hsa_allocp_operand_code_list); + hsa_list_operand_code_list.safe_push (list); memset (list, 0, sizeof (hsa_op_code_list)); list->kind = BRIG_KIND_OPERAND_CODE_LIST; @@ -748,6 +773,7 @@ hsa_alloc_call_insn (void) hsa_insn_call *call; call = (hsa_insn_call *) pool_alloc (hsa_allocp_inst_call); + hsa_list_insn_call.safe_push (call); memset (call, 0, sizeof (hsa_insn_call)); return call; } @@ -760,6 +786,7 @@ hsa_alloc_call_block_insn (void) hsa_insn_call_block *call_block; call_block = (hsa_insn_call_block *) pool_alloc (hsa_allocp_inst_call_block); + hsa_list_insn_call_block.safe_push (call_block); memset (call_block, 0, sizeof (hsa_insn_call_block)); call_block->opcode = HSA_OPCODE_CALL_BLOCK; @@ -2332,7 +2359,9 @@ wrap_hsa (void) strcpy (extension, "\0"); asprintf (&extension, "%s", ".o\0"); strcat (filename, extension); + free (extension); str = build_string_literal (strlen(filename)+1,filename); + free (filename); } } CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, str); @@ -2345,6 +2374,7 @@ wrap_hsa (void) sanitize_hsa_name (tmpname + 1); str = build_string_literal (slen + 2, tmpname); + free (tmpname); CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, str); int discard_arguents; int num_args = gimple_call_num_args (call_stmt); diff --git a/gcc/hsa.h b/gcc/hsa.h index 0cce181..525ef6e 100644 --- a/gcc/hsa.h +++ b/gcc/hsa.h @@ -120,7 +120,7 @@ struct hsa_op_reg : public hsa_op_base /* Defining instrution while still in the SSA. */ hsa_insn_basic *def_insn; /* Uses of the value while still in SSA. */ - vec <hsa_insn_basic_p> uses; + auto_vec <hsa_insn_basic_p> uses; /* If the register allocator decides to spill the register, this is the appropriate spill symbol. */ @@ -198,9 +198,14 @@ is_a_helper <hsa_op_code_ref *>::test (hsa_op_base *p) /* Code list HSA operand. */ struct hsa_op_code_list: public hsa_op_base { + /* Destructor. */ + ~hsa_op_code_list () + { + } + /* Offset to variable-sized array in hsa_data section, where are offsets to entries in the hsa_code section. */ - vec<unsigned> offsets; + auto_vec<unsigned> offsets; }; /* Report whether or not P is a code list operand. */ @@ -401,6 +406,11 @@ is_a_helper <hsa_insn_seg *>::test (hsa_insn_basic *p) struct hsa_insn_call: hsa_insn_basic { + /* Destructor. */ + ~hsa_insn_call () + { + } + /* Called function */ tree called_function; @@ -408,7 +418,7 @@ struct hsa_insn_call: hsa_insn_basic struct hsa_op_code_ref func; /* Argument symbols. */ - vec <hsa_symbol *> args_symbols; + auto_vec <hsa_symbol *> args_symbols; /* Code list for arguments of the function. */ hsa_op_code_list *args_code_list; @@ -438,11 +448,16 @@ is_a_helper <hsa_insn_call *>::test (hsa_insn_basic *p) struct hsa_insn_call_block: hsa_insn_basic { + /* Destructor. */ + ~hsa_insn_call_block () + { + } + /* Input formal arguments. */ - vec <hsa_symbol *> input_args; + auto_vec <hsa_symbol *> input_args; /* Input arguments store instructions. */ - vec <hsa_insn_mem *> input_arg_insns; + auto_vec <hsa_insn_mem *> input_arg_insns; /* Output argument, can be NULL for void functions. */ hsa_symbol *output_arg; -- 2.1.2