From mboxrd@z Thu Jan 1 00:00:00 1970 From: Simon Glass Date: Sun, 8 Nov 2015 23:47:52 -0700 Subject: [U-Boot] [PATCH v2 10/26] dm: core: Add safe device iteration macros In-Reply-To: <1447051688-24936-1-git-send-email-sjg@chromium.org> References: <1447051688-24936-1-git-send-email-sjg@chromium.org> Message-ID: <1447051688-24936-11-git-send-email-sjg@chromium.org> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de Add iteration macros which support unbinding a device within the loop. Signed-off-by: Simon Glass --- Changes in v2: None include/dm/device.h | 12 ++++++++++++ include/dm/uclass.h | 15 +++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/dm/device.h b/include/dm/device.h index 28ba4ca..7fb9935 100644 --- a/include/dm/device.h +++ b/include/dm/device.h @@ -509,6 +509,18 @@ static inline bool device_is_on_pci_bus(struct udevice *dev) return device_get_uclass_id(dev->parent) == UCLASS_PCI; } +/** + * device_foreach_child_safe() - iterate through child devices safely + * + * This allows the @pos child to be removed in the loop if required. + * + * @pos: struct udevice * for the current device + * @next: struct udevice * for the next device + * @parent: parent device to scan + */ +#define device_foreach_child_safe(pos, next, parent) \ + list_for_each_entry_safe(pos, next, &parent->child_head, sibling_node) + /* device resource management */ typedef void (*dr_release_t)(struct udevice *dev, void *res); typedef int (*dr_match_t)(struct udevice *dev, void *res, void *match_data); diff --git a/include/dm/uclass.h b/include/dm/uclass.h index d214b88..bfbd27a 100644 --- a/include/dm/uclass.h +++ b/include/dm/uclass.h @@ -243,4 +243,19 @@ int uclass_resolve_seq(struct udevice *dev); #define uclass_foreach_dev(pos, uc) \ list_for_each_entry(pos, &uc->dev_head, uclass_node) +/** + * uclass_foreach_dev_safe() - Helper function to safely iteration through devs + * + * This creates a for() loop which works through the available devices in + * a uclass in order from start to end. Inside the loop, it is safe to remove + * @pos if required. + * + * @pos: struct udevice * to hold the current device. Set to NULL when there + * are no more devices. + * @next: struct udevice * to hold the next next + * @uc: uclass to scan + */ +#define uclass_foreach_dev_safe(pos, next, uc) \ + list_for_each_entry_safe(pos, next, &uc->dev_head, uclass_node) + #endif -- 2.6.0.rc2.230.g3dd15c0