diff --git a/.clang-format b/.clang-format index b26764d5d0..e5014cb777 100644 --- a/.clang-format +++ b/.clang-format @@ -79,7 +79,7 @@ PenaltyBreakString: 80 PenaltyExcessCharacter: 100 Standard: Cpp11 ContinuationIndentWidth: 8 -ForEachMacros: [ 'cds_lfs_for_each', 'cds_lfs_for_each_safe', 'cds_list_for_each_entry_safe', 'ISC_LIST_FOREACH', 'ISC_LIST_FOREACH_SAFE', 'ISC_LIST_FOREACH_REV', 'ISC_LIST_FOREACH_REV_SAFE', 'MSG_SECTION_FOREACH', 'DNS_DBITERATOR_FOREACH', 'DNS_RDATASET_FOREACH', 'DNS_RDATASETITER_FOREACH', 'CFG_LIST_FOREACH' ] +ForEachMacros: [ 'cds_lfs_for_each', 'cds_lfs_for_each_safe', 'cds_list_for_each_entry_safe', 'ISC_LIST_FOREACH', 'ISC_LIST_FOREACH_SAFE', 'ISC_LIST_FOREACH_REV', 'ISC_LIST_FOREACH_REV_SAFE', 'MSG_SECTION_FOREACH', 'DNS_DBITERATOR_FOREACH', 'DNS_RDATASET_FOREACH', 'DNS_RDATASETITER_FOREACH', 'CFG_LIST_FOREACH', 'DNS_SLABTOP_FOREACH', 'DNS_SLABTOP_FOREACH_SAFE', 'DNS_SLABTOP_FOREACH_NEXT' ] RemoveParentheses: ReturnStatement RemoveSemicolon: true SpaceBeforeParens: ControlStatementsExceptControlMacros diff --git a/lib/dns/include/dns/rdataslab.h b/lib/dns/include/dns/rdataslab.h index a292b4acd3..6f8e04985a 100644 --- a/lib/dns/include/dns/rdataslab.h +++ b/lib/dns/include/dns/rdataslab.h @@ -64,6 +64,17 @@ struct dns_slabheader_proof { dns_rdatatype_t type; }; +#define DNS_SLABTOP_FOREACH(node, elt) \ + for (elt = (node)->data; elt != NULL; elt = elt->next) + +#define DNS_SLABTOP_FOREACH_NEXT(elt, elt_next) \ + for (elt = elt_next; elt != NULL; elt = elt->next) + +#define DNS_SLABTOP_FOREACH_SAFE(node, elt, elt_next) \ + for (elt = (node)->data, elt_next = (elt != NULL) ? elt->next : NULL; \ + elt != NULL; \ + elt = elt_next, elt_next = (elt != NULL) ? elt->next : NULL) + typedef struct dns_slabtop dns_slabtop_t; struct dns_slabtop { dns_slabtop_t *next; diff --git a/lib/dns/qpcache.c b/lib/dns/qpcache.c index 5f60293e71..ae1a2b9d36 100644 --- a/lib/dns/qpcache.c +++ b/lib/dns/qpcache.c @@ -565,8 +565,7 @@ clean_cache_node(qpcache_t *qpdb, qpcnode_t *node) { * Caller must be holding the node lock. */ - for (top = node->data; top != NULL; top = top_next) { - top_next = top->next; + DNS_SLABTOP_FOREACH_SAFE(node, top, top_next) { clean_stale_headers(top->header); /* @@ -1289,7 +1288,7 @@ check_zonecut(qpcnode_t *node, void *arg DNS__DB_FLARG) { /* * Look for a DNAME or RRSIG DNAME rdataset. */ - for (top = node->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(node, top) { if (check_stale_header(top->header, search)) { continue; } @@ -1352,7 +1351,7 @@ find_deepest_zonecut(qpc_search_t *search, qpcnode_t *node, /* * Look for NS and RRSIG NS rdatasets. */ - for (top = node->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(node, top) { if (check_stale_header(top->header, search)) { continue; } @@ -1456,7 +1455,7 @@ find_coveringnsec(qpc_search_t *search, const dns_name_t *name, nlock = &search->qpdb->buckets[node->locknum].lock; NODE_RDLOCK(nlock, &nlocktype); - for (top = node->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(node, top) { if (check_stale_header(top->header, search)) { continue; } @@ -1657,7 +1656,7 @@ qpcache_find(dns_db_t *db, const dns_name_t *name, dns_dbversion_t *version, nsecsig = NULL; cnamesig = NULL; empty_node = true; - for (top = node->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(node, top) { if (check_stale_header(top->header, &search)) { continue; } @@ -1924,7 +1923,7 @@ seek_ns_headers(qpc_search_t *search, qpcnode_t *node, dns_dbnode_t **nodep, NODE_RDLOCK(nlock, &nlocktype); - for (top = node->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(node, top) { bool ns = top->typepair == DNS_TYPEPAIR(dns_rdatatype_ns) || top->typepair == DNS_SIGTYPEPAIR(dns_rdatatype_ns); if (check_stale_header(top->header, search)) { @@ -2095,7 +2094,7 @@ qpcache_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version, sigpair = !dns_rdatatype_issig(type) ? DNS_SIGTYPEPAIR(type) : dns_typepair_none; - for (top = qpnode->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(qpnode, top) { if (check_stale_header(top->header, &search)) { continue; } @@ -2586,9 +2585,7 @@ add(qpcache_t *qpdb, qpcnode_t *qpnode, dns_slabheader_t *newheader, * only rdataset that can be found at this * node is the negative cache entry. */ - for (top = qpnode->data; top != NULL; - top = top->next) - { + DNS_SLABTOP_FOREACH(qpnode, top) { mark_ancient(top->header); } goto find_header; @@ -2601,9 +2598,7 @@ add(qpcache_t *qpdb, qpcnode_t *qpnode, dns_slabheader_t *newheader, * ancient. */ - for (top = qpnode->data; top != NULL; - top = top->next) - { + DNS_SLABTOP_FOREACH(qpnode, top) { if (rdtype == DNS_TYPEPAIR_TYPE(top->typepair)) { @@ -2617,7 +2612,7 @@ add(qpcache_t *qpdb, qpcnode_t *qpnode, dns_slabheader_t *newheader, * Otherwise look for any RRSIGs of the given * type so they can be marked ancient later. */ - for (top = qpnode->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(qpnode, top) { if (top->typepair == sigpair) { sigheader = top->header; break; @@ -2628,7 +2623,7 @@ add(qpcache_t *qpdb, qpcnode_t *qpnode, dns_slabheader_t *newheader, * We're adding something that isn't a * negative cache entry. */ - for (top = qpnode->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(qpnode, top) { /* * Look for any existing negative cache * entries. @@ -2684,7 +2679,7 @@ add(qpcache_t *qpdb, qpcnode_t *qpnode, dns_slabheader_t *newheader, } } - for (top = qpnode->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(qpnode, top) { if (ACTIVE(top->header, now)) { ++ntypes; expiretop = top; @@ -3348,7 +3343,7 @@ rdatasetiter_first(dns_rdatasetiter_t *it DNS__DB_FLARG) { NODE_RDLOCK(nlock, &nlocktype); - for (top = qpnode->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(qpnode, top) { if (EXISTS(top->header) && (EXPIREDOK(iterator) || iterator_active(qpdb, iterator, top->header))) @@ -3384,7 +3379,7 @@ rdatasetiter_next(dns_rdatasetiter_t *it DNS__DB_FLARG) { NODE_RDLOCK(nlock, &nlocktype); - for (top = top->next; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH_NEXT(top, top->next) { if (EXISTS(top->header) && (EXPIREDOK(iterator) || iterator_active(qpdb, iterator, top->header))) @@ -3862,9 +3857,7 @@ qpcnode_destroy(qpcnode_t *qpnode) { dns_slabtop_t *top = NULL, *top_next = NULL; qpcache_t *qpdb = qpnode->qpdb; - for (top = qpnode->data; top != NULL; top = top_next) { - top_next = top->next; - + DNS_SLABTOP_FOREACH_SAFE(qpnode, top, top_next) { dns_slabheader_t *down = NULL, *down_next = NULL; for (down = top->header; down != NULL; down = down_next) { down_next = down->down; diff --git a/lib/dns/qpzone.c b/lib/dns/qpzone.c index 247434d3ac..be5066ff73 100644 --- a/lib/dns/qpzone.c +++ b/lib/dns/qpzone.c @@ -822,9 +822,7 @@ clean_zone_node(qpznode_t *node, uint32_t least_serial) { */ REQUIRE(least_serial != 0); - for (top = node->data; top != NULL; top = top_next) { - top_next = top->next; - + DNS_SLABTOP_FOREACH_SAFE(node, top, top_next) { INSIST(top->header != NULL); /* @@ -1293,7 +1291,7 @@ rollback_node(qpznode_t *node, uint32_t serial) { * 'serial'. When the reference count goes to zero, these rdatasets * will be cleaned up; until that time, they will be ignored. */ - for (top = node->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(node, top) { dns_slabheader_t *header = top->header; if (header->serial == serial) { @@ -1587,7 +1585,7 @@ qpzone_findrdataset(dns_db_t *db, dns_dbnode_t *dbnode, sigpair = dns_typepair_none; } - for (top = node->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(node, top) { dns_slabheader_t *header = top->header; do { if (header->serial <= serial && !IGNORE(header)) { @@ -1715,7 +1713,7 @@ cname_and_other(qpznode_t *node, uint32_t serial) { * ("Other data" is any rdataset whose type is not KEY, NSEC, SIG * or RRSIG. */ - for (top = node->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(node, top) { dns_slabheader_t *header = top->header; rdtype = DNS_TYPEPAIR_TYPE(top->typepair); @@ -1842,7 +1840,7 @@ add(qpzonedb_t *qpdb, qpznode_t *node, const dns_name_t *nodename, } ntypes = 0; - for (top = node->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(node, top) { ++ntypes; if (prio_type(top->typepair)) { priotop = top; @@ -2677,7 +2675,7 @@ step(qpz_search_t *search, dns_qpiter_t *it, direction_t direction, dns_slabheader_t *found = NULL; NODE_RDLOCK(nlock, &nlocktype); - for (top = node->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(node, top) { dns_slabheader_t *header = top->header; while (header != NULL && (IGNORE(header) || @@ -2839,7 +2837,7 @@ find_wildcard(qpz_search_t *search, qpznode_t **nodep, const dns_name_t *qname, * may not need the information, because it simplifies the * locking and code flow. */ - for (top = node->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(node, top) { dns_slabheader_t *header = top->header; if (header->serial <= search->serial && !IGNORE(header) && EXISTS(header)) @@ -2879,9 +2877,7 @@ find_wildcard(qpz_search_t *search, qpznode_t **nodep, const dns_name_t *qname, */ nlock = qpzone_get_lock(wnode); NODE_RDLOCK(nlock, &nlocktype); - for (top = wnode->data; top != NULL; - top = top->next) - { + DNS_SLABTOP_FOREACH(wnode, top) { dns_slabheader_t *header = top->header; if (header->serial <= search->serial && !IGNORE(header) && EXISTS(header)) @@ -3061,7 +3057,7 @@ again: isc_rwlock_t *nlock = qpzone_get_lock(node); NODE_RDLOCK(nlock, &nlocktype); empty_node = true; - for (top = node->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(node, top) { dns_slabheader_t *header = top->header; /* * Look for an active, extant NSEC or RRSIG NSEC. @@ -3205,7 +3201,7 @@ qpzone_check_zonecut(qpznode_t *node, void *arg DNS__DB_FLARG) { /* * Look for an NS or DNAME rdataset active in our version. */ - for (top = node->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(node, top) { dns_slabheader_t *header = top->header; if (top->typepair == DNS_TYPEPAIR(dns_rdatatype_ns) || top->typepair == DNS_TYPEPAIR(dns_rdatatype_dname) || @@ -3536,7 +3532,7 @@ found: sigpair = DNS_SIGTYPEPAIR(type); empty_node = true; - for (top = node->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(node, top) { dns_slabheader_t *header = top->header; /* * Look for an active, extant rdataset. @@ -4049,7 +4045,7 @@ rdatasetiter_first(dns_rdatasetiter_t *iterator DNS__DB_FLARG) { NODE_RDLOCK(nlock, &nlocktype); - for (top = node->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(node, top) { dns_slabheader_t *header = top->header; while (header != NULL && (IGNORE(header) || header->serial > version->serial)) @@ -4094,7 +4090,7 @@ rdatasetiter_next(dns_rdatasetiter_t *iterator DNS__DB_FLARG) { /* * Find the start of the header chain for the next type. */ - for (top = top->next; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH_NEXT(top, top->next) { dns_slabheader_t *header = top->header; while (header != NULL && (IGNORE(header) || header->serial > version->serial)) @@ -4881,7 +4877,7 @@ qpzone_subtractrdataset(dns_db_t *db, dns_dbnode_t *dbnode, NODE_WRLOCK(nlock, &nlocktype); changed = add_changed(qpdb, newheader, version DNS__DB_FLARG_PASS); - for (top = node->data; top != NULL; top = top->next) { + DNS_SLABTOP_FOREACH(node, top) { if (top->typepair == newheader->typepair) { break; } @@ -5435,9 +5431,7 @@ static void destroy_qpznode(qpznode_t *node) { dns_slabtop_t *top = NULL, *top_next = NULL; - for (top = node->data; top != NULL; top = top_next) { - top_next = top->next; - + DNS_SLABTOP_FOREACH_SAFE(node, top, top_next) { dns_slabheader_t *down = NULL, *down_next = NULL; for (down = top->header; down != NULL; down = down_next) { down_next = down->down;