public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Ilias Apalodimas <ilias.apalodimas@linaro.org>
To: Simon Glass <sjg@chromium.org>
Cc: U-Boot Mailing List <u-boot@lists.denx.de>,
	AKASHI Takahiro <takahiro.akashi@linaro.org>,
	Heinrich Schuchardt <xypron.glpk@gmx.de>
Subject: Re: [PATCH v2] disk: Use a helper function to reduce duplication
Date: Mon, 20 Mar 2023 09:31:58 +0200	[thread overview]
Message-ID: <ZBgL7sydZIt/ztwD@hera> (raw)
In-Reply-To: <20230319192957.1084530-1-sjg@chromium.org>

Hi Simon,

Patch looks good, but isn't the new function name a bit misleading?
Something like blk_part_find_start() sounds a bit more descriptive, or am I
missing something?

Cheers
/Ilias

On Mon, Mar 20, 2023 at 08:29:57AM +1300, Simon Glass wrote:
> Reduce the duplicated code slightly by using a helper function to handle
> the common code.
>
> This reduces the code size very slightly.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> Changes in v2:
> - Rebase to -next
>
>  disk/disk-uclass.c | 46 +++++++++++++++++++++++++---------------------
>  1 file changed, 25 insertions(+), 21 deletions(-)
>
> diff --git a/disk/disk-uclass.c b/disk/disk-uclass.c
> index d32747e2242d..7f1fd80b2248 100644
> --- a/disk/disk-uclass.c
> +++ b/disk/disk-uclass.c
> @@ -65,26 +65,38 @@ int part_create_block_devices(struct udevice *blk_dev)
>  	return 0;
>  }
>
> +static int blk_part_setup(struct udevice *dev, lbaint_t *startp,
> +			  lbaint_t blkcnt)
> +{
> +	struct disk_part *part;
> +
> +	part = dev_get_uclass_plat(dev);
> +	if (*startp >= part->gpt_part_info.size)
> +		return -E2BIG;
> +
> +	if (*startp + blkcnt > part->gpt_part_info.size)
> +		blkcnt = part->gpt_part_info.size - *startp;
> +	*startp += part->gpt_part_info.start;
> +
> +	return 0;
> +}
> +
>  static ulong part_blk_read(struct udevice *dev, lbaint_t start,
>  			   lbaint_t blkcnt, void *buffer)
>  {
>  	struct udevice *parent;
> -	struct disk_part *part;
>  	const struct blk_ops *ops;
> +	int ret;
>
>  	parent = dev_get_parent(dev);
>  	ops = blk_get_ops(parent);
>  	if (!ops->read)
>  		return -ENOSYS;
>
> -	part = dev_get_uclass_plat(dev);
> -	if (start >= part->gpt_part_info.size)
> +	ret = blk_part_setup(dev, &start, blkcnt);
> +	if (ret)
>  		return 0;
>
> -	if ((start + blkcnt) > part->gpt_part_info.size)
> -		blkcnt = part->gpt_part_info.size - start;
> -	start += part->gpt_part_info.start;
> -
>  	return ops->read(parent, start, blkcnt, buffer);
>  }
>
> @@ -92,22 +104,18 @@ static ulong part_blk_write(struct udevice *dev, lbaint_t start,
>  			    lbaint_t blkcnt, const void *buffer)
>  {
>  	struct udevice *parent;
> -	struct disk_part *part;
>  	const struct blk_ops *ops;
> +	int ret;
>
>  	parent = dev_get_parent(dev);
>  	ops = blk_get_ops(parent);
>  	if (!ops->write)
>  		return -ENOSYS;
>
> -	part = dev_get_uclass_plat(dev);
> -	if (start >= part->gpt_part_info.size)
> +	ret = blk_part_setup(dev, &start, blkcnt);
> +	if (ret)
>  		return 0;
>
> -	if ((start + blkcnt) > part->gpt_part_info.size)
> -		blkcnt = part->gpt_part_info.size - start;
> -	start += part->gpt_part_info.start;
> -
>  	return ops->write(parent, start, blkcnt, buffer);
>  }
>
> @@ -115,22 +123,18 @@ static ulong part_blk_erase(struct udevice *dev, lbaint_t start,
>  			    lbaint_t blkcnt)
>  {
>  	struct udevice *parent;
> -	struct disk_part *part;
>  	const struct blk_ops *ops;
> +	int ret;
>
>  	parent = dev_get_parent(dev);
>  	ops = blk_get_ops(parent);
>  	if (!ops->erase)
>  		return -ENOSYS;
>
> -	part = dev_get_uclass_plat(dev);
> -	if (start >= part->gpt_part_info.size)
> +	ret = blk_part_setup(dev, &start, blkcnt);
> +	if (ret)
>  		return 0;
>
> -	if ((start + blkcnt) > part->gpt_part_info.size)
> -		blkcnt = part->gpt_part_info.size - start;
> -	start += part->gpt_part_info.start;
> -
>  	return ops->erase(parent, start, blkcnt);
>  }
>
> --
> 2.40.0.rc1.284.g88254d51c5-goog
>

  reply	other threads:[~2023-03-20  7:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-19 19:29 [PATCH v2] disk: Use a helper function to reduce duplication Simon Glass
2023-03-20  7:31 ` Ilias Apalodimas [this message]
2023-03-20  7:57   ` AKASHI Takahiro
2023-03-31 14:17 ` Tom Rini
2023-04-03  0:06   ` AKASHI Takahiro
2023-04-03  1:16     ` Simon Glass
2023-04-03 14:46     ` Tom Rini

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=ZBgL7sydZIt/ztwD@hera \
    --to=ilias.apalodimas@linaro.org \
    --cc=sjg@chromium.org \
    --cc=takahiro.akashi@linaro.org \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.de \
    /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