1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: add a destructor caller for the generated _vcleanup, when using -shared (#8464)

This commit is contained in:
Seven Du 2021-01-31 18:10:49 +08:00 committed by GitHub
parent 7eb7d042ec
commit 09c65163b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 13 deletions

View File

@ -5009,9 +5009,10 @@ fn (mut g Gen) write_init_function() {
if g.pref.printfn_list.len > 0 && '_vinit' in g.pref.printfn_list {
println(g.out.after(fn_vinit_start_pos))
}
//
fn_vcleanup_start_pos := g.out.len
g.writeln('void _vcleanup() {')
if g.is_autofree {
fn_vcleanup_start_pos := g.out.len
g.writeln('void _vcleanup() {')
// g.writeln('puts("cleaning up...");')
reversed_table_modules := g.table.modules.reverse()
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('\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
if needs_constructor {
// 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 [].
g.writeln('__attribute__ ((constructor))')
g.writeln('void _vinit_caller() {')
g.writeln('\tstatic bool once = false; if (once) {return;} once = true;')
g.writeln('\t_vinit(0,0);')
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('}')
}
}

View File

@ -78,9 +78,7 @@ fn (mut g Gen) gen_c_main_header() {
}
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('}')
}
@ -160,9 +158,7 @@ pub fn (mut g Gen) gen_c_main_for_tests() {
if g.pref.is_stats {
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('}')
if g.pref.printfn_list.len > 0 && 'main' in g.pref.printfn_list {