+2007-07-26 Jürg Billeter <j@bitron.ch>
+
+ * vala/valastruct.vala, gobject/valacodegeneratormethod.vala,
+ gobject/valacodegeneratorstruct.vala: use GSlice and generate free
+ function for reference-type structs with a creation method
+
2007-07-25 Jürg Billeter <j@bitron.ch>
* vala/valaarray.vala: remove comments of overridden methods
} else {
var st = (Struct) m.parent_symbol;
var cdecl = new CCodeDeclaration (st.get_cname () + "*");
- var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
- ccall.add_argument (new CCodeConstant (st.get_cname ()));
- ccall.add_argument (new CCodeConstant ("1"));
+ var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_new0"));
+ ccall.add_argument (new CCodeIdentifier (st.get_cname ()));
cdecl.add_declarator (new CCodeVariableDeclarator.with_initializer ("self", ccall));
cinit.append (cdecl);
}
var old_instance_dispose_fragment = instance_dispose_fragment;
current_type_symbol = st;
instance_struct = new CCodeStruct ("_%s".printf (st.get_cname ()));
- instance_dispose_fragment = null;
+ instance_dispose_fragment = new CCodeFragment ();
CCodeFragment decl_frag;
CCodeFragment def_frag;
st.accept_children (this);
+ if (st.default_construction_method != null) {
+ var function = new CCodeFunction (st.get_lower_case_cprefix () + "free", "void");
+ if (st.access == MemberAccessibility.PRIVATE) {
+ function.modifiers = CCodeModifiers.STATIC;
+ }
+
+ function.add_parameter (new CCodeFormalParameter ("self", st.get_cname () + "*"));
+
+ decl_frag.append (function.copy ());
+
+ var cblock = new CCodeBlock ();
+
+ cblock.add_statement (instance_dispose_fragment);
+
+ var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_free"));
+ ccall.add_argument (new CCodeIdentifier (st.get_cname ()));
+ ccall.add_argument (new CCodeIdentifier ("self"));
+ cblock.add_statement (new CCodeExpressionStatement (ccall));
+
+ function.block = cblock;
+
+ def_frag.append (function);
+ }
+
current_type_symbol = old_type_symbol;
instance_struct = old_instance_struct;
instance_dispose_fragment = old_instance_dispose_fragment;
public override string get_free_function () {
if (free_function == null) {
- Report.error (source_reference, "The type `%s` doesn't contain a free function".printf (get_full_name ()));
+ if (default_construction_method != null) {
+ free_function = get_lower_case_cprefix () + "free";
+ } else {
+ Report.error (source_reference, "The type `%s` doesn't contain a free function".printf (get_full_name ()));
+ }
}
return free_function;
}