Re: [RFC v2 2/7] tables.h: add linker table support

From: Luis R. Rodriguez
Date: Fri Feb 19 2016 - 16:12:53 EST


On Fri, Feb 19, 2016 at 12:33:27PM -0800, H. Peter Anvin wrote:
> On 02/19/2016 05:45 AM, Luis R. Rodriguez wrote:
> > +/**
> > + * LINKTABLE_RUN_ERR - run each linker table entry func and return error if any
> > + *
> > + * @tbl: linker table
> > + * @func: structure name for the function name we want to call.
> > + * @args...: arguments to pass to func
> > + *
> > + * Example usage:
> > + *
> > + * unsigned int err = LINKTABLE_RUN_ERR(frobnicator_fns, some_run,);
> > + */
> > +#define LINKTABLE_RUN_ERR(tbl, func, args...) \
> > +({ \
> > + size_t i; \
> > + int err = 0; \
> > + for (i = 0; !err && i < LINKTABLE_SIZE(tbl); i++) \
> > + err = (tbl[i]).func (args); \
> > + err; \
> > +})
>
> This is wrong and pointless. As written it returns the error code of
> the last instance.
>
> What I suggested for this macro was that we ought to exit the loop on error.
>
> Furthermore:
>
> 1. Using an advancing pointer would make more sense than a counter.

OK I'll modify to use LINKTABLE_FOR_EACH and much cleaner and obvious
code to bail on error.

> 2. .func doesn't make any sense -- it ought to be "func"; otherwise you
> can't call into either a table containing pure function pointers,
> nor into a field of table *pointed to* by the table; which is
> likely to be quite common.
>
> So the idea is to use something like:
>
> /* Array of function pointers */
> LINKTABLE_RUN_ALL(frobnicator_fns, , arg1, arg2);
>
> /* Array of structures */
> LINKTABLE_RUN_ALL(frobnicator_fns, .some_run, arg1, arg2);
>
> /* Array of structure pointers */
> LINKTABLE_RUN_ALL(frobnicator_fns, ->some_run, arg1, arg2);

Ah that's sexy, thanks. Will amend.

Luis