PegCount replaced = 0; // found entry and replaced it
};
-template<typename Key, typename Value, typename Hash>
+template<typename Key, typename Value, typename Hash, typename Eq = std::equal_to<Key>>
class LruCacheShared
{
public:
protected:
using LruList = std::list<std::pair<Key, Data>>;
using LruListIter = typename LruList::iterator;
- using LruMap = std::unordered_map<Key, LruListIter, Hash>;
+ using LruMap = std::unordered_map<Key, LruListIter, Hash, Eq>;
using LruMapIter = typename LruMap::iterator;
static constexpr size_t mem_chunk = sizeof(Data) + sizeof(Value);
}
};
-template<typename Key, typename Value, typename Hash>
-bool LruCacheShared<Key, Value, Hash>::set_max_size(size_t newsize)
+template<typename Key, typename Value, typename Hash, typename Eq>
+bool LruCacheShared<Key, Value, Hash, Eq>::set_max_size(size_t newsize)
{
if (newsize == 0)
return false; // Not allowed to set size to zero.
return true;
}
-template<typename Key, typename Value, typename Hash>
-std::shared_ptr<Value> LruCacheShared<Key, Value, Hash>::find(const Key& key)
+template<typename Key, typename Value, typename Hash, typename Eq>
+std::shared_ptr<Value> LruCacheShared<Key, Value, Hash, Eq>::find(const Key& key)
{
LruMapIter map_iter;
std::lock_guard<std::mutex> cache_lock(cache_mutex);
return map_iter->second->second;
}
-template<typename Key, typename Value, typename Hash>
-std::shared_ptr<Value> LruCacheShared<Key, Value, Hash>::operator[](const Key& key)
+template<typename Key, typename Value, typename Hash, typename Eq>
+std::shared_ptr<Value> LruCacheShared<Key, Value, Hash, Eq>::operator[](const Key& key)
{
return find_else_create(key, nullptr);
}
-template<typename Key, typename Value, typename Hash>
-std::shared_ptr<Value> LruCacheShared<Key, Value, Hash>::
+template<typename Key, typename Value, typename Hash, typename Eq>
+std::shared_ptr<Value> LruCacheShared<Key, Value, Hash, Eq>::
find_else_create(const Key& key, bool* new_data)
{
LruMapIter map_iter;
return data;
}
-template<typename Key, typename Value, typename Hash>
-bool LruCacheShared<Key, Value, Hash>::
+template<typename Key, typename Value, typename Hash, typename Eq>
+bool LruCacheShared<Key, Value, Hash, Eq>::
find_else_insert(const Key& key, std::shared_ptr<Value>& data, bool replace)
{
LruMapIter map_iter;
return false;
}
-template<typename Key, typename Value, typename Hash>
+template<typename Key, typename Value, typename Hash, typename Eq>
std::vector< std::pair<Key, std::shared_ptr<Value>> >
-LruCacheShared<Key, Value, Hash>::get_all_data()
+LruCacheShared<Key, Value, Hash, Eq>::get_all_data()
{
std::vector<std::pair<Key, Data> > vec;
std::lock_guard<std::mutex> cache_lock(cache_mutex);
return vec;
}
-template<typename Key, typename Value, typename Hash>
-bool LruCacheShared<Key, Value, Hash>::remove(const Key& key)
+template<typename Key, typename Value, typename Hash, typename Eq>
+bool LruCacheShared<Key, Value, Hash, Eq>::remove(const Key& key)
{
LruMapIter map_iter;
return true;
}
-template<typename Key, typename Value, typename Hash>
-bool LruCacheShared<Key, Value, Hash>::remove(const Key& key, std::shared_ptr<Value>& data)
+template<typename Key, typename Value, typename Hash, typename Eq>
+bool LruCacheShared<Key, Value, Hash, Eq>::remove(const Key& key, std::shared_ptr<Value>& data)
{
LruMapIter map_iter;
#include "sfip/sf_ip.h"
#include "utils/stats.h"
-// Used to create hash of key for indexing into cache.
+// Used to create hash of key for indexing into cache.
+//
+// Note that both HashIp and IpEqualTo below ignore the IP family.
+// This means that 1.2.3.4 and ::ffff:0102:0304 will be treated
+// as equal (same host).
struct HashIp
{
size_t operator()(const snort::SfIp& ip) const
}
};
-template<typename Key, typename Value, typename Hash>
-class LruCacheSharedMemcap : public LruCacheShared<Key, Value, Hash>, public HostCacheInterface
+struct IpEqualTo
+{
+ bool operator()(const snort::SfIp &lhs, const snort::SfIp &rhs) const
+ {
+ return lhs.fast_eq6(rhs);
+ }
+};
+
+template<typename Key, typename Value, typename Hash, typename Eq = std::equal_to<Key>>
+class LruCacheSharedMemcap : public LruCacheShared<Key, Value, Hash, Eq>, public HostCacheInterface
{
public:
- using LruBase = LruCacheShared<Key, Value, Hash>;
+ using LruBase = LruCacheShared<Key, Value, Hash, Eq>;
using LruBase::cache_mutex;
using LruBase::current_size;
using LruBase::list;
LruCacheSharedMemcap(const LruCacheSharedMemcap& arg) = delete;
LruCacheSharedMemcap& operator=(const LruCacheSharedMemcap& arg) = delete;
- LruCacheSharedMemcap(const size_t initial_size) : LruCacheShared<Key, Value, Hash>(initial_size) {}
+ LruCacheSharedMemcap(const size_t initial_size) : LruCacheShared<Key, Value, Hash, Eq>(initial_size) {}
size_t mem_size() override
{
friend class TEST_host_cache_module_misc_Test; // for unit test
};
-typedef LruCacheSharedMemcap<snort::SfIp, snort::HostTracker, HashIp> HostCacheIp;
+typedef LruCacheSharedMemcap<snort::SfIp, snort::HostTracker, HashIp, IpEqualTo> HostCacheIp;
extern SO_PUBLIC HostCacheIp host_cache;