@@ -372,24 +372,6 @@ irep_idt recursive_initializationt::build_constructor(const exprt &expr)
372
372
return type_constructor_names.at (key);
373
373
}
374
374
375
- symbol_exprt recursive_initializationt::get_malloc_function ()
376
- {
377
- auto malloc_sym = goto_model.symbol_table .lookup (" malloc" );
378
- if (malloc_sym == nullptr )
379
- {
380
- symbolt new_malloc_sym{
381
- " malloc" ,
382
- code_typet{
383
- {code_typet::parametert{size_type ()}}, pointer_type (empty_typet{})},
384
- initialization_config.mode };
385
- new_malloc_sym.pretty_name = " malloc" ;
386
- new_malloc_sym.base_name = " malloc" ;
387
- goto_model.symbol_table .insert (new_malloc_sym);
388
- return new_malloc_sym.symbol_expr ();
389
- }
390
- return malloc_sym->symbol_expr ();
391
- }
392
-
393
375
bool recursive_initializationt::should_be_treated_as_array (
394
376
const irep_idt &array_name) const
395
377
{
@@ -630,24 +612,6 @@ std::string recursive_initializationt::type2id(const typet &type) const
630
612
return " " ;
631
613
}
632
614
633
- symbol_exprt recursive_initializationt::get_free_function ()
634
- {
635
- auto free_sym = goto_model.symbol_table .lookup (" free" );
636
- if (free_sym == nullptr )
637
- {
638
- symbolt new_free_sym{
639
- " free" ,
640
- code_typet{
641
- {code_typet::parametert{pointer_type (empty_typet{})}}, empty_typet{}},
642
- initialization_config.mode };
643
- new_free_sym.pretty_name = " free" ;
644
- new_free_sym.base_name = " free" ;
645
- goto_model.symbol_table .insert (new_free_sym);
646
- return new_free_sym.symbol_expr ();
647
- }
648
- return free_sym->symbol_expr ();
649
- }
650
-
651
615
code_blockt recursive_initializationt::build_pointer_constructor (
652
616
const exprt &depth,
653
617
const symbol_exprt &result)
@@ -724,10 +688,13 @@ code_blockt recursive_initializationt::build_pointer_constructor(
724
688
725
689
then_case.add (code_declt{local_result});
726
690
const namespacet ns{goto_model.symbol_table };
727
- then_case.add (code_function_callt {
691
+ then_case.add (code_assignt {
728
692
local_result,
729
- get_malloc_function (),
730
- {*size_of_expr (non_const_pointer_type.base_type (), ns)}});
693
+ side_effect_exprt{
694
+ ID_allocate,
695
+ {*size_of_expr (non_const_pointer_type.base_type (), ns), false_exprt{}},
696
+ non_const_pointer_type,
697
+ source_locationt::nil ()}});
731
698
initialize (
732
699
dereference_exprt{local_result},
733
700
plus_exprt{depth, from_integer (1 , depth.type ())},
@@ -869,10 +836,16 @@ code_blockt recursive_initializationt::build_dynamic_array_constructor(
869
836
{
870
837
body.add (code_ifthenelset{
871
838
equal_exprt{nondet_size, from_integer (array_size, nondet_size.type ())},
872
- code_function_callt{local_result,
873
- get_malloc_function (),
874
- {mult_exprt{from_integer (array_size, size_type ()),
875
- *size_of_expr (element_type, ns)}}}});
839
+ code_assignt{
840
+ local_result,
841
+ side_effect_exprt{
842
+ ID_allocate,
843
+ {mult_exprt{
844
+ from_integer (array_size, size_type ()),
845
+ *size_of_expr (element_type, ns)},
846
+ false_exprt{}},
847
+ mutable_dynamic_array_type,
848
+ source_locationt::nil ()}}});
876
849
}
877
850
878
851
const symbol_exprt &index_iter = get_fresh_local_symexpr (" index" );
@@ -942,18 +915,35 @@ bool recursive_initializationt::needs_freeing(const exprt &expr) const
942
915
return true ;
943
916
}
944
917
918
+ code_blockt
919
+ recursive_initializationt::deallocate_code (const exprt &pointer) const
920
+ {
921
+ code_blockt block;
922
+ const auto should_track =
923
+ get_fresh_local_typed_symexpr (" mark_deallocated" , bool_typet{});
924
+ block.add (code_declt{should_track});
925
+ const symbol_exprt deallocated = goto_model.get_symbol_table ()
926
+ .lookup_ref (CPROVER_PREFIX " deallocated" )
927
+ .symbol_expr ();
928
+ block.add (code_ifthenelset{
929
+ should_track,
930
+ code_assignt{
931
+ deallocated,
932
+ typecast_exprt::conditional_cast (pointer, deallocated.type ())}});
933
+ return block;
934
+ }
935
+
945
936
void recursive_initializationt::free_if_possible (
946
937
const exprt &expr,
947
938
code_blockt &body)
948
939
{
949
940
PRECONDITION (expr.id () == ID_symbol);
950
941
const auto expr_id = to_symbol_expr (expr).get_identifier ();
951
942
const auto maybe_cluster_index = find_equal_cluster (expr_id);
952
- const auto call_free = code_function_callt{get_free_function (), {expr}};
953
943
if (!maybe_cluster_index.has_value ())
954
944
{
955
945
// not in any equality cluster -> just free
956
- body.add (call_free );
946
+ body.add (deallocate_code (expr) );
957
947
return ;
958
948
}
959
949
@@ -965,7 +955,7 @@ void recursive_initializationt::free_if_possible(
965
955
// in equality cluster but not common origin -> free if not equal to origin
966
956
const auto condition =
967
957
notequal_exprt{expr, *common_arguments_origins[*maybe_cluster_index]};
968
- body.add (code_ifthenelset{condition, call_free });
958
+ body.add (code_ifthenelset{condition, deallocate_code (expr) });
969
959
}
970
960
else
971
961
{
@@ -979,7 +969,7 @@ void recursive_initializationt::free_cluster_origins(code_blockt &body)
979
969
{
980
970
for (auto const &origin : common_arguments_origins)
981
971
{
982
- body.add (code_function_callt{ get_free_function (), { *origin}} );
972
+ body.add (deallocate_code ( *origin) );
983
973
}
984
974
}
985
975
0 commit comments