[PATCH] make private play nice with type system

"K. Haley" <[email protected]> Wed, 11 Aug 2004 16:54:14 -0600
Newsgroups gmane.comp.gnome.devtools.gob.general
Message-ID <[email protected]>
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)
gob.diff (text/plain, 1.2 KB)
--- 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,
signature.asc (application/pgp-signature, 254 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBGqOcDMrq4AbU59ERAoSyAJ4xAnRTw7RQbzkbC5t1Msnl9l4h5wCfZ72I
YKXQt2mX4vyw4ip3YWRbfIg=
=XC2U
-----END PGP SIGNATURE-----