[vala] Improve readability of reference counting
- From: Jürg Billeter <juergbi src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [vala] Improve readability of reference counting
- Date: Thu, 17 Sep 2009 11:23:51 +0000 (UTC)
commit 599bebc78755313cb08e2ab62b2053e0bf69b40a
Author: Jürg Billeter <j bitron ch>
Date: Thu Sep 17 12:09:02 2009 +0200
Improve readability of reference counting
Fixes bug 472740.
codegen/valaccodearraymodule.vala | 6 ++--
codegen/valaccodebasemodule.vala | 47 ++++++++++++++++++++++++++++++++++++-
2 files changed, 49 insertions(+), 4 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index 2121dc7..d21c428 100644
--- a/codegen/valaccodearraymodule.vala
+++ b/codegen/valaccodearraymodule.vala
@@ -645,12 +645,12 @@ internal class Vala.CCodeArrayModule : CCodeMethodCallModule {
}
}
- public override CCodeExpression get_unref_expression (CCodeExpression cvar, DataType type, Expression expr) {
+ public override CCodeExpression get_unref_expression (CCodeExpression cvar, DataType type, Expression expr, bool is_macro_definition) {
if (type is ArrayType) {
var array_type = (ArrayType) type;
if (!array_type.fixed_length) {
- return base.get_unref_expression (cvar, type, expr);
+ return base.get_unref_expression (cvar, type, expr, is_macro_definition);
}
requires_array_free = true;
@@ -664,7 +664,7 @@ internal class Vala.CCodeArrayModule : CCodeMethodCallModule {
return ccall;
} else {
- return base.get_unref_expression (cvar, type, expr);
+ return base.get_unref_expression (cvar, type, expr, is_macro_definition);
}
}
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index a93440d..4a12029 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -2352,7 +2352,7 @@ internal class Vala.CCodeBaseModule : CCodeModule {
return null;
}
- public virtual CCodeExpression get_unref_expression (CCodeExpression cvar, DataType type, Expression expr) {
+ public virtual CCodeExpression get_unref_expression (CCodeExpression cvar, DataType type, Expression expr, bool is_macro_definition = false) {
var ccall = new CCodeFunctionCall (get_destroy_func_expression (type));
if (type is ValueType && !type.nullable) {
@@ -2374,6 +2374,22 @@ internal class Vala.CCodeBaseModule : CCodeModule {
}
}
+ if (ccall.call is CCodeIdentifier && !(type is ArrayType) && !is_macro_definition) {
+ // generate and use NULL-aware free macro to simplify code
+
+ var freeid = (CCodeIdentifier) ccall.call;
+ string free0_func = "_%s0".printf (freeid.name);
+
+ if (add_wrapper (free0_func)) {
+ var macro = get_unref_expression (new CCodeIdentifier ("var"), type, expr, true);
+ source_declarations.add_type_declaration (new CCodeMacroReplacement.with_expression ("%s(var)".printf (free0_func), macro));
+ }
+
+ ccall = new CCodeFunctionCall (new CCodeIdentifier (free0_func));
+ ccall.add_argument (cvar);
+ return ccall;
+ }
+
/* (foo == NULL ? NULL : foo = (unref (foo), NULL)) */
/* can be simplified to
@@ -3148,6 +3164,35 @@ internal class Vala.CCodeBaseModule : CCodeModule {
return null;
}
+ if (dupexpr is CCodeIdentifier && !(expression_type is ArrayType) && !is_ref_function_void (expression_type)) {
+ // generate and call NULL-aware ref function to reduce number
+ // of temporary variables and simplify code
+
+ var dupid = (CCodeIdentifier) dupexpr;
+ string dup0_func = "_%s0".printf (dupid.name);
+
+ // g_strdup is already NULL-safe
+ if (dupid.name == "g_strdup") {
+ dup0_func = dupid.name;
+ } else if (add_wrapper (dup0_func)) {
+ var dup0_fun = new CCodeFunction (dup0_func, "gpointer");
+ dup0_fun.add_parameter (new CCodeFormalParameter ("self", "gpointer"));
+ dup0_fun.modifiers = CCodeModifiers.STATIC;
+ dup0_fun.block = new CCodeBlock ();
+
+ var dup_call = new CCodeFunctionCall (dupexpr);
+ dup_call.add_argument (new CCodeIdentifier ("self"));
+
+ dup0_fun.block.add_statement (new CCodeReturnStatement (new CCodeConditionalExpression (new CCodeIdentifier ("self"), dup_call, new CCodeConstant ("NULL"))));
+
+ source_type_member_definition.append (dup0_fun);
+ }
+
+ var ccall = new CCodeFunctionCall (new CCodeIdentifier (dup0_func));
+ ccall.add_argument (cexpr);
+ return ccall;
+ }
+
var ccall = new CCodeFunctionCall (dupexpr);
if (!(expression_type is ArrayType) && expr != null && expr.is_non_null ()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]