GNOME Bugzilla – Bug 780083
Support autocleanups for all exported types
Last modified: 2017-05-01 20:12:21 UTC
GLib 2.44.0 added some features to make it easier to use __attribute__((cleanup)) [1] on compilers that support it (ie. gcc and clang). A type has to define its cleanup function using G_DEFINE_AUTOPTR_CLEANUP_FUNC(), G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC() or G_DEFINE_AUTO_CLEANUP_FREE_FUNC(). See: https://developer.gnome.org/glib/stable/glib-Miscellaneous-Macros.html#G-DEFINE-AUTOPTR-CLEANUP-FUNC:CAPS Note that defining the cleanup function doesn't add any dependency on __attribute__((cleanup)). Then client code relying on a compiler that supporting cleanup will be able to use g_auto, g_autofree and g_autoptr with such types. See: https://developer.gnome.org/glib/stable/glib-Miscellaneous-Macros.html#g-auto GEGL itself won't be able to use g_auto* because of its stricter portability requirements. However, gnome-photos doesn't care about anything other than gcc and clang, so it can. [1] https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
GLib 2.44.0 enables you to reduce the amount of GObject boilerplate using the G_DECLARE_FINAL_TYPE(), G_DECLARE_DERIVABLE_TYPE() or G_DECLARE_INTERFACE() macros. See: https://developer.gnome.org/gobject/stable/gobject-Type-Information.html#G-DECLARE-FINAL-TYPE:CAPS Here is how a GObject that's defined using these macros will look like: https://wiki.gnome.org/HowDoI/SubclassGObject Here are some sample diffs against pre-existing GObject type definitions: G_DECLARE_FINAL_TYPE: https://git.gnome.org/browse/gnome-photos/commit/?id=c0bac819d26e514a226fa6b1d3f56c5292c3fb5f G_DECLARE_DERIVABLE: https://git.gnome.org/browse/gnome-photos/commit/?id=d01d6201b3dd0e7008031faf7b355331b3057a72 G_DECLARE_INTERFACE: https://git.gnome.org/browse/gnome-photos/commit/?id=d629308e815cbe1bb67bf60190cdbd80dd7bd3d9 Apart from the usual things, these declaration macros also define the cleanup function for the GObject (ie. g_object_unref) using G_DEFINE_AUTOPTR_CLEANUP_FUNC(). However, that only works if the parent type also defined a cleanup function. This means that one can only use G_DECLARE_* to define gnome-photos' builtin GEGL operations, if the GeglOperation base classes define cleanup functions.
Created attachment 348063 [details] [review] operation: Add autocleanups for GeglOperation and its sub-types
As for the GLib version, 2.44.0 was released 2 years ago in March, 2015. It's available in RHEL 7: https://git.centos.org/summary/?r=rpms/glib2.git Looks like it is in Debian testing: https://packages.debian.org/search?suite=default§ion=all&arch=any&searchon=names&keywords=libglib2
Since GeglOperations subclasses are reference managed by GeglNodes, does adding these autoptr cleanups to these classes make sense?
(In reply to Øyvind Kolås from comment #4) > Since GeglOperations subclasses are reference managed by GeglNodes, does > adding these autoptr cleanups to these classes make sense? Yes, because of the last paragraph of comment 1: (In reply to Debarshi Ray from comment #1) > Apart from the usual things, these declaration macros also define the > cleanup function for the GObject (ie. g_object_unref) using > G_DEFINE_AUTOPTR_CLEANUP_FUNC(). However, that only works if the parent type > also defined a cleanup function. This means that one can only use > G_DECLARE_* to define gnome-photos' builtin GEGL operations, if the > GeglOperation base classes define cleanup functions. Otherwise, at the very least, they lead to a bunch of compiler warnings. For things like GeglNode and GeglBuffer, the cleanup functions would actually be used with g_auto*.
Created attachment 348300 [details] [review] types: Make it self-contained
Created attachment 348301 [details] [review] types: Add autocleanups
Created attachment 348302 [details] [review] operation: Add autocleanups for GeglOperation sub-classes
Created attachment 348303 [details] [review] random: Add autocleanups
I do not fully understand the implications, but if glib/gobject upstream encourages it - it probably will continue to work also with non gcc-compilers, best way to discover possible trouble is pushing it to master.. commit d52b0385eb0147a1b8b0b75519836f45e7da8e9e Author: Debarshi Ray <debarshir@gnome.org> Date: Wed Mar 15 10:41:04 2017 +0100 operation: Add autocleanups for GeglOperation sub-classes Since GeglOperation sub-classes are reference-managed by GeglNode, it is unlikely that applications will need to use g_autoptr with them. This is primarily meant to let applications use G_DECLARE_* to reduce the GObject boilerplate when defining their own operations. Since G_DECLARE_* also defines the cleanup function for the new type, it is necessary for the parent to also define it, even if it won't be used. https://bugzilla.gnome.org/show_bug.cgi?id=780083 commit 7244e41fefc8cdf73ac032fff967ff359e344780 Author: Debarshi Ray <debarshir@gnome.org> Date: Mon Mar 20 10:39:20 2017 +0100 types: Add autocleanups Bump required GLib version to 2.44.0. https://bugzilla.gnome.org/show_bug.cgi?id=780083 commit 4947088401edf42cd5dea8645cc37959f5ecd0c5 Author: Debarshi Ray <debarshir@gnome.org> Date: Mon Mar 20 10:58:38 2017 +0100 types: Make it self-contained The gegl/gegl-types.h header uses GType, which is defined by glib-object.h. Instead of relying on some other header to pull in the core GObject headers, let's explicitly include it.
(In reply to Øyvind Kolås from comment #10) > I do not fully understand the implications, but if glib/gobject upstream > encourages it - it probably will continue to work also with non > gcc-compilers, best way to discover possible trouble is pushing it to > master.. As far as I am aware, __attribute__((cleanup)) works on GCC and Clang/LLVM. It doesn't work with Microsoft's C compiler. GLib's G_DEFINE_AUTO* macros will expand as no-ops on compilers that don't support 'cleanup', and that's why they are used unconditionally: https://developer.gnome.org/glib/stable/glib-Miscellaneous-Macros.html#G-DEFINE-AUTOPTR-CLEANUP-FUNC:CAPS Also note that declaring the cleanup function with the G_DEFINE_AUTO* macros doesn't add any dependency on __attribute__((cleanup)) to GEGL. It allows client-code that uses GEGL to (a) leverage the 'cleanup' feature through g_auto*, and (b) reduce the GObject boilerplate through some newer macros.