[ccan] Additional macro to iterate over list (#1)

Rusty Russell rusty at rustcorp.com.au
Mon Nov 21 12:59:39 EST 2011


On Wed, 16 Nov 2011 03:07:29 -0800, ndreys <reply+i-2255063-93fcad02060c514ba3c4dd5329ddee92ebf69400-775844 at reply.github.com> wrote:
> Neither list_for_each, nor list_for_each_safe allow user to iterate
> over a list if the structure of its elements is opaque.
> List_for_each_opaque provides said functionality, for opaque
> structures pointers to which can be safely cast to `struct
> node_list *`. In other words if the corresponding `struct
> node_list` field is placed at the top of the structure declaration.

Interesting.  The limitation that the list struct be the first member
makes this dangerous to use, though.

Should we go for a more generic wrapper, like so:


        list_for_each_off(h, i, off)

Where off == 0 is your case.  That at least makes the user realize their
implicit assumption, and may allow the others to be implemented in terms
of that macro (using offsetof() and typeof()?)...

> P.S. I tried my best to figure out how the testing infrastructure
> works, but I'm afraid I wasn't completely successful. I added my tests
> to run.c, but the purpose of run-with-debug.c(looks completely
> identical) and the mechanics behind using "Examples" section of
> documentation for testing completely evade me. So for now I just
> ignored them but if it's necessary I'll add tests there.

Your run.c patch was fine.  run-with-debug.c defines CCAN_LIST_DEBUG
before including the list module, so it runs with all the debug checks.

I will fix it now to be clearer, ie:

        #define CCAN_LIST_DEBUG 1
        #include <ccan/list/test/run.c>

As to examples, you need to have an Example: header so it can be found
by ccanlint.  *Then* it's kind of tricky, because it tries to compile
each example, where necessary combining it with previous ones.  When you
insert a new function like this, it's easy to break the chain and break
following examples...

Thanks!
Rusty.


More information about the ccan mailing list