qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: "Daniel P. Berrangé" <berrange@redhat.com>, qemu-devel@nongnu.org
Cc: "Marc-André Lureau" <marcandre.lureau@gmail.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Stefan Hajnoczi" <stefanha@gmail.com>
Subject: Re: [Qemu-devel] [PATCH v3 3/4] docs: document use of automatic cleanup functions in glib
Date: Thu, 5 Sep 2019 09:46:51 -0500	[thread overview]
Message-ID: <066b199b-3f1d-11a3-3e51-92c5b80f5475@redhat.com> (raw)
In-Reply-To: <20190829165036.9773-4-berrange@redhat.com>


[-- Attachment #1.1: Type: text/plain, Size: 3160 bytes --]

On 8/29/19 11:50 AM, Daniel P. Berrangé wrote:
> Document the use of g_autofree and g_autoptr in glib for automatic
> freeing of memory.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  CODING_STYLE.rst | 85 ++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 85 insertions(+)
> 
> diff --git a/CODING_STYLE.rst b/CODING_STYLE.rst
> index 4501d87352..39397f0f6f 100644
> --- a/CODING_STYLE.rst
> +++ b/CODING_STYLE.rst
> @@ -441,6 +441,91 @@ In addition, QEMU assumes that the compiler does not use the latitude
>  given in C99 and C11 to treat aspects of signed '<<' as undefined, as
>  documented in the GNU Compiler Collection manual starting at version 4.0.
>  
> +Automatic memory deallocation
> +=============================
> +
> +QEMU has a mandatory dependency either the GCC or CLang compiler. As

s/either/on either/

> +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.
> +

> +
> +For example, instead of
> +
> +.. code-block:: c
> +
> +    int somefunc(void) {

Should that { be on its own line to match our prevailing style?

> +        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:
> +
> +.. code-block:: c
> +
> +    int somefunc(void) {

here too

> +        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
> +
> +
> +.. code-block:: c
> +
> +    char *somefunc(void) {

and again

> +        g_autofree char *foo = g_strdup_printf("foo%", "wibble");
> +        g_autoptr (GList) bar = .....
> +
> +        if (eek) {
> +           return NULL;
> +        }
> +
> +        return g_steal_pointer(&foo);
> +    }
> +
> +
>  Error handling and reporting
>  ============================
>  
> 

With those fixes,
Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

  reply	other threads:[~2019-09-05 14:47 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-29 16:50 [Qemu-devel] [PATCH v3 0/4] docs: add docs about use of automatic cleanup functions Daniel P. Berrangé
2019-08-29 16:50 ` [Qemu-devel] [PATCH v3 1/4] docs: convert README, CODING_STYLE and HACKING to RST syntax Daniel P. Berrangé
2019-09-09 12:57   ` Philippe Mathieu-Daudé
2019-09-09 13:15     ` Daniel P. Berrangé
2019-09-09 13:20       ` Philippe Mathieu-Daudé
2019-09-09 14:25         ` Paolo Bonzini
2019-09-09 15:14           ` Alex Bennée
2019-08-29 16:50 ` [Qemu-devel] [PATCH v3 2/4] docs: merge HACKING.rst contents into CODING_STYLE.rst Daniel P. Berrangé
2019-09-05 14:43   ` Eric Blake
2019-08-29 16:50 ` [Qemu-devel] [PATCH v3 3/4] docs: document use of automatic cleanup functions in glib Daniel P. Berrangé
2019-09-05 14:46   ` Eric Blake [this message]
2019-08-29 16:50 ` [Qemu-devel] [PATCH v3 4/4] docs: split the CODING_STYLE doc into distinct groups Daniel P. Berrangé
2019-09-05 13:31   ` Peter Maydell

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=066b199b-3f1d-11a3-3e51-92c5b80f5475@redhat.com \
    --to=eblake@redhat.com \
    --cc=alex.bennee@linaro.org \
    --cc=berrange@redhat.com \
    --cc=marcandre.lureau@gmail.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@gmail.com \
    /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).