Blog Archives

Help wanted in ModemManager/NetworkManager development!

If you have free time, and experience with gtk-doc documentation or man pages, ModemManager developers will thank you [1] if you help writing the new libmm-common, libmm-glib and mmcli documentation:
[MM 0.6] Documentation

If you have free time, basic GLib/GIO development knowledge, and a broadband modem (either USB dongle or internal) to play with, ModemManager developers will thank you [2] if you port any of the missing plugins to the new codebase in the 06-api branch:
[MM 0.6] Plugins

If you have free time, NetworkManager installed [3] and basic GLib/GIO development knowledge, ModemManager developers will thank you [4] if you help providing support for the new ModemManager1 interface in NetworkManager:
[MM 0.6] NM integration

[1] You’ll get one beer for free if we ever meet.
[2] You’ll get two beers for free if we ever meet.
[3] This already leaves out Jose, Luca and Alfred.
[4] You’ll get three! beers for free if we ever meet.

Avoid G_TYPE_INSTANCE_GET_PRIVATE() in GObjects

When developing GObjects in Glib/GObject framework, it is usual to have a structure defined in the source file with all the private members of the object. Doing this, the internals of the object are not published in its API, so that no other module outside can modify them without using the defined GObject methods.

There are several ways to achieve this, and one of them is the recommended one in GObject’s tutorial [1], which uses the G_TYPE_INSTANCE_GET_PRIVATE [2] macro to get the glib-instantiated private structure. This private structure, allocated for each instance of the object, is specified in the Class initialization function with g_type_class_add_private(), and allocated every time a new GObject instance is created.

Usually, you will have a macro for your specific GObject which will call G_TYPE_INSTANCE_GET_PRIVATE:

#define MY_GOBJECT_GET_PRIVATE(o) \
    (G_TYPE_INSTANCE_GET_PRIVATE((o), MY_GOBJECT_TYPE, MyGObjectPrivate))


This seems a good way of doing it, and quite simple to use. Glib will take care of allocating and deallocating that private structure for us… so why avoid it?

Well… if you ever tried to run Valgrind’s callgrind tool to measure how much time your program spends in a given function, you will see the reason. Other people already did that and published results on the web, so go and check them [3] or just try it with your application:

$> valgrind --tool=callgrind /usr/local/bin/your_program</span


Now, an easy way of getting the same result, with a little bit more of work but achieving the best performance, is just using an opaque pointer in the GObject’s public structure and define, allocate and deallocate it yourself in the source file (so also can be treated as private, as the users of the API don’t know the internals of the structure).

These are the basic changes you need to do to avoid calling G_TYPE_INSTANCE_GET_PRIVATE():

**** In the HEADER of the GObject, when the struct defining the Object is specified, add a “gpointer priv” variable.

struct _MyGObject
{
    /** Parent object */
    GObject parent;
    /** Private data pointer */
    gpointer priv;
}


**** Then, in the SOURCE of the GObject, modify the GET_PRIVATE macro so that instead of doing the standard GObject lookup for the correct type, we just get the opaque “priv” pointer defined in the header, and we cast it to the correct type.

#define MY_GOBJECT_GET_PRIVATE(o) \
    ((MyGObjectPrivate *)((MY_GOBJECT(o))->priv))


**** As we won’t use the automatic allocation of the Private data structure, we need to allocate it ourselves in the _init() function.

static void
my_gobject_init(MyGObject *self)
{
    /* Allocate Private data structure */
    (MY_GOBJECT(self))->priv = \
        (MyGObjectPrivate *) g_malloc0(sizeof(MyGObjectPrivate));
    /* If correctly allocated, initialize parameters */
    if((MY_GOBJECT(self))->priv != NULL)
    {
        MyGObjectPrivate *priv = MY_GOBJECT_GET_PRIVATE(self);
        /* Initialize private data, if any */
    }
}


**** And finally, the last change is just de-allocating the structure when no more needed in the _dispose() function.

static void
my_gobject_dispose(GObject *object)
{
    MyGObject *self = (MyGObject *)object;
    MyGObjectPrivate *priv = MY_GOBJECT_GET_PRIVATE(self);
    /* Check if not NULL! To avoid calling dispose multiple times */
    if(priv != NULL)
    {
        /* Deallocate contents of the private data, if any */
        /* Deallocate private data structure */
        g_free(priv);
        /* And finally set the opaque pointer back to NULL, so that
         *  we don't deallocate it twice. */
        (MY_GOBJECT(self))->priv = NULL;
    }
}


References:

Please, forget it as soon as possible

New development version of Glib just released.

GLib 2.19.1 is now available for download at:

ftp://ftp.gtk.org/pub/glib/2.19/

http://download.gnome.org/sources/glib/2.19/

glib-2.19.1.tar.bz2 md5sum: 9309139a515408d9c99558d051c18302
glib-2.19.1.tar.gz md5sum: eab90f5965f3d004b8787989be5cca85

This is the second development release leading up to GLib 2.20.

It’s great they include the changelog with fixed bugs:

* Bugs fixed:
562538 GObject interface tutorial shouldn’t finalise with
“Please forget everything”

This one is great (among others, including multiple mem-leaks fixed).

Until this last release, the GObject tutorial had the following comment at the end of the “Non-instantiable classed types: interfaces” section:

“Now that you have read this section, you can forget about it. Please, forget it as soon as possible. “

What I am not sure about is: did they remove it because now interfaces are great? or just to skip publishing the idea that even GObject developers think that interfaces suck? Who knows…

Previous documentation:
http://library.gnome.org/devel/gobject/2.18/gtype-non-instantiable-classed.html
Latest documentation:
http://library.gnome.org/devel/gobject/unstable/gtype-non-instantiable-classed.html

Follow

Get every new post delivered to your Inbox.

Join 36 other followers