qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: "Daniel P. Berrangé" <berrange@redhat.com>
Cc: qemu-devel@nongnu.org, Michael Roth <mdroth@linux.vnet.ibm.com>
Subject: Re: [PATCH v2] qapi: enable use of g_autoptr with QAPI types
Date: Fri, 24 Jul 2020 09:58:12 +0200	[thread overview]
Message-ID: <87wo2tcpkb.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <20200723153845.2934357-1-berrange@redhat.com> ("Daniel P. Berrangé"'s message of "Thu, 23 Jul 2020 16:38:45 +0100")

Daniel P. Berrangé <berrange@redhat.com> writes:

> Currently QAPI generates a type and function for free'ing it:
>
>   typedef struct QCryptoBlockCreateOptions QCryptoBlockCreateOptions;
>   void qapi_free_QCryptoBlockCreateOptions(QCryptoBlockCreateOptions *obj);
>
> This is used in the traditional manner:
>
>   QCryptoBlockCreateOptions *opts = NULL;
>
>   opts = g_new0(QCryptoBlockCreateOptions, 1);
>
>   ....do stuff with opts...
>
>   qapi_free_QCryptoBlockCreateOptions(opts);
>
> Since bumping the min glib to 2.48, QEMU has incrementally adopted the
> use of g_auto/g_autoptr. This allows the compiler to run a function to
> free a variable when it goes out of scope, the benefit being the
> compiler can guarantee it is freed in all possible code ptahs.
>
> This benefit is applicable to QAPI types too, and given the seriously
> long method names for some qapi_free_XXXX() functions, is much less
> typing. This change thus makes the code generator emit:
>
>  G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoBlockCreateOptions,
>                               qapi_free_QCryptoBlockCreateOptions)
>
> The above code example now becomes
>
>   g_autoptr(QCryptoBlockCreateOptions) opts = NULL;
>
>   opts = g_new0(QCryptoBlockCreateOptions, 1);
>
>   ....do stuff with opts...
>
> Note, if the local pointer needs to live beyond the scope holding the
> variable, then g_steal_pointer can be used. This is useful to return the
> pointer to the caller in the success codepath, while letting it be freed
> in all error codepaths.
>
>   return g_steal_pointer(&opts);
>
> The crypto/block.h header needs updating to avoid symbol clash now that
> the g_autoptr support is a standard QAPI feature.
>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>

Reviewed-by: Markus Armbruster <armbru@redhat.com>

Queued for 5.2, thanks!



      parent reply	other threads:[~2020-07-24  7:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-23 15:38 [PATCH v2] qapi: enable use of g_autoptr with QAPI types Daniel P. Berrangé
2020-07-23 15:56 ` Eric Blake
2020-07-24  7:58 ` Markus Armbruster [this message]

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=87wo2tcpkb.fsf@dusky.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=mdroth@linux.vnet.ibm.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).