All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Alex Bennée" <alex.bennee@linaro.org>,
	"Daniel P. Berrangé" <berrange@redhat.com>
Subject: [Qemu-devel] [PATCH 3/4] docs: document use of automatic cleanup functions in glib
Date: Fri, 23 Aug 2019 17:39:30 +0100	[thread overview]
Message-ID: <20190823163931.7442-4-berrange@redhat.com> (raw)
In-Reply-To: <20190823163931.7442-1-berrange@redhat.com>

Document the use of g_autofree and g_autoptr in glib for automatic
freeing of memory, or other resource cleanup (eg mutex unlocking).

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 CODING_STYLE.md | 101 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)

diff --git a/CODING_STYLE.md b/CODING_STYLE.md
index 9f4fc9dc77..f37b6c2d01 100644
--- a/CODING_STYLE.md
+++ b/CODING_STYLE.md
@@ -479,3 +479,104 @@ terminate QEMU.
 
 Note that &error_fatal is just another way to exit(1), and &error_abort
 is just another way to abort().
+
+
+## Automatic memory deallocation
+
+QEMU has a mandatory dependency either the GCC or CLang compiler. As
+such it has the freedom to make use of a C language extension for
+automatically running a cleanup function when a stack variable goes
+out of scope. This can be used to simplify function cleanup paths,
+often allowing many goto jumps to be eliminated, through automatic
+free'ing of memory.
+
+The GLib2 library provides a number of functions/macros for enabling
+automatic cleanup:
+
+ https://developer.gnome.org/glib/stable/glib-Miscellaneous-Macros.html
+
+Most notably:
+
+ - g_autofree - will invoke g_free() on the variable going out of scope
+
+ - g_autoptr - for structs / objects, will invoke the cleanup func created
+               by a previous use of G_DEFINE_AUTOPTR_CLEANUP_FUNC. This is
+               supported for most GLib data types and GObjects
+
+For example, instead of
+
+    int somefunc(void) {
+        int ret = -1;
+        char *foo = g_strdup_printf("foo%", "wibble");
+        GList *bar = .....
+
+        if (eek) {
+           goto cleanup;
+        }
+
+        ret = 0;
+
+      cleanup:
+        g_free(foo);
+        g_list_free(bar);
+        return ret;
+    }
+
+Using g_autofree/g_autoptr enables the code to be written as:
+
+    int somefunc(void) {
+        g_autofree char *foo = g_strdup_printf("foo%", "wibble");
+        g_autoptr (GList) bar = .....
+
+        if (eek) {
+           return -1;
+        }
+
+        return 0;
+    }
+
+While this generally results in simpler, less leak-prone code, there
+are still some caveats to beware of
+
+ * Variables declared with g_auto* MUST always be initialized,
+   otherwise the cleanup function will use uninitialized stack memory
+
+ * If a variable declared with g_auto* holds a value which must
+   live beyond the life of the function, that value must be saved
+   and the original variable NULL'd out. This can be simpler using
+   g_steal_pointer
+
+
+    char *somefunc(void) {
+        g_autofree char *foo = g_strdup_printf("foo%", "wibble");
+        g_autoptr (GList) bar = .....
+
+        if (eek) {
+           return NULL;
+        }
+
+        return g_steal_pointer(&foo);
+    }
+
+The cleanup functions are not restricted to simply free'ing memory. The
+GMutexLocker class is a variant of GMutex that has automatic locking and
+unlocking at start and end of the enclosing scope
+
+In the following example, the `lock` in `MyObj` will be held for the
+precise duration of the `somefunc` function
+
+    typedef struct {
+        GMutex lock;
+    } MyObj;
+
+    char *somefunc(MyObj *obj) {
+        g_autofree GMutexLocker *locker = g_mutex_locker_new(&obj->lock)
+        g_autofree char *foo = g_strdup_printf("foo%", "wibble");
+        g_autoptr (GList) bar = .....
+
+        if (eek) {
+           return NULL;
+        }
+
+        return g_steal_pointer(&foo);
+    }
-- 
2.21.0



  parent reply	other threads:[~2019-08-23 16:43 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-23 16:39 [Qemu-devel] [PATCH 0/4] docs: add docs about use of automatic cleanup functions Daniel P. Berrangé
2019-08-23 16:39 ` [Qemu-devel] [PATCH 1/4] docs: convert CODING_STYLE and HACKING to markdown syntax Daniel P. Berrangé
2019-08-28 12:25   ` Alex Bennée
2019-08-28 13:08     ` Daniel P. Berrangé
2019-08-23 16:39 ` [Qemu-devel] [PATCH 2/4] docs: merge HACKING.md contents into CODING_STYLE.md Daniel P. Berrangé
2019-08-23 19:35   ` Eric Blake
2019-08-28 15:06     ` Alex Bennée
2019-08-28 15:10       ` Daniel P. Berrangé
2019-08-23 16:39 ` Daniel P. Berrangé [this message]
2019-08-23 19:53   ` [Qemu-devel] [PATCH 3/4] docs: document use of automatic cleanup functions in glib Eric Blake
2019-08-28  9:00   ` Stefan Hajnoczi
2019-08-28 15:14   ` Alex Bennée
2019-08-28 15:20     ` Daniel P. Berrangé
2019-08-28 16:04       ` Alex Bennée
2019-08-23 16:39 ` [Qemu-devel] [PATCH 4/4] docs: add table of contents to CODING_STYLE.md Daniel P. Berrangé
2019-08-23 21:48 ` [Qemu-devel] [PATCH 0/4] docs: add docs about use of automatic cleanup functions Marc-André Lureau
2019-08-28 12:30   ` Alex Bennée
2019-08-28 13:07     ` Daniel P. Berrangé

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=20190823163931.7442-4-berrange@redhat.com \
    --to=berrange@redhat.com \
    --cc=alex.bennee@linaro.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.