Re: [PATCH 5/5] CUSE: implement CUSE - Character device inUserspace

From: Andrew Morton
Date: Thu Aug 28 2008 - 18:33:19 EST


On Thu, 28 Aug 2008 15:15:25 -0700
Greg KH <greg@xxxxxxxxx> wrote:

> On Thu, Aug 28, 2008 at 01:07:40PM -0700, Andrew Morton wrote:
> > On Fri, 29 Aug 2008 03:19:04 +0900
> > Tejun Heo <tj@xxxxxxxxxx> wrote:
> > > +#define fc_to_cc(_fc) container_of((_fc), struct cuse_conn, fc)
> > > +#define cdev_to_cc(_cdev) container_of((_cdev), struct cuse_conn, cdev)
> > > +#define cuse_conn_get(cc) ({mntget((cc)->mnt); cc;})
> > > +#define cuse_conn_put(cc) mntput((cc)->mnt)
> >
> > I believe all the above could be implemented in C.
>
> "traditionally" container_of() is used in #define, not a function call
> as it is just pointer math that can be done at compile time.
>

Well yeah. But it isn't a very good tradition.

static inline struct cuse_conn *cdev_to_cc(struct cdev *cdev)
{
return container_of(cdev, struct cuse_conn, cdev);
}

should generate the same code and is prettier.

Unfortunately it has no additional type-safety. You can still pass it
the address of a tty_driver.cdev instead of a cuse_conn.cdev and the
compiler will happily swallow it. Not a big problem in practice though.

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/