]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Include <mutex>; constify a few methods
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Tue, 15 Sep 2020 11:21:09 +0000 (13:21 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Tue, 15 Sep 2020 11:21:09 +0000 (13:21 +0200)
pdns/recursordist/negcache.cc
pdns/recursordist/negcache.hh

index fd9b58e6f25124a0e957b37714ceabc17b72ce94..5897ffb725ab06ec4b17b6e4a0287b9e15fbd9eb 100644 (file)
@@ -43,10 +43,10 @@ NegCache::~NegCache()
   }
 }
 
-size_t NegCache::size()
+size_t NegCache::size() const
 {
   size_t count = 0;
-  for (auto& map : d_maps) {
+  for (const auto& map : d_maps) {
     count += map.d_entriesCount;
   }
   return count;
@@ -168,9 +168,9 @@ void NegCache::updateValidationStatus(const DNSName& qname, const QType& qtype,
  *
  * \param qname The name of the entries to be counted
  */
-size_t NegCache::count(const DNSName& qname)
+size_t NegCache::count(const DNSName& qname) const
 {
-  auto& map = getMap(qname);
+  const auto& map = getMap(qname);
   const lock l(map);
   return map.d_map.count(tie(qname));
 }
@@ -181,9 +181,9 @@ size_t NegCache::count(const DNSName& qname)
  * \param qname The name of the entries to be counted
  * \param qtype The type of the entries to be counted
  */
-size_t NegCache::count(const DNSName& qname, const QType qtype)
+size_t NegCache::count(const DNSName& qname, const QType qtype) const
 {
-  auto& map = getMap(qname);
+  const auto& map = getMap(qname);
   const lock l(map);
   return map.d_map.count(tie(qname, qtype));
 }
@@ -252,29 +252,30 @@ void NegCache::prune(size_t maxEntries)
  *
  * \param fp A pointer to an open FILE object
  */
-size_t NegCache::dumpToFile(FILE* fp)
+size_t NegCache::dumpToFile(FILE* fp) const
 {
-  size_t ret(0);
+  size_t ret = 0;
   struct timeval now;
   Utility::gettimeofday(&now, nullptr);
 
-  for (auto& m : d_maps) {
+  for (const auto& m : d_maps) {
     const lock l(m);
     auto& sidx = m.d_map.get<SequenceTag>();
     for (const NegCacheEntry& ne : sidx) {
       ret++;
-      fprintf(fp, "%s %" PRId64 " IN %s VIA %s ; (%s)\n", ne.d_name.toString().c_str(), static_cast<int64_t>(ne.d_ttd - now.tv_sec), ne.d_qtype.getName().c_str(), ne.d_auth.toString().c_str(), vStateToString(ne.d_validationState).c_str());
+      int64_t ttl = ne.d_ttd - now.tv_sec;
+      fprintf(fp, "%s %" PRId64 " IN %s VIA %s ; (%s)\n", ne.d_name.toString().c_str(), ttl, ne.d_qtype.getName().c_str(), ne.d_auth.toString().c_str(), vStateToString(ne.d_validationState).c_str());
       for (const auto& rec : ne.authoritySOA.records) {
-        fprintf(fp, "%s %" PRId64 " IN %s %s ; (%s)\n", rec.d_name.toString().c_str(), static_cast<int64_t>(ne.d_ttd - now.tv_sec), DNSRecordContent::NumberToType(rec.d_type).c_str(), rec.d_content->getZoneRepresentation().c_str(), vStateToString(ne.d_validationState).c_str());
+        fprintf(fp, "%s %" PRId64 " IN %s %s ; (%s)\n", rec.d_name.toString().c_str(), ttl, DNSRecordContent::NumberToType(rec.d_type).c_str(), rec.d_content->getZoneRepresentation().c_str(), vStateToString(ne.d_validationState).c_str());
       }
       for (const auto& sig : ne.authoritySOA.signatures) {
-        fprintf(fp, "%s %" PRId64 " IN RRSIG %s ;\n", sig.d_name.toString().c_str(), static_cast<int64_t>(ne.d_ttd - now.tv_sec), sig.d_content->getZoneRepresentation().c_str());
+        fprintf(fp, "%s %" PRId64 " IN RRSIG %s ;\n", sig.d_name.toString().c_str(), ttl, sig.d_content->getZoneRepresentation().c_str());
       }
       for (const auto& rec : ne.DNSSECRecords.records) {
-        fprintf(fp, "%s %" PRId64 " IN %s %s ; (%s)\n", rec.d_name.toString().c_str(), static_cast<int64_t>(ne.d_ttd - now.tv_sec), DNSRecordContent::NumberToType(rec.d_type).c_str(), rec.d_content->getZoneRepresentation().c_str(), vStateToString(ne.d_validationState).c_str());
+        fprintf(fp, "%s %" PRId64 " IN %s %s ; (%s)\n", rec.d_name.toString().c_str(), ttl, DNSRecordContent::NumberToType(rec.d_type).c_str(), rec.d_content->getZoneRepresentation().c_str(), vStateToString(ne.d_validationState).c_str());
       }
       for (const auto& sig : ne.DNSSECRecords.signatures) {
-        fprintf(fp, "%s %" PRId64 " IN RRSIG %s ;\n", sig.d_name.toString().c_str(), static_cast<int64_t>(ne.d_ttd - now.tv_sec), sig.d_content->getZoneRepresentation().c_str());
+        fprintf(fp, "%s %" PRId64 " IN RRSIG %s ;\n", sig.d_name.toString().c_str(), ttl, sig.d_content->getZoneRepresentation().c_str());
       }
     }
   }
index 91b37178ba1f8c8f4e4a49bbce8c80d0a3d94d20..894d07501b7284fa1ef476de97e024abe4a469d8 100644 (file)
@@ -21,6 +21,8 @@
  */
 #pragma once
 
+#include <mutex>
+#include <vector>
 #include <boost/multi_index_container.hpp>
 #include <boost/multi_index/hashed_index.hpp>
 #include "dnsparser.hh"
@@ -70,13 +72,13 @@ public:
   void updateValidationStatus(const DNSName& qname, const QType& qtype, const vState newState, boost::optional<time_t> capTTD);
   bool get(const DNSName& qname, const QType& qtype, const struct timeval& now, NegCacheEntry& ne, bool typeMustMatch = false);
   bool getRootNXTrust(const DNSName& qname, const struct timeval& now, NegCacheEntry& ne);
-  size_t count(const DNSName& qname);
-  size_t count(const DNSName& qname, const QType qtype);
+  size_t count(const DNSName& qname) const;
+  size_t count(const DNSName& qname, const QType qtype) const;
   void prune(size_t maxEntries);
   void clear();
-  size_t dumpToFile(FILE* fd);
+  size_t dumpToFile(FILE* fd) const;
   size_t wipe(const DNSName& name, bool subtree = false);
-  size_t size();
+  size_t size() const;
 
   void preRemoval(const NegCacheEntry& entry)
   {
@@ -110,10 +112,10 @@ private:
     MapCombo(const MapCombo &) = delete;
     MapCombo & operator=(const MapCombo &) = delete;
     negcache_t d_map;
-    std::mutex mutex;
+    mutable std::mutex mutex;
     std::atomic<uint64_t> d_entriesCount{0};
-    uint64_t d_contended_count{0};
-    uint64_t d_acquired_count{0};
+    mutable uint64_t d_contended_count{0};
+    mutable uint64_t d_acquired_count{0};
     bool d_cachecachevalid{false}; // XXX
   };
 
@@ -123,9 +125,13 @@ private:
   {
     return d_maps[qname.hash() % d_maps.size()];
   }
+  const MapCombo& getMap(const DNSName &qname) const
+  {
+    return d_maps[qname.hash() % d_maps.size()];
+  }
 public:
   struct lock {
-    lock(MapCombo& map) : m(map.mutex)
+    lock(const MapCombo& map) : m(map.mutex)
     {
       if (!m.try_lock()) {
         m.lock();