public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@infradead.org>
To: David Sterba <dsterba@suse.com>
Cc: linux-btrfs@vger.kernel.org
Subject: Re: [PATCH 1/2] btrfs: replace unsigned char with u8 for type casts
Date: Fri, 14 Jul 2023 07:44:37 -0700	[thread overview]
Message-ID: <ZLFfVSY3+Lwp8l4v@infradead.org> (raw)
In-Reply-To: <903ef08411ce5f8456f1c5b7a099c526d19dbf21.1689257327.git.dsterba@suse.com>

On Thu, Jul 13, 2023 at 04:10:24PM +0200, David Sterba wrote:
> There's no reason to use 'unsigned char' when we can simply use u8
> for byte buffers like for checksums or send buffers. The char could be
> signed or unsigned depending on the compiler settings, per [1] it's not
> simple.
> 
> [1] https://lore.kernel.org/lkml/CAHk-=wgz3Uba8w7kdXhsqR1qvfemYL+OFQdefJnkeqXG8qZ_pA@mail.gmail.com/

I don't understand how this link is related.  Plain char is indeed
a mess because it could be signed or not.  unsigned char vs u8 is
just plain preference as the latter is always an alias to the former.

u8 tends to look nice to my eyes, but this is a simple cleanup / style
change and not relatded to the above.

> 
> Checksum buffer item already uses u8 so this unifies the types and in
> btrfs_readdir_delayed_dir_index() the unsigned char has been probably
> inherited from fs_ftype_to_dtype() bit it's not strictly necessary

That one isn't used by a cast anyway..

>  	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
> +	item = (struct btrfs_csum_item *)((u8 *)item + csum_offset * csum_size);

This still looks a bit odd to me, because btrfs_csum_item returns the
pointer as the last argument, and the next thing we'd do here is to cast
that away.  If you don't mind using the gcc extention of void pointer
arithmetics that we use all over the kernel this could become:

	item = btrfs_item_ptr(leaf, path->slots[0], void) +
		csum_offset * csum_size;

which I find quite a bit readable.

>  	leaf = path->nodes[0];
>  csum:
>  	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
> -	item_end = (struct btrfs_csum_item *)((unsigned char *)item +
> +	item_end = (struct btrfs_csum_item *)((u8 *)item +
>  				      btrfs_item_size(leaf, path->slots[0]));
> -	item = (struct btrfs_csum_item *)((unsigned char *)item +
> -					  csum_offset * csum_size);
> +	item = (struct btrfs_csum_item *)((u8 *)item + csum_offset * csum_size);

And something similar here.  No point in asking btrfs_item_ptr
to cast to a a btrfs_csum_item, when you then instantly cast it to u8
(or void per the above) for pointer arithmetic.


  reply	other threads:[~2023-07-14 14:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-13 14:10 [PATCH 0/2] Minor macro and type cleanups David Sterba
2023-07-13 14:10 ` [PATCH 1/2] btrfs: replace unsigned char with u8 for type casts David Sterba
2023-07-14 14:44   ` Christoph Hellwig [this message]
2023-07-13 14:10 ` [PATCH 2/2] btrfs: use helper sizeof_field in struct accessors David Sterba
2023-07-14 14:45   ` Christoph Hellwig
2023-07-14 10:24 ` [PATCH 0/2] Minor macro and type cleanups Johannes Thumshirn

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=ZLFfVSY3+Lwp8l4v@infradead.org \
    --to=hch@infradead.org \
    --cc=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.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