]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #2257 in SNORT/snort3 from ~ANTOROZC/snort3:duapalme_replace_cache...
authorSteve Chew (stechew) <stechew@cisco.com>
Fri, 12 Jun 2020 03:17:27 +0000 (03:17 +0000)
committerSteve Chew (stechew) <stechew@cisco.com>
Fri, 12 Jun 2020 03:17:27 +0000 (03:17 +0000)
Squashed commit of the following:

commit 90cf5e3a304b16b6494ed496c2f6d326dad0a381
Author: Duane Palmer <duapalme@cisco.com>
Date:   Thu Jun 11 15:34:47 2020 -0500

    lru_cache_shared: replace the cache entry if found

src/hash/lru_cache_shared.cc
src/hash/lru_cache_shared.h

index 18ecae99ea4599edda640a293a01b996aa3d3fdd..5b50b33d070b2bbaa16f1b5563312620e045587b 100644 (file)
@@ -32,5 +32,6 @@ const PegInfo lru_cache_shared_peg_names[] =
     { CountType::SUM, "find_misses", "lru cache did not find entry in cache" },
     { CountType::SUM, "reload_prunes", "lru cache pruned entry for lower memcap during reload" },
     { CountType::SUM, "removes", "lru cache found entry and removed it" },
+    { CountType::SUM, "replaced", "lru cache found entry and replaced it" },
     { CountType::END, nullptr, nullptr },
 };
index 32bd2167ac88fa1870e46fd68b2dcef435bdfd40..024dbc4fb7dc034a1d40c0f6d6a2283536ce4b6c 100644 (file)
@@ -45,6 +45,7 @@ struct LruCacheSharedStats
     PegCount find_misses = 0;   // did not find entry in cache
     PegCount reload_prunes = 0; // when an old entry is removed due to lower memcap during reload
     PegCount removes = 0;       // found entry and removed it
+    PegCount replaced = 0;      // found entry and replaced it
 };
 
 template<typename Key, typename Value, typename Hash>
@@ -74,8 +75,8 @@ public:
     // Same as operator[]; additionally, sets the boolean if a new entry is created.
     Data find_else_create(const Key& key, bool* new_data);
 
-    // Returns true if found, takes a ref to a user managed entry
-    bool find_else_insert(const Key& key, std::shared_ptr<Value>& data);
+    // Returns true if found or replaced, takes a ref to a user managed entry
+    bool find_else_insert(const Key& key, std::shared_ptr<Value>& data, bool replace = false);
 
     // Return all data from the LruCache in order (most recently used to least)
     std::vector<std::pair<Key, Data> > get_all_data();
@@ -275,7 +276,7 @@ find_else_create(const Key& key, bool* new_data)
 
 template<typename Key, typename Value, typename Hash>
 bool LruCacheShared<Key, Value, Hash>::
-find_else_insert(const Key& key, std::shared_ptr<Value>& data)
+find_else_insert(const Key& key, std::shared_ptr<Value>& data, bool replace)
 {
     LruMapIter map_iter;
 
@@ -286,6 +287,13 @@ find_else_insert(const Key& key, std::shared_ptr<Value>& data)
     if (map_iter != map.end())
     {
         stats.find_hits++;
+        if (replace)
+        {
+            // Explicitly calling the reset so its more clear that destructor could be called for the object
+            map_iter->second->second.reset();
+            map_iter->second->second = data; 
+            stats.replaced++;
+        }
         list.splice(list.begin(), list, map_iter->second); // update LRU
         return true;
     }