After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 780083 - Support autocleanups for all exported types
Support autocleanups for all exported types
Status: RESOLVED FIXED
Product: GEGL
Classification: Other
Component: general
git master
Other All
: Normal enhancement
: ---
Assigned To: Default Gegl Component Owner
Default Gegl Component Owner
Depends on:
Blocks: 763712
 
 
Reported: 2017-03-15 09:54 UTC by Debarshi Ray
Modified: 2017-05-01 20:12 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
operation: Add autocleanups for GeglOperation and its sub-types (8.20 KB, patch)
2017-03-16 06:48 UTC, Debarshi Ray
none Details | Review
types: Make it self-contained (791 bytes, patch)
2017-03-20 10:18 UTC, Debarshi Ray
none Details | Review
types: Add autocleanups (2.71 KB, patch)
2017-03-20 10:18 UTC, Debarshi Ray
none Details | Review
operation: Add autocleanups for GeglOperation sub-classes (7.44 KB, patch)
2017-03-20 10:19 UTC, Debarshi Ray
none Details | Review
random: Add autocleanups (746 bytes, patch)
2017-03-20 10:19 UTC, Debarshi Ray
none Details | Review

Description Debarshi Ray 2017-03-15 09:54:37 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
Comment 1 Debarshi Ray 2017-03-16 06:46:30 UTC
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.
Comment 2 Debarshi Ray 2017-03-16 06:48:30 UTC
Created attachment 348063 [details] [review]
operation: Add autocleanups for GeglOperation and its sub-types
Comment 3 Debarshi Ray 2017-03-16 06:56:41 UTC
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&section=all&arch=any&searchon=names&keywords=libglib2
Comment 4 Øyvind Kolås (pippin) 2017-03-16 11:19:28 UTC
Since GeglOperations subclasses are reference managed by GeglNodes, does adding these autoptr cleanups to these classes make sense?
Comment 5 Debarshi Ray 2017-03-16 20:43:15 UTC
(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*.
Comment 6 Debarshi Ray 2017-03-20 10:18:43 UTC
Created attachment 348300 [details] [review]
types: Make it self-contained
Comment 7 Debarshi Ray 2017-03-20 10:18:57 UTC
Created attachment 348301 [details] [review]
types: Add autocleanups
Comment 8 Debarshi Ray 2017-03-20 10:19:17 UTC
Created attachment 348302 [details] [review]
operation: Add autocleanups for GeglOperation sub-classes
Comment 9 Debarshi Ray 2017-03-20 10:19:33 UTC
Created attachment 348303 [details] [review]
random: Add autocleanups
Comment 10 Øyvind Kolås (pippin) 2017-04-29 15:34:17 UTC
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.
Comment 11 Debarshi Ray 2017-05-01 20:12:21 UTC
(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.