aboutsummaryrefslogtreecommitdiffstats
path: root/src/algo_factory
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-10-13 03:25:16 +0000
committerlloyd <[email protected]>2010-10-13 03:25:16 +0000
commitba142ed2eaa21445d49cbbc8ae3b3d4dc625b9d7 (patch)
treebe1a9fa07d0ea26fd7713bb52205f7a3bcc3e907 /src/algo_factory
parent406d4f8556d41083bfa551e7a777b0cb02925b6c (diff)
parent5913bf42b4c32e43d0db11bf9299130f3b0b62a4 (diff)
propagate from branch 'net.randombit.botan' (head 2898d79f992f27a328a3e41d34b46eb1052da0de)
to branch 'net.randombit.botan.c++0x' (head 6cba76268fd69a73195760c021b7f881b8a6552c)
Diffstat (limited to 'src/algo_factory')
-rw-r--r--src/algo_factory/algo_cache.h50
-rw-r--r--src/algo_factory/algo_factory.cpp13
-rw-r--r--src/algo_factory/algo_factory.h3
-rw-r--r--src/algo_factory/info.txt1
4 files changed, 28 insertions, 39 deletions
diff --git a/src/algo_factory/algo_cache.h b/src/algo_factory/algo_cache.h
index 0d891ad3f..279ba9389 100644
--- a/src/algo_factory/algo_cache.h
+++ b/src/algo_factory/algo_cache.h
@@ -8,8 +8,9 @@
#ifndef BOTAN_ALGORITHM_CACHE_TEMPLATE_H__
#define BOTAN_ALGORITHM_CACHE_TEMPLATE_H__
-#include <botan/internal/mutex.h>
+#include <botan/types.h>
#include <botan/internal/stl_util.h>
+#include <mutex>
#include <string>
#include <vector>
#include <map>
@@ -67,21 +68,12 @@ class Algorithm_Cache
*/
void clear_cache();
- /**
- * Constructor
- * @param m a mutex to serialize internal access
- */
- Algorithm_Cache(Mutex* m) : mutex(m) {}
- ~Algorithm_Cache() { clear_cache(); delete mutex; }
+ ~Algorithm_Cache() { clear_cache(); }
private:
- typedef typename std::map<std::string, std::map<std::string, T*> >::iterator
- algorithms_iterator;
-
- typedef typename std::map<std::string, T*>::iterator provider_iterator;
+ typename std::map<std::string, std::map<std::string, T*> >::const_iterator
+ find_algorithm(const std::string& algo_spec);
- algorithms_iterator find_algorithm(const std::string& algo_spec);
-
- Mutex* mutex;
+ std::mutex mutex;
std::map<std::string, std::string> aliases;
std::map<std::string, std::string> pref_providers;
std::map<std::string, std::map<std::string, T*> > algorithms;
@@ -92,16 +84,15 @@ class Algorithm_Cache
* Assumes object lock is held
*/
template<typename T>
-typename Algorithm_Cache<T>::algorithms_iterator
+typename std::map<std::string, std::map<std::string, T*> >::const_iterator
Algorithm_Cache<T>::find_algorithm(const std::string& algo_spec)
{
- algorithms_iterator algo = algorithms.find(algo_spec);
+ auto algo = algorithms.find(algo_spec);
// Not found? Check if a known alias
if(algo == algorithms.end())
{
- std::map<std::string, std::string>::const_iterator alias =
- aliases.find(algo_spec);
+ auto alias = aliases.find(algo_spec);
if(alias != aliases.end())
algo = algorithms.find(alias->second);
@@ -117,16 +108,16 @@ template<typename T>
const T* Algorithm_Cache<T>::get(const std::string& algo_spec,
const std::string& requested_provider)
{
- Mutex_Holder lock(mutex);
+ std::lock_guard<std::mutex> lock(mutex);
- algorithms_iterator algo = find_algorithm(algo_spec);
+ auto algo = find_algorithm(algo_spec);
if(algo == algorithms.end()) // algo not found at all (no providers)
return 0;
// If a provider is requested specifically, return it or fail entirely
if(requested_provider != "")
{
- provider_iterator prov = algo->second.find(requested_provider);
+ auto prov = algo->second.find(requested_provider);
if(prov != algo->second.end())
return prov->second;
return 0;
@@ -138,7 +129,7 @@ const T* Algorithm_Cache<T>::get(const std::string& algo_spec,
const std::string pref_provider = search_map(pref_providers, algo_spec);
- for(provider_iterator i = algo->second.begin(); i != algo->second.end(); ++i)
+ for(auto i = algo->second.begin(); i != algo->second.end(); ++i)
{
const std::string prov_name = i->first;
const size_t prov_weight = static_provider_weight(prov_name);
@@ -169,7 +160,7 @@ void Algorithm_Cache<T>::add(T* algo,
if(!algo)
return;
- Mutex_Holder lock(mutex);
+ std::lock_guard<std::mutex> lock(mutex);
delete algorithms[algo->name()][provider];
algorithms[algo->name()][provider] = algo;
@@ -187,15 +178,14 @@ void Algorithm_Cache<T>::add(T* algo,
template<typename T> std::vector<std::string>
Algorithm_Cache<T>::providers_of(const std::string& algo_name)
{
- Mutex_Holder lock(mutex);
+ std::lock_guard<std::mutex> lock(mutex);
std::vector<std::string> providers;
- algorithms_iterator algo = find_algorithm(algo_name);
-
+ auto algo = find_algorithm(algo_name);
if(algo != algorithms.end())
{
- provider_iterator provider = algo->second.begin();
+ auto provider = algo->second.begin();
while(provider != algo->second.end())
{
@@ -214,7 +204,7 @@ template<typename T>
void Algorithm_Cache<T>::set_preferred_provider(const std::string& algo_spec,
const std::string& provider)
{
- Mutex_Holder lock(mutex);
+ std::lock_guard<std::mutex> lock(mutex);
pref_providers[algo_spec] = provider;
}
@@ -225,11 +215,11 @@ void Algorithm_Cache<T>::set_preferred_provider(const std::string& algo_spec,
template<typename T>
void Algorithm_Cache<T>::clear_cache()
{
- algorithms_iterator algo = algorithms.begin();
+ auto algo = algorithms.begin();
while(algo != algorithms.end())
{
- provider_iterator provider = algo->second.begin();
+ auto provider = algo->second.begin();
while(provider != algo->second.end())
{
diff --git a/src/algo_factory/algo_factory.cpp b/src/algo_factory/algo_factory.cpp
index ba4a435d7..9859a3bca 100644
--- a/src/algo_factory/algo_factory.cpp
+++ b/src/algo_factory/algo_factory.cpp
@@ -87,12 +87,12 @@ const T* factory_prototype(const std::string& algo_spec,
/*
* Setup caches
*/
-Algorithm_Factory::Algorithm_Factory(Mutex_Factory& mf)
+Algorithm_Factory::Algorithm_Factory()
{
- block_cipher_cache = new Algorithm_Cache<BlockCipher>(mf.make());
- stream_cipher_cache = new Algorithm_Cache<StreamCipher>(mf.make());
- hash_cache = new Algorithm_Cache<HashFunction>(mf.make());
- mac_cache = new Algorithm_Cache<MessageAuthenticationCode>(mf.make());
+ block_cipher_cache = new Algorithm_Cache<BlockCipher>();
+ stream_cipher_cache = new Algorithm_Cache<StreamCipher>();
+ hash_cache = new Algorithm_Cache<HashFunction>();
+ mac_cache = new Algorithm_Cache<MessageAuthenticationCode>();
}
/*
@@ -105,7 +105,8 @@ Algorithm_Factory::~Algorithm_Factory()
delete hash_cache;
delete mac_cache;
- std::for_each(engines.begin(), engines.end(), del_fun<Engine>());
+ for(auto i = engines.begin(); i != engines.end(); ++i)
+ delete *i;
}
void Algorithm_Factory::clear_caches()
diff --git a/src/algo_factory/algo_factory.h b/src/algo_factory/algo_factory.h
index 1c865b470..e8714bd6c 100644
--- a/src/algo_factory/algo_factory.h
+++ b/src/algo_factory/algo_factory.h
@@ -25,7 +25,6 @@ class MessageAuthenticationCode;
template<typename T> class Algorithm_Cache;
class Engine;
-class Mutex_Factory;
/**
* Algorithm Factory
@@ -37,7 +36,7 @@ class BOTAN_DLL Algorithm_Factory
* Constructor
* @param mf a mutex factory
*/
- Algorithm_Factory(Mutex_Factory& mf);
+ Algorithm_Factory();
/**
* Destructor
diff --git a/src/algo_factory/info.txt b/src/algo_factory/info.txt
index eae4b3934..236fdda0e 100644
--- a/src/algo_factory/info.txt
+++ b/src/algo_factory/info.txt
@@ -20,6 +20,5 @@ block
engine
hash
mac
-mutex
stream
</requires>