From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Daniel P. Berrangé" <berrange@redhat.com>
Subject: [Qemu-devel] [PATCH v2 0/3] require newer glib2 to enable autofree'ing of stack variables exiting scope
Date: Thu, 25 Jul 2019 09:43:38 +0100 [thread overview]
Message-ID: <20190725084341.8287-1-berrange@redhat.com> (raw)
Both GCC and CLang support a C extension attribute((cleanup)) which
allows you to define a function that is invoked when a stack variable
exits scope. This typically used to free the memory allocated to it,
though you're not restricted to this. For example it could be used to
unlock a mutex.
We could use that functionality now, but the syntax is a bit ugly in
plain C. Since version 2.44 of GLib, there have been a few macros to
make it more friendly to use - g_autofree, g_autoptr and
G_DEFINE_AUTOPTR_CLEANUP_FUNC.
https://developer.gnome.org/glib/stable/glib-Miscellaneous-Macros.html
https://blogs.gnome.org/desrt/2015/01/30/g_autoptr/
The key selling point is that it simplifies the cleanup code paths,
often eliminating the need to goto cleanup labels. This improves
the readability of the code and makes it less likely that you'll
leak memory accidentally.
Inspired by seeing it added to glib, and used in systemd, Libvirt
finally got around to adopting this in Feb 2019. Overall our
experience with it has been favourable/positive, with the code
simplification being very nice.
The main caveats with it are
- Only works with GCC or CLang. We don't care as those are
the only two compilers we declare support for.
- You must always initialize the variables when declared
to ensure predictable behaviour when they leave scope.
Chances are most methods with goto jumps for cleanup
are doing this already
- You must not directly return the value that's assigned
to a auto-cleaned variable. You must steal the pointer
in some way. ie
BAD:
g_autofree char *wibble = g_strdup("wibble")
....
return wibble;
GOOD:
g_autofree char *wibble = g_strdup("wibble")
...
return g_steal_pointer(wibble);
g_steal_pointer is an inline function which simply copies
the pointer to a new variable, and sets the original variable
to NULL, thus avoiding cleanup.
I've illustrated the usage by converting a bunch of the crypto code in
QEMU to use auto cleanup.
Changed on v2:
- Actually commit the rest of the changes to patch 3 so that what's
posted works :-) Sigh.
Daniel P. Berrangé (3):
glib: bump min required glib library version to 2.48
crypto: define cleanup functions for use with g_autoptr
crypto: use auto cleanup for many stack variables
configure | 2 +-
crypto/afsplit.c | 28 ++++----------
crypto/block-luks.c | 74 +++++++++++--------------------------
crypto/block.c | 15 +++-----
crypto/hmac-glib.c | 5 ---
crypto/pbkdf.c | 5 +--
crypto/secret.c | 38 ++++++++-----------
crypto/tlscredsanon.c | 16 +++-----
crypto/tlscredspsk.c | 5 +--
crypto/tlscredsx509.c | 16 +++-----
include/crypto/block.h | 2 +
include/crypto/cipher.h | 2 +
include/crypto/hmac.h | 2 +
include/crypto/ivgen.h | 2 +
include/crypto/tlssession.h | 2 +
include/glib-compat.h | 42 +--------------------
16 files changed, 78 insertions(+), 178 deletions(-)
--
2.21.0
next reply other threads:[~2019-07-25 8:44 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-07-25 8:43 Daniel P. Berrangé [this message]
2019-07-25 8:43 ` [Qemu-devel] [PATCH v2 1/3] glib: bump min required glib library version to 2.48 Daniel P. Berrangé
2019-07-25 8:43 ` [Qemu-devel] [PATCH v2 2/3] crypto: define cleanup functions for use with g_autoptr Daniel P. Berrangé
2019-07-25 8:43 ` [Qemu-devel] [PATCH v2 3/3] crypto: use auto cleanup for many stack variables Daniel P. Berrangé
2019-07-29 14:58 ` Stefan Hajnoczi
2019-07-31 12:59 ` [Qemu-devel] [PATCH v2 0/3] require newer glib2 to enable autofree'ing of stack variables exiting scope Marc-André Lureau
2019-07-31 14:04 ` Alex Bennée
2019-07-31 14:08 ` Eric Blake
2019-07-31 14:10 ` Daniel P. Berrangé
2019-07-31 14:33 ` Alex Bennée
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190725084341.8287-1-berrange@redhat.com \
--to=berrange@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).