From: Steve Chew (stechew) Date: Fri, 12 Jun 2020 03:17:27 +0000 (+0000) Subject: Merge pull request #2257 in SNORT/snort3 from ~ANTOROZC/snort3:duapalme_replace_cache... X-Git-Tag: 3.0.1-5~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=de950760f6e3bde062e29fe26be6c1b05fd79b6b;p=thirdparty%2Fsnort3.git Merge pull request #2257 in SNORT/snort3 from ~ANTOROZC/snort3:duapalme_replace_cache to master Squashed commit of the following: commit 90cf5e3a304b16b6494ed496c2f6d326dad0a381 Author: Duane Palmer Date: Thu Jun 11 15:34:47 2020 -0500 lru_cache_shared: replace the cache entry if found --- diff --git a/src/hash/lru_cache_shared.cc b/src/hash/lru_cache_shared.cc index 18ecae99e..5b50b33d0 100644 --- a/src/hash/lru_cache_shared.cc +++ b/src/hash/lru_cache_shared.cc @@ -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 }, }; diff --git a/src/hash/lru_cache_shared.h b/src/hash/lru_cache_shared.h index 32bd2167a..024dbc4fb 100644 --- a/src/hash/lru_cache_shared.h +++ b/src/hash/lru_cache_shared.h @@ -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 @@ -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& 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& data, bool replace = false); // Return all data from the LruCache in order (most recently used to least) std::vector > get_all_data(); @@ -275,7 +276,7 @@ find_else_create(const Key& key, bool* new_data) template bool LruCacheShared:: -find_else_insert(const Key& key, std::shared_ptr& data) +find_else_insert(const Key& key, std::shared_ptr& data, bool replace) { LruMapIter map_iter; @@ -286,6 +287,13 @@ find_else_insert(const Key& key, std::shared_ptr& 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; }