cgen: add a destructor caller for the generated _vcleanup, when using -shared (#8464)
parent
7eb7d042ec
commit
09c65163b4
|
@ -5009,9 +5009,10 @@ fn (mut g Gen) write_init_function() {
|
||||||
if g.pref.printfn_list.len > 0 && '_vinit' in g.pref.printfn_list {
|
if g.pref.printfn_list.len > 0 && '_vinit' in g.pref.printfn_list {
|
||||||
println(g.out.after(fn_vinit_start_pos))
|
println(g.out.after(fn_vinit_start_pos))
|
||||||
}
|
}
|
||||||
|
//
|
||||||
|
fn_vcleanup_start_pos := g.out.len
|
||||||
|
g.writeln('void _vcleanup() {')
|
||||||
if g.is_autofree {
|
if g.is_autofree {
|
||||||
fn_vcleanup_start_pos := g.out.len
|
|
||||||
g.writeln('void _vcleanup() {')
|
|
||||||
// g.writeln('puts("cleaning up...");')
|
// g.writeln('puts("cleaning up...");')
|
||||||
reversed_table_modules := g.table.modules.reverse()
|
reversed_table_modules := g.table.modules.reverse()
|
||||||
for mod_name in reversed_table_modules {
|
for mod_name in reversed_table_modules {
|
||||||
|
@ -5020,21 +5021,29 @@ fn (mut g Gen) write_init_function() {
|
||||||
}
|
}
|
||||||
// g.writeln('\tfree(g_str_buf);')
|
// g.writeln('\tfree(g_str_buf);')
|
||||||
g.writeln('\tarray_free(&as_cast_type_indexes);')
|
g.writeln('\tarray_free(&as_cast_type_indexes);')
|
||||||
g.writeln('}')
|
|
||||||
if g.pref.printfn_list.len > 0 && '_vcleanup' in g.pref.printfn_list {
|
|
||||||
println(g.out.after(fn_vcleanup_start_pos))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
g.writeln('}')
|
||||||
|
if g.pref.printfn_list.len > 0 && '_vcleanup' in g.pref.printfn_list {
|
||||||
|
println(g.out.after(fn_vcleanup_start_pos))
|
||||||
|
}
|
||||||
|
//
|
||||||
needs_constructor := g.pref.is_shared && g.pref.os != .windows
|
needs_constructor := g.pref.is_shared && g.pref.os != .windows
|
||||||
if needs_constructor {
|
if needs_constructor {
|
||||||
// shared libraries need a way to call _vinit/2. For that purpose,
|
// shared libraries need a way to call _vinit/2. For that purpose,
|
||||||
// provide a constructor, ensuring that all constants are initialized just once.
|
// provide a constructor/destructor pair, ensuring that all constants
|
||||||
|
// are initialized just once, and that they will be freed too.
|
||||||
// NB: os.args in this case will be [].
|
// NB: os.args in this case will be [].
|
||||||
g.writeln('__attribute__ ((constructor))')
|
g.writeln('__attribute__ ((constructor))')
|
||||||
g.writeln('void _vinit_caller() {')
|
g.writeln('void _vinit_caller() {')
|
||||||
g.writeln('\tstatic bool once = false; if (once) {return;} once = true;')
|
g.writeln('\tstatic bool once = false; if (once) {return;} once = true;')
|
||||||
g.writeln('\t_vinit(0,0);')
|
g.writeln('\t_vinit(0,0);')
|
||||||
g.writeln('}')
|
g.writeln('}')
|
||||||
|
|
||||||
|
g.writeln('__attribute__ ((destructor))')
|
||||||
|
g.writeln('void _vcleanup_caller() {')
|
||||||
|
g.writeln('\tstatic bool once = false; if (once) {return;} once = true;')
|
||||||
|
g.writeln('\t_vcleanup();')
|
||||||
|
g.writeln('}')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -78,9 +78,7 @@ fn (mut g Gen) gen_c_main_header() {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut g Gen) gen_c_main_footer() {
|
pub fn (mut g Gen) gen_c_main_footer() {
|
||||||
if g.is_autofree {
|
g.writeln('\t_vcleanup();')
|
||||||
g.writeln('\t_vcleanup();')
|
|
||||||
}
|
|
||||||
g.writeln('\treturn 0;')
|
g.writeln('\treturn 0;')
|
||||||
g.writeln('}')
|
g.writeln('}')
|
||||||
}
|
}
|
||||||
|
@ -160,9 +158,7 @@ pub fn (mut g Gen) gen_c_main_for_tests() {
|
||||||
if g.pref.is_stats {
|
if g.pref.is_stats {
|
||||||
g.writeln('\tmain__BenchedTests_end_testing(&bt);')
|
g.writeln('\tmain__BenchedTests_end_testing(&bt);')
|
||||||
}
|
}
|
||||||
if g.is_autofree {
|
g.writeln('\t_vcleanup();')
|
||||||
g.writeln('\t_vcleanup();')
|
|
||||||
}
|
|
||||||
g.writeln('\treturn g_test_fails > 0;')
|
g.writeln('\treturn g_test_fails > 0;')
|
||||||
g.writeln('}')
|
g.writeln('}')
|
||||||
if g.pref.printfn_list.len > 0 && 'main' in g.pref.printfn_list {
|
if g.pref.printfn_list.len > 0 && 'main' in g.pref.printfn_list {
|
||||||
|
|
Loading…
Reference in New Issue