[PATCH] make private play nice with type system

From: K. Haley <haleykd_at_users.sourceforge.net>
Date: Wed, 11 Aug 2004 16:54:14 -0600

The GType system has a means of adding a private structure to a type so
that both are allocated and freed by the type system. This allows both
public and private instance data to be allocated at the same time.
While this is mostly useful for types using n_preallocs (allocated with
g_mem_chunk), using a single memory allocation for both should benefit
all users.

On the subject of n_preallocs, would it be possible to specify its
value in the gob file without creating a custom get_type function? I
believe it is the only value that most gob users would want to change.
I know I have two classes that will be using it.

As for the patch:

class_init : specify size of private struct with g_type_class_add_private
init : set _priv with G_TYPE_INSTANCE_GET_PRIVATE
finalize : remove g_free(_priv)

--- gob2-2.0.9/src/main.c Mon Jul 19 10:28:07 2004
+++ gob2-2.0.9-m/src/main.c Wed Aug 11 16:19:23 2004
@@ -2137,10 +2137,10 @@
                 }
         }
 
- if (privates > 0) {
+/* if (privates > 0) {
                 out_printf(out, "\tg_free (priv);\n");
         }
-
+*/
         out_printf(out, "}\n"
                    "#undef __GOB_FUNCTION__\n\n");
 }
@@ -2202,10 +2202,15 @@
                                    "#define __GOB_FUNCTION__ \"%s::init\"\n",
                                    c->otype);
                         if(privates > 0) {
- out_printf(out, "\t%s->_priv = "
+ /*out_printf(out, "\t%s->_priv = "
                                            "g_new0 (%sPrivate, 1);\n",
                                            ((FuncArg *)m->args->data)->name,
- typebase);
+ typebase);*/
+ out_printf(out, "\t%s->_priv = "
+ "G_TYPE_INSTANCE_GET_PRIVATE(%s,TYPE_SELF,%sPrivate);\n",
+ ((FuncArg *)m->args->data)->name,
+ ((FuncArg *)m->args->data)->name,
+ typebase);
                         } else if(always_private_struct) {
                                 out_printf(out, "\t%s->_priv = NULL;\n",
                                            ((FuncArg *)m->args->data)->name);
@@ -2249,6 +2254,10 @@
                                 did_base_obj = TRUE;
                         }
 
+ if(privates>0)
+ out_printf(out,"\n\tg_type_class_add_private(%s,sizeof(%sPrivate);\n",
+ ((FuncArg *)m->args->data)->name,typebase);
+
                         if (overrides > 0)
                                 add_overrides (c,
                                                ((FuncArg *)m->args->data)->name,

Received on Wed Aug 11 2004 - 22:54:18 CDT

This archive was generated by hypermail 2.2.0 : Sun Apr 17 2011 - 21:05:02 CDT