From 99cc02685bb9d83d2cc789a0a0420d0651455bde Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 17 Mar 2021 12:31:36 +0200 Subject: [PATCH] cgen: implement IError.free() and use it for the builtin `none` const --- vlib/builtin/option.v | 24 ++++++++++++++++++++++++ vlib/v/gen/c/cgen.v | 7 +++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/vlib/builtin/option.v b/vlib/builtin/option.v index 753314ff61..1c301ac848 100644 --- a/vlib/builtin/option.v +++ b/vlib/builtin/option.v @@ -62,3 +62,27 @@ fn opt_ok(data voidptr, mut option Option, size int) { C.memcpy(byteptr(&option.err) + sizeof(IError), data, size) } } + +[unsafe] +pub fn (e &Error) free() { + unsafe { e.msg.free() } +} + +[unsafe] +pub fn (n &None__) free() { + unsafe { n.msg.free() } +} + +[typedef] +struct C.IError { + _object voidptr +} + +[unsafe] +pub fn (ie &IError) free() { + unsafe { + ie.msg.free() + cie := &C.IError(ie) + C.free(cie._object) + } +} diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 1801883acf..f9d8076273 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -975,8 +975,9 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) { stmt_pos = stmt.expr.position() } if stmt_pos.pos == 0 { - print('autofree: first stmt pos = 0. ') - println(stmt.type_name()) + $if trace_autofree ? { + println('autofree: first stmt pos = 0. $stmt.type_name()') + } return } } @@ -4849,6 +4850,8 @@ fn (mut g Gen) const_decl_init_later(mod string, name string, val string, typ ta g.cleanups[mod].writeln('\tstring_free(&$cname);') } else if sym.kind == .map { g.cleanups[mod].writeln('\tmap_free(&$cname);') + } else if styp == 'IError' { + g.cleanups[mod].writeln('\tIError_free(&$cname);') } } }