All of lore.kernel.org
 help / color / mirror / Atom feed
From: Werner Almesberger <werner@almesberger.net>
To: Alan Ott <alan@signal11.us>
Cc: Varka Bhadram <varkabhadram@gmail.com>,
	alex.aring@gmail.com, Varka Bhadram <varkab@cdac.in>,
	netdev@vger.kernel.org, linux-zigbee-devel@lists.sourceforge.net,
	davem@davemloft.net
Subject: Re: [Linux-zigbee-devel] [PATCH] mrf24j40: seperate mrf24j40 h/w init and add checkings
Date: Wed, 23 Apr 2014 11:36:34 -0300	[thread overview]
Message-ID: <20140423143633.GA19663@ws> (raw)
In-Reply-To: <5357C565.8080009@signal11.us>

Alan Ott wrote:
> 3. The code to check for it just adds a lot of bloat without much 
> measurable benefit.

As a very general note, if - in any C program, not just the kernel
- you have too many error checks for comfort, you may want to
consider keeping a cumulative error status (e.g., in this case,
struct mrf24j40), and just check that. Similar to ferror in stdio.

Something like


static int my_check_and_clear_rc(struct foo *foo)
{
	int rc;

	rc = foo->rc;
	foo->rc = 0;
	return rc;
}


static int my_operation(struct foo *foo, int arg)
{
	int rc;

	/* don't make it worse - optional */
	if (foo->rc)
		return foo->rc;

	rc = really_do_my_operation(foo, arg);
	if (rc < 0 && !foo->rc)
		foo->rc = rc;

	return foo->rc ? foo->rc : rc;
}


Then the phalanx of tedious checks shrinks to

	/* make sure foo->rc is initialized to 0 */

	my_operation(foo, 1);
	my_operation(foo, 2);
	...
	my_operation(foo, 1000);

	rc = my_check_and_clear_rc(foo);
	if (rc) {
		complain("something terribly wrong");
		...
	}
	...

You can easily extend this to also record line numbers, file
names, and such, if necessary. Add atomic/locking as needed.

- Werner

      parent reply	other threads:[~2014-04-23 14:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-23 13:11 [PATCH] mrf24j40: seperate mrf24j40 h/w init and add checkings Varka Bhadram
2014-04-23 13:51 ` Alan Ott
2014-04-23 14:00   ` Alexander Aring
2014-04-24  3:19     ` Varka Bhadram
2014-04-23 14:36   ` Werner Almesberger [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=20140423143633.GA19663@ws \
    --to=werner@almesberger.net \
    --cc=alan@signal11.us \
    --cc=alex.aring@gmail.com \
    --cc=davem@davemloft.net \
    --cc=linux-zigbee-devel@lists.sourceforge.net \
    --cc=netdev@vger.kernel.org \
    --cc=varkab@cdac.in \
    --cc=varkabhadram@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.