From ae997d9e210033c6dea7a236a2a4783bfd31b376 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Mon, 24 Apr 2023 12:40:33 +0200 Subject: [PATCH] Add ISC_LIST_FOREACH(_SAFE) macros There's a recurring pattern walking the ISC_LISTs that just repeats over and over. Add two macros: * ISC_LIST_FOREACH(list, elt, link) - walk the static list * ISC_LIST_FOREACH_SAFE(list, elt, link, next) - walk the list in a manner that's safe against list member deletions --- .clang-format | 2 +- lib/isc/include/isc/list.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/.clang-format b/.clang-format index 6a8388bc91..124d82a269 100644 --- a/.clang-format +++ b/.clang-format @@ -78,4 +78,4 @@ PenaltyBreakString: 80 PenaltyExcessCharacter: 100 Standard: Cpp11 ContinuationIndentWidth: 8 -ForEachMacros: [ 'cds_lfs_for_each', 'cds_lfs_for_each_safe', 'cds_list_for_each_entry_safe' ] +ForEachMacros: [ 'cds_lfs_for_each', 'cds_lfs_for_each_safe', 'cds_list_for_each_entry_safe', 'ISC_LIST_FOREACH', 'ISC_LIST_FOREACH_SAFE' ] diff --git a/lib/isc/include/isc/list.h b/lib/isc/include/isc/list.h index 2cf4437542..a168254d40 100644 --- a/lib/isc/include/isc/list.h +++ b/lib/isc/include/isc/list.h @@ -227,3 +227,17 @@ INSIST(ISC_LIST_EMPTY(dest)); \ ISC_LIST_MOVEUNSAFE(dest, src); \ } + +/* clang-format off */ +#define ISC_LIST_FOREACH(list, elt, link) \ + for (elt = ISC_LIST_HEAD(list); \ + elt != NULL; \ + elt = ISC_LIST_NEXT(elt, link)) +/* clang-format on */ + +/* clang-format off */ +#define ISC_LIST_FOREACH_SAFE(list, elt, link, next) \ + for (elt = ISC_LIST_HEAD(list), next = (elt != NULL) ? ISC_LIST_NEXT(elt, link) : NULL; \ + elt != NULL; \ + elt = next, next = (elt != NULL) ? ISC_LIST_NEXT(elt, link) : NULL) +/* clang-format on */