[PATCH] libfdt: Add fdt_next_subnode() to permit easy subnode iteration
David Gibson
david at gibson.dropbear.id.au
Fri Apr 26 21:59:51 EST 2013
On Thu, Apr 25, 2013 at 11:22:56AM -0700, Simon Glass wrote:
> Iterating through subnodes with libfdt is a little painful to write as we
> need something like this:
>
> for (depth = 0, count = 0,
> offset = fdt_next_node(fdt, parent_offset, &depth);
> (offset >= 0) && (depth > 0);
> offset = fdt_next_node(fdt, offset, &depth)) {
> if (depth == 1) {
> /* code body */
> }
> }
>
> Using fdt_next_subnode() we can instead write this, which is shorter and
> easier to get right:
>
> for (depth = 0, offset = fdt_next_subnode(fdt, parent_offset, &depth);
> offset >= 0;
> offset = fdt_next_subnode(fdt, offset, &depth)) {
> /* code body */
> }
>
> Also, it doesn't require two levels of indentation for the loop
> body.
I like the idea in principle. But if you split this into
find_first_subnode() and find_next_subnode() functions, you should be
able to get rid of the requirement for the caller to provide the depth
variable.
--
David Gibson | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://lists.ozlabs.org/pipermail/devicetree-discuss/attachments/20130426/efc6d471/attachment-0001.sig>
More information about the devicetree-discuss
mailing list