From a96ece7b3fc2821c234710458ec9ab1b8fbc737a Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Tue, 20 Dec 2011 17:16:03 +0100 Subject: [PATCH] typerep: freeing a type frees contained entities Types "own" entities, so they should also free them when they get freed. --- include/libfirm/typerep.h | 17 ++++------------- ir/ir/irprog.c | 4 ++-- ir/tr/type.c | 32 ++++++++++++++++---------------- ir/tr/type_t.h | 3 ++- 4 files changed, 24 insertions(+), 32 deletions(-) diff --git a/include/libfirm/typerep.h b/include/libfirm/typerep.h index 59e61b984..c409f04c4 100644 --- a/include/libfirm/typerep.h +++ b/include/libfirm/typerep.h @@ -1070,20 +1070,11 @@ FIRM_API int check_type(ir_type *tp); */ FIRM_API int tr_verify(void); -/** Frees all entities associated with a type. - * Does not free the array entity. - * Warning: ensure these entities are not referenced anywhere else. - */ -FIRM_API void free_type_entities(ir_type *tp); - -/** Frees the memory used by the type. +/** + * Frees the memory used by the type. * - * Removes the type from the type list. Does not free the entities - * belonging to the type, except for the array element entity. Does - * not free if tp is "none" or "unknown". Frees entities in value - * param subtypes of method types!!! Make sure these are not - * referenced any more. Further make sure there is no pointer type - * that refers to this type. + * Removes the type from the type list and frees all entities + * belonging to the type. */ FIRM_API void free_type(ir_type *tp); diff --git a/ir/ir/irprog.c b/ir/ir/irprog.c index 1b26a7e0c..bd5a73c2c 100644 --- a/ir/ir/irprog.c +++ b/ir/ir/irprog.c @@ -131,8 +131,8 @@ void free_ir_prog(void) for (i = get_irp_n_irgs(); i > 0;) free_ir_graph(get_irp_irg(--i)); - free_type_entities(get_glob_type()); - /* must iterate backwards here */ + /* free entities first to avoid entity types being destroyed before + * the entities using them */ for (i = get_irp_n_types(); i > 0;) free_type_entities(get_irp_type(--i)); diff --git a/ir/tr/type.c b/ir/tr/type.c index f28e687bd..4e0812640 100644 --- a/ir/tr/type.c +++ b/ir/tr/type.c @@ -145,10 +145,26 @@ ir_type *new_type(const tp_op *type_op, ir_mode *mode, type_dbg_info *db) return res; } +void free_type_entities(ir_type *tp) +{ + const tp_op *op = get_type_tpop(tp); + if (op->ops.free_entities != NULL) + op->ops.free_entities(tp); +} + +static void free_type_attrs(ir_type *tp) +{ + const tp_op *tpop = get_type_tpop(tp); + + if (tpop->ops.free_attrs) + tpop->ops.free_attrs(tp); +} + void free_type(ir_type *tp) { const tp_op *op = get_type_tpop(tp); + free_type_entities(tp); /* Remove from list of all types */ remove_irp_type(tp); /* Free the attributes of the type. */ @@ -163,22 +179,6 @@ void free_type(ir_type *tp) free(tp); } -void free_type_entities(ir_type *tp) -{ - const tp_op *tpop = get_type_tpop(tp); - - if (tpop->ops.free_entities) - tpop->ops.free_entities(tp); -} - -void free_type_attrs(ir_type *tp) -{ - const tp_op *tpop = get_type_tpop(tp); - - if (tpop->ops.free_attrs) - tpop->ops.free_attrs(tp); -} - void *(get_type_link)(const ir_type *tp) { return _get_type_link(tp); diff --git a/ir/tr/type_t.h b/ir/tr/type_t.h index 19685bde2..b5f3fd7ee 100644 --- a/ir/tr/type_t.h +++ b/ir/tr/type_t.h @@ -196,7 +196,8 @@ struct ir_type { * initialized. The type is in state layout_undefined. */ ir_type *new_type(const tp_op *type_op, ir_mode *mode, type_dbg_info *db); -void free_type_attrs(ir_type *tp); + +void free_type_entities(ir_type *tp); void free_class_entities (ir_type *clss); void free_struct_entities (ir_type *strct); -- 2.20.1