From 8c3e3d1eac0676fc1598a4ea843bcc3374e2ae03 Mon Sep 17 00:00:00 2001 From: Arnaud DABY-SEESARAM Date: Tue, 20 Dec 2022 16:52:59 +0100 Subject: [PATCH] [C] malloc->calloc + conditions merged in free_state_* --- src/ast_to_c.ml | 2 +- src/cprint.ml | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/ast_to_c.ml b/src/ast_to_c.ml index 3bd4c28..cca059f 100644 --- a/src/ast_to_c.ml +++ b/src/ast_to_c.ml @@ -237,7 +237,7 @@ let cp_init_aux_nodes fmt (node, h) = | Some n -> begin Format.fprintf fmt "%a\t\tif(!state->is_reset) {\n\ - \t\t\tstate->aux_states[%d] = malloc (sizeof (%s));\n\ + \t\t\tstate->aux_states[%d] = calloc (1, sizeof (%s));\n\ \t\t}\n\ \t\t((%s*)(state->aux_states[%d]))->is_init = true;\n\ \t\t((%s*)(state->aux_states[%d]))->is_reset = state->is_reset;\n" diff --git a/src/cprint.ml b/src/cprint.ml index a47edcd..07013db 100644 --- a/src/cprint.ml +++ b/src/cprint.ml @@ -91,13 +91,16 @@ let cp_state_frees fmt (iprog, sts) = | None -> () | Some callee_name -> let callee_st = Hashtbl.find sts callee_name in - if callee_st.nt_count_app > 0 then - Format.fprintf fmt "\tif (st->aux_states[%d])\n\ - \t\tfree_state_%s((t_state_%s*)(st->aux_states[%d]));\n" - idx callee_name callee_name idx; - Format.fprintf fmt "\tif (st->aux_states[%d])\n\ - \t\tfree(st->aux_states[%d]);\n%a" - idx idx cp_free_aux (i+1, caller_name) + if callee_st.nt_count_app > 0 + then + Format.fprintf fmt "\tif (st->aux_states[%d]) {\n\ + \t\tfree_state_%s((t_state_%s*)(st->aux_states[%d]));\n\ + \t\tfree (st->aux_state[%d]);\n\t}\n%a" + idx callee_name callee_name idx + idx cp_free_aux (i+1, caller_name) + else Format.fprintf fmt "\tif (st->aux_states[%d])\n\ + \t\tfree(st->aux_states[%d]);\n%a" + idx idx cp_free_aux (i+1, caller_name) in Hashtbl.iter (fun node_name node_st ->