aboutsummaryrefslogtreecommitdiffstats
path: root/src/algo_factory
diff options
context:
space:
mode:
authorlloyd <[email protected]>2009-12-08 15:29:22 +0000
committerlloyd <[email protected]>2009-12-08 15:29:22 +0000
commit457ce43934a4e51ead4d21e43013eef9d448d0e1 (patch)
treec40635ba737cf01f8e1e8c894422683222c07c4b /src/algo_factory
parentf63359f348d347bebee036b11d0c9641ee3d56d6 (diff)
parent9559e323cb631af4e154f5fd30ff2927748817ed (diff)
propagate from branch 'net.randombit.botan' (head 142a9359ba02d5dfcf3f2c9f99902f82ab41724e)
to branch 'net.randombit.botan.c++0x' (head 390a9abce0eb6ee24eeb3cd243b6dcaaa8944ad0)
Diffstat (limited to 'src/algo_factory')
-rw-r--r--src/algo_factory/algo_cache.h46
-rw-r--r--src/algo_factory/algo_factory.cpp14
-rw-r--r--src/algo_factory/algo_factory.h5
-rw-r--r--src/algo_factory/info.txt1
4 files changed, 28 insertions, 38 deletions
diff --git a/src/algo_factory/algo_cache.h b/src/algo_factory/algo_cache.h
index 08b25cd47..09bbc4b5a 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/mutex.h>
+#include <botan/types.h>
#include <botan/stl_util.h>
+#include <mutex>
#include <string>
#include <vector>
#include <map>
@@ -50,17 +51,12 @@ class Algorithm_Cache
*/
std::vector<std::string> providers_of(const std::string& algo_name);
- Algorithm_Cache(Mutex* m) : mutex(m) {}
~Algorithm_Cache();
private:
- typedef typename std::map<std::string, std::map<std::string, T*> >::iterator
- algorithms_iterator;
+ typename std::map<std::string, std::map<std::string, T*> >::const_iterator
+ find_algorithm(const std::string& algo_spec);
- typedef typename std::map<std::string, T*>::iterator provider_iterator;
-
- 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;
@@ -71,16 +67,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);
@@ -96,16 +91,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;
@@ -117,7 +112,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 u32bit prov_weight = static_provider_weight(prov_name);
@@ -148,7 +143,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;
@@ -166,15 +161,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())
{
@@ -193,7 +187,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;
}
@@ -204,11 +198,11 @@ void Algorithm_Cache<T>::set_preferred_provider(const std::string& algo_spec,
template<typename T>
Algorithm_Cache<T>::~Algorithm_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())
{
@@ -218,8 +212,6 @@ Algorithm_Cache<T>::~Algorithm_Cache()
++algo;
}
-
- delete mutex;
}
}
diff --git a/src/algo_factory/algo_factory.cpp b/src/algo_factory/algo_factory.cpp
index 22915d97c..0b8422bcb 100644
--- a/src/algo_factory/algo_factory.cpp
+++ b/src/algo_factory/algo_factory.cpp
@@ -84,15 +84,14 @@ const T* factory_prototype(const std::string& algo_spec,
/**
* Setup caches
*/
-Algorithm_Factory::Algorithm_Factory(const std::vector<Engine*>& engines_in,
- Mutex_Factory& mf)
+Algorithm_Factory::Algorithm_Factory(const std::vector<Engine*>& engines_in)
{
engines = engines_in;
- 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>();
}
/**
@@ -100,7 +99,8 @@ Algorithm_Factory::Algorithm_Factory(const std::vector<Engine*>& engines_in,
*/
Algorithm_Factory::~Algorithm_Factory()
{
- std::for_each(engines.begin(), engines.end(), del_fun<Engine>());
+ for(auto i = engines.begin(); i != engines.end(); ++i)
+ delete *i;
delete block_cipher_cache;
delete stream_cipher_cache;
diff --git a/src/algo_factory/algo_factory.h b/src/algo_factory/algo_factory.h
index 73e592013..1f4b577ee 100644
--- a/src/algo_factory/algo_factory.h
+++ b/src/algo_factory/algo_factory.h
@@ -8,7 +8,7 @@
#ifndef BOTAN_ALGORITHM_FACTORY_H__
#define BOTAN_ALGORITHM_FACTORY_H__
-#include <botan/mutex.h>
+#include <botan/types.h>
#include <string>
#include <vector>
@@ -37,8 +37,7 @@ class BOTAN_DLL Algorithm_Factory
* @param engines_in the list of engines to use
* @param mf a mutex factory
*/
- Algorithm_Factory(const std::vector<Engine*>& engines_in,
- Mutex_Factory& mf);
+ Algorithm_Factory(const std::vector<Engine*>& engines_in);
/**
* Destructor
diff --git a/src/algo_factory/info.txt b/src/algo_factory/info.txt
index 4b25c7fc5..afd350bdb 100644
--- a/src/algo_factory/info.txt
+++ b/src/algo_factory/info.txt
@@ -14,6 +14,5 @@ block
engine
hash
mac
-mutex
stream
</requires>