From 6dcb485864400d4329103945c044c4a5f3cab528 Mon Sep 17 00:00:00 2001 From: lloyd Date: Wed, 12 Nov 2008 21:59:27 +0000 Subject: In Algorithm_Factory, create the Algorithm_Cache<> objects dynamically so that algo_cache.h does not have to be visible in the source of all callers who include libstate.h/algo_factory.h --- src/algo_factory/algo_factory.cpp | 59 ++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 25 deletions(-) (limited to 'src/algo_factory/algo_factory.cpp') diff --git a/src/algo_factory/algo_factory.cpp b/src/algo_factory/algo_factory.cpp index 4ff5bb2b6..e966aee78 100644 --- a/src/algo_factory/algo_factory.cpp +++ b/src/algo_factory/algo_factory.cpp @@ -4,6 +4,7 @@ Algorithm Factory */ #include +#include #include #include #include @@ -53,20 +54,23 @@ const T* factory_prototype(const std::string& algo_spec, const std::string& provider, const std::vector& engines, Algorithm_Factory& af, - Algorithm_Cache& cache) + Algorithm_Cache* cache) { - if(const T* cache_hit = cache.get(algo_spec, provider)) + if(const T* cache_hit = cache->get(algo_spec, provider)) return cache_hit; SCAN_Name scan_name(algo_spec); for(u32bit i = 0; i != engines.size(); ++i) { - T* impl = engine_get_algo(engines[i], scan_name, af); - if(impl) - cache.add(impl, algo_spec, engines[i]->provider_name()); + if(provider == "" || engines[i]->provider_name() == provider) + { + T* impl = engine_get_algo(engines[i], scan_name, af); + if(impl) + cache->add(impl, algo_spec, engines[i]->provider_name()); + } } - return cache.get(algo_spec, provider); + return cache->get(algo_spec, provider); } } @@ -75,13 +79,14 @@ const T* factory_prototype(const std::string& algo_spec, * Setup caches */ Algorithm_Factory::Algorithm_Factory(const std::vector& engines_in, - Mutex_Factory& mf) : - engines(engines_in), - block_cipher_cache(mf.make()), - stream_cipher_cache(mf.make()), - hash_cache(mf.make()), - mac_cache(mf.make()) + Mutex_Factory& mf) { + engines = engines_in; + + block_cipher_cache = new Algorithm_Cache(mf.make()); + stream_cipher_cache = new Algorithm_Cache(mf.make()); + hash_cache = new Algorithm_Cache(mf.make()); + mac_cache = new Algorithm_Cache(mf.make()); } /** @@ -90,7 +95,11 @@ Algorithm_Factory::Algorithm_Factory(const std::vector& engines_in, Algorithm_Factory::~Algorithm_Factory() { std::for_each(engines.begin(), engines.end(), del_fun()); - engines.clear(); + + delete block_cipher_cache; + delete stream_cipher_cache; + delete hash_cache; + delete mac_cache; } /** @@ -100,13 +109,13 @@ void Algorithm_Factory::set_preferred_provider(const std::string& algo_spec, const std::string& provider) { if(prototype_block_cipher(algo_spec)) - block_cipher_cache.set_preferred_provider(algo_spec, provider); + block_cipher_cache->set_preferred_provider(algo_spec, provider); else if(prototype_stream_cipher(algo_spec)) - stream_cipher_cache.set_preferred_provider(algo_spec, provider); + stream_cipher_cache->set_preferred_provider(algo_spec, provider); else if(prototype_hash_function(algo_spec)) - hash_cache.set_preferred_provider(algo_spec, provider); + hash_cache->set_preferred_provider(algo_spec, provider); else if(prototype_mac(algo_spec)) - mac_cache.set_preferred_provider(algo_spec, provider); + mac_cache->set_preferred_provider(algo_spec, provider); } /** @@ -132,13 +141,13 @@ Algorithm_Factory::providers_of(const std::string& algo_spec) */ if(prototype_block_cipher(algo_spec)) - return block_cipher_cache.providers_of(algo_spec); + return block_cipher_cache->providers_of(algo_spec); else if(prototype_stream_cipher(algo_spec)) - return stream_cipher_cache.providers_of(algo_spec); + return stream_cipher_cache->providers_of(algo_spec); else if(prototype_hash_function(algo_spec)) - return hash_cache.providers_of(algo_spec); + return hash_cache->providers_of(algo_spec); else if(prototype_mac(algo_spec)) - return mac_cache.providers_of(algo_spec); + return mac_cache->providers_of(algo_spec); else return std::vector(); } @@ -241,7 +250,7 @@ Algorithm_Factory::make_mac(const std::string& algo_spec, void Algorithm_Factory::add_block_cipher(BlockCipher* block_cipher, const std::string& provider) { - block_cipher_cache.add(block_cipher, block_cipher->name(), provider); + block_cipher_cache->add(block_cipher, block_cipher->name(), provider); } /** @@ -250,7 +259,7 @@ void Algorithm_Factory::add_block_cipher(BlockCipher* block_cipher, void Algorithm_Factory::add_stream_cipher(StreamCipher* stream_cipher, const std::string& provider) { - stream_cipher_cache.add(stream_cipher, stream_cipher->name(), provider); + stream_cipher_cache->add(stream_cipher, stream_cipher->name(), provider); } /** @@ -259,7 +268,7 @@ void Algorithm_Factory::add_stream_cipher(StreamCipher* stream_cipher, void Algorithm_Factory::add_hash_function(HashFunction* hash, const std::string& provider) { - hash_cache.add(hash, hash->name(), provider); + hash_cache->add(hash, hash->name(), provider); } /** @@ -268,7 +277,7 @@ void Algorithm_Factory::add_hash_function(HashFunction* hash, void Algorithm_Factory::add_mac(MessageAuthenticationCode* mac, const std::string& provider) { - mac_cache.add(mac, mac->name(), provider); + mac_cache->add(mac, mac->name(), provider); } } -- cgit v1.2.3