public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Josef Bacik <josef@toxicpanda.com>
To: Mark Harmstone <maharmstone@fb.com>
Cc: linux-btrfs@vger.kernel.org
Subject: Re: [PATCH] btrfs-progs: simplify mkfs_main cleanup
Date: Fri, 19 Jul 2024 16:32:04 -0400	[thread overview]
Message-ID: <20240719203204.GC2312632@perftesting> (raw)
In-Reply-To: <20240719154649.4127040-1-maharmstone@fb.com>

On Fri, Jul 19, 2024 at 04:46:43PM +0100, Mark Harmstone wrote:
> mkfs_main is a main-like function, meaning that return and exit are
> equivalent. Deduplicate code by adding a new out2 label.
> 
> Signed-off-by: Mark Harmstone <maharmstone@fb.com>
> ---
>  mkfs/main.c | 14 +++++---------
>  1 file changed, 5 insertions(+), 9 deletions(-)
> 
> diff --git a/mkfs/main.c b/mkfs/main.c
> index a69aa24b..5705acb6 100644
> --- a/mkfs/main.c
> +++ b/mkfs/main.c
> @@ -1915,6 +1915,8 @@ out:
>  	}
>  
>  	btrfs_close_all_devices();
> +
> +out2:

Generally we don't like out2/out3 things, that makes it hard to figure out what
is going on, I'd rather it be like out_free.

However all error is doing here now is setting ret = 1 and going to out2.  At
this point you should just make sure ret is set in the places that call 'goto
error' and then move error to where out2 is.

If you're looking for even better cleanup semantics, I would do something like
the following

#define __free(func) __attribute__((cleanup(func)))

static inline void freep(void *ptr)
{
        void *real_ptr = *(void **)ptr;
        if (real_ptr == NULL)
                return;
        free(real_ptr);
}

to one of our common utils headers, and then do

pthread_t *t_prepare __free(freep) = NULL;
struct prepare_device_progress *prepare_ctx __free(freep) = NULL;
char *label __free(freep) = NULL;
char *source_dir __free(freep) = NULL;

and then you don't have to worry about the free stuff, and then you just have
the close part.

If you wanted to be even fancier you could also change how prepare_ctx is
allocated, so it has the number of devices in itself, and then the array of the
fd's, and you could add a custom cleanup function for that which would close all
the fd's and then free the memory.

But at the very least the out2 thing needs to be addressed.  The rest of these
thoughts are possible cleanup options that could be explored later.  Thanks,

Josef

      reply	other threads:[~2024-07-19 20:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-19 15:46 [PATCH] btrfs-progs: simplify mkfs_main cleanup Mark Harmstone
2024-07-19 20:32 ` Josef Bacik [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=20240719203204.GC2312632@perftesting \
    --to=josef@toxicpanda.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=maharmstone@fb.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