From: Andrew Morton <akpm@osdl.org>
To: Jeff Garzik <jeff@garzik.org>
Cc: ambx1@neo.rr.com, LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] PNP: handle sysfs errors
Date: Wed, 11 Oct 2006 15:45:01 -0700 [thread overview]
Message-ID: <20061011154501.7ae43d56.akpm@osdl.org> (raw)
In-Reply-To: <20061011215109.GA22264@havoc.gtf.org>
On Wed, 11 Oct 2006 17:51:09 -0400
Jeff Garzik <jeff@garzik.org> wrote:
> + int rc = device_create_file(&dev->dev,&dev_attr_options);
> + if (rc) goto err;
> + rc = device_create_file(&dev->dev,&dev_attr_resources);
> + if (rc) goto err_opt;
> + rc = device_create_file(&dev->dev,&dev_attr_id);
> + if (rc) goto err_res;
> +
> return 0;
> +
> +err_res:
> + device_remove_file(&dev->dev,&dev_attr_resources);
> +err_opt:
> + device_remove_file(&dev->dev,&dev_attr_options);
> +err:
> + return rc;
That's a common pattern, isn't it?
I wonder if we could create some sort of automatic-unwinding engine:
int pnp_interface_attach_device(struct pnp_dev *dev)
{
struct unwind_engine *u = NULL;
int err;
u = UNWINDABLE(u, err, device_create_file(&dev->dev,&dev_attr_options),
device_remove_file, &dev->dev, &dev_attr_options);
u = UNWINDABLE(u, err, device_create_file(&dev->dev,&dev_attr_id)
device_remove_file, &dev->dev, &dev_attr_options);
u = UNWINDABLE(u, err, device_create_file(&dev->dev,&dev_attr_id),
device_remove_file, &dev->dev, &dev_attr_id);
err = unwind(err, u);
return err;
}
and, umm,
#define UNWINDABLE(u, err, expr, undo_fn, arg1, arg2)
if (err == 0) {
err = (expr);
if (err == 0)
u = add_unwind(u, undo_fn, arg1, arg2);
}
u;
int unwind(int err, struct unwind_engine *u)
{
if (err == 0)
return 0;
for (all entries in u in opposite order) {
u->fn(u->arg0, u>arg1);
kfree(u);
}
return err;
}
I dunno - probably too crappy to live, but it'd encourage/help people to
dtrt.
next prev parent reply other threads:[~2006-10-11 22:46 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-10-11 21:51 [PATCH] PNP: handle sysfs errors Jeff Garzik
2006-10-11 22:45 ` Andrew Morton [this message]
2006-10-12 3:54 ` Dmitry Torokhov
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=20061011154501.7ae43d56.akpm@osdl.org \
--to=akpm@osdl.org \
--cc=ambx1@neo.rr.com \
--cc=jeff@garzik.org \
--cc=linux-kernel@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