From b49c9c1ad3b5be46fb5bd9451d26f133bbe98126 Mon Sep 17 00:00:00 2001 From: Martin Nagy Date: Wed, 29 Jul 2009 09:58:53 +0200 Subject: Add FOR_EACH style macros The FOR_EACH() macro will simply traverse the given list. Usage: FOR_EACH(element, list) do_something(element); The FOR_EACH_UNLINK() macro will traverse the list and unlink each element. Useful when destroying a whole list. Usage: FOR_EACH_UNLINK(element, list) { destroy(&element); } END_FOR_EACH_UNLINK(element); All these macros assume that 'link' is used to connect list elements, as used in standard ISC list macros. --- src/util.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/util.h b/src/util.h index 372cd4d..0dd6ee3 100644 --- a/src/util.h +++ b/src/util.h @@ -89,4 +89,19 @@ dns_name_setbuffer(&name, &name##__buffer); \ } while (0) +#define FOR_EACH(elt, list) \ + for ((elt) = HEAD(list); (elt) != NULL; (elt) = NEXT(elt, link)) + +#define FOR_EACH_UNLINK(elt, list) \ + do { \ + typeof(elt) __next_elt; \ + for (elt = HEAD(list); elt != NULL; elt = NEXT(elt, link)) { \ + __next_elt = NEXT(elt, link); \ + UNLINK(list, elt, link); + +#define END_FOR_EACH_UNLINK(elt) \ + elt = __next_elt; \ + } \ + } while (0) + #endif /* !_LD_UTIL_H_ */ -- cgit