aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstate
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-03-21 23:30:14 +0000
committerlloyd <[email protected]>2010-03-21 23:30:14 +0000
commitf8a77dd91e8b3aed644c2c206ff9b8822fb3ae02 (patch)
tree315cbc118a8ba2afda674154e7c65e8900a00cb8 /src/libstate
parent572c106e868821da13447ca7349523ef4a90ec2c (diff)
parent634f3d27f7faad1dc558821382f71ecc2194637d (diff)
propagate from branch 'net.randombit.botan' (head 96d0a1885774b624812fd143d541c8bcda319217)
to branch 'net.randombit.botan.c++0x' (head e14368ab9d7976f3e111c6bc0adf24eebeb7c114)
Diffstat (limited to 'src/libstate')
-rw-r--r--src/libstate/info.txt2
-rw-r--r--src/libstate/init.cpp35
-rw-r--r--src/libstate/libstate.cpp106
-rw-r--r--src/libstate/libstate.h32
4 files changed, 44 insertions, 131 deletions
diff --git a/src/libstate/info.txt b/src/libstate/info.txt
index 0829506fb..aa5cbbe16 100644
--- a/src/libstate/info.txt
+++ b/src/libstate/info.txt
@@ -33,8 +33,6 @@ hash
kdf
mac
mode_pad
-mutex
-noop_mutex
pk_pad
pubkey
rng
diff --git a/src/libstate/init.cpp b/src/libstate/init.cpp
index b908de6c7..0d9a2420c 100644
--- a/src/libstate/init.cpp
+++ b/src/libstate/init.cpp
@@ -1,12 +1,11 @@
/**
* Default Initialization Function
-* (C) 1999-2007 Jack Lloyd
+* (C) 1999-2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#include <botan/init.h>
-#include <botan/parsing.h>
#include <botan/libstate.h>
namespace Botan {
@@ -14,36 +13,8 @@ namespace Botan {
/*
* Library Initialization
*/
-void LibraryInitializer::initialize(const std::string& arg_string)
+void LibraryInitializer::initialize(const std::string&)
{
- bool thread_safe = false;
-
- const std::vector<std::string> arg_list = split_on(arg_string, ' ');
- for(u32bit j = 0; j != arg_list.size(); ++j)
- {
- if(arg_list[j].size() == 0)
- continue;
-
- std::string name, value;
-
- if(arg_list[j].find('=') == std::string::npos)
- {
- name = arg_list[j];
- value = "true";
- }
- else
- {
- std::vector<std::string> name_and_value = split_on(arg_list[j], '=');
- name = name_and_value[0];
- value = name_and_value[1];
- }
-
- bool is_on =
- (value == "1" || value == "true" || value == "yes" || value == "on");
-
- if(name == "thread_safe")
- thread_safe = is_on;
- }
try
{
@@ -55,7 +26,7 @@ void LibraryInitializer::initialize(const std::string& arg_string)
*/
set_global_state(new Library_State);
- global_state().initialize(thread_safe);
+ global_state().initialize();
}
catch(...)
{
diff --git a/src/libstate/libstate.cpp b/src/libstate/libstate.cpp
index c62bd08db..943a7c2e6 100644
--- a/src/libstate/libstate.cpp
+++ b/src/libstate/libstate.cpp
@@ -6,13 +6,9 @@
*/
#include <botan/libstate.h>
-#include <botan/charset.h>
-#include <botan/engine.h>
#include <botan/init.h>
#include <botan/internal/defalloc.h>
#include <botan/internal/default_engine.h>
-#include <botan/internal/mutex.h>
-#include <botan/internal/mux_noop.h>
#include <botan/internal/stl_util.h>
#include <botan/internal/mlock.h>
#include <algorithm>
@@ -21,14 +17,6 @@
#include <botan/selftest.h>
#endif
-#if defined(BOTAN_HAS_MUTEX_PTHREAD)
- #include <botan/internal/mux_pthr.h>
-#elif defined(BOTAN_HAS_MUTEX_WIN32)
- #include <botan/internal/mux_win32.h>
-#elif defined(BOTAN_HAS_MUTEX_QT)
- #include <botan/internal/mux_qt.h>
-#endif
-
#if defined(BOTAN_HAS_ALLOC_MMAP)
#include <botan/internal/mmap_mem.h>
#endif
@@ -95,25 +83,17 @@ void set_global_state(Library_State* new_state)
*/
Library_State* swap_global_state(Library_State* new_state)
{
- Library_State* old_state = global_lib_state;
+ auto old_state = global_lib_state;
global_lib_state = new_state;
return old_state;
}
/*
-* Get a new mutex object
-*/
-Mutex* Library_State::get_mutex() const
- {
- return mutex_factory->make();
- }
-
-/*
* Get an allocator by its name
*/
-Allocator* Library_State::get_allocator(const std::string& type) const
+Allocator* Library_State::get_allocator(const std::string& type)
{
- Mutex_Holder lock(allocator_lock);
+ std::lock_guard<std::mutex> lock(allocator_lock);
if(type != "")
return search_map<std::string, Allocator*>(alloc_factory, type, 0);
@@ -133,7 +113,7 @@ Allocator* Library_State::get_allocator(const std::string& type) const
*/
void Library_State::add_allocator(Allocator* allocator)
{
- Mutex_Holder lock(allocator_lock);
+ std::lock_guard<std::mutex> lock(allocator_lock);
allocator->init();
@@ -146,11 +126,11 @@ void Library_State::add_allocator(Allocator* allocator)
*/
void Library_State::set_default_allocator(const std::string& type)
{
- Mutex_Holder lock(allocator_lock);
-
if(type == "")
return;
+ std::lock_guard<std::mutex> lock(allocator_lock);
+
default_allocator_name = type;
cached_default_allocator = 0;
}
@@ -159,9 +139,9 @@ void Library_State::set_default_allocator(const std::string& type)
* Get a configuration value
*/
std::string Library_State::get(const std::string& section,
- const std::string& key) const
+ const std::string& key)
{
- Mutex_Holder lock(config_lock);
+ std::lock_guard<std::mutex> lock(config_lock);
return search_map<std::string, std::string>(config,
section + "/" + key, "");
@@ -171,9 +151,9 @@ std::string Library_State::get(const std::string& section,
* See if a particular option has been set
*/
bool Library_State::is_set(const std::string& section,
- const std::string& key) const
+ const std::string& key)
{
- Mutex_Holder lock(config_lock);
+ std::lock_guard<std::mutex> lock(config_lock);
return search_map(config, section + "/" + key, false, true);
}
@@ -184,12 +164,11 @@ bool Library_State::is_set(const std::string& section,
void Library_State::set(const std::string& section, const std::string& key,
const std::string& value, bool overwrite)
{
- Mutex_Holder lock(config_lock);
+ std::lock_guard<std::mutex> lock(config_lock);
std::string full_key = section + "/" + key;
- std::map<std::string, std::string>::const_iterator i =
- config.find(full_key);
+ auto i = config.find(full_key);
if(overwrite || i == config.end() || i->second == "")
config[full_key] = value;
@@ -206,7 +185,7 @@ void Library_State::add_alias(const std::string& key, const std::string& value)
/*
* Dereference an alias to a fixed name
*/
-std::string Library_State::deref_alias(const std::string& key) const
+std::string Library_State::deref_alias(const std::string& key)
{
std::string result = key;
while(is_set("alias", result))
@@ -229,7 +208,7 @@ Algorithm_Factory& Library_State::algorithm_factory() const
*/
RandomNumberGenerator& Library_State::global_rng()
{
- Mutex_Holder lock(global_rng_lock);
+ std::lock_guard<std::mutex> lock(global_rng_lock);
if(!global_rng_ptr)
global_rng_ptr = make_global_rng(algorithm_factory(),
@@ -241,75 +220,53 @@ RandomNumberGenerator& Library_State::global_rng()
/*
* Load a set of modules
*/
-void Library_State::initialize(bool thread_safe)
+void Library_State::initialize()
{
- if(mutex_factory)
+ if(m_algorithm_factory)
throw Invalid_State("Library_State has already been initialized");
- if(!thread_safe)
- {
- mutex_factory = new Noop_Mutex_Factory;
- }
- else
- {
-#if defined(BOTAN_HAS_MUTEX_PTHREAD)
- mutex_factory = new Pthread_Mutex_Factory;
-#elif defined(BOTAN_HAS_MUTEX_WIN32)
- mutex_factory = new Win32_Mutex_Factory;
-#elif defined(BOTAN_HAS_MUTEX_QT)
- mutex_factory Qt_Mutex_Factory;
-#else
- throw Invalid_State("Could not find a thread-safe mutex object to use");
-#endif
- }
-
- allocator_lock = get_mutex();
- config_lock = get_mutex();
- global_rng_lock = get_mutex();
-
- global_rng_ptr = 0;
-
cached_default_allocator = 0;
default_allocator_name = has_mlock() ? "locking" : "malloc";
add_allocator(new Malloc_Allocator);
- add_allocator(new Locking_Allocator(get_mutex()));
+ add_allocator(new Locking_Allocator);
#if defined(BOTAN_HAS_ALLOC_MMAP)
- add_allocator(new MemoryMapping_Allocator(get_mutex()));
+ add_allocator(new MemoryMapping_Allocator);
#endif
load_default_config();
- std::vector<Engine*> engines;
+ std::vector<Engine*> engines = {
#if defined(BOTAN_HAS_ENGINE_GNU_MP)
- engines.push_back(new GMP_Engine);
+ new GMP_Engine,
#endif
#if defined(BOTAN_HAS_ENGINE_OPENSSL)
- engines.push_back(new OpenSSL_Engine);
+ new OpenSSL_Engine,
#endif
#if defined(BOTAN_HAS_ENGINE_AES_ISA)
- engines.push_back(new AES_ISA_Engine);
+ new AES_ISA_Engine,
#endif
#if defined(BOTAN_HAS_ENGINE_SIMD)
- engines.push_back(new SIMD_Engine);
+ new SIMD_Engine,
#endif
#if defined(BOTAN_HAS_ENGINE_AMD64_ASSEMBLER)
- engines.push_back(new AMD64_Assembler_Engine);
+ new AMD64_Assembler_Engine,
#endif
#if defined(BOTAN_HAS_ENGINE_IA32_ASSEMBLER)
- engines.push_back(new IA32_Assembler_Engine);
+ new IA32_Assembler_Engine,
#endif
- engines.push_back(new Default_Engine);
+ new Default_Engine
+ };
- m_algorithm_factory = new Algorithm_Factory(engines, *mutex_factory);
+ m_algorithm_factory = new Algorithm_Factory(engines);
#if defined(BOTAN_HAS_SELFTESTS)
confirm_startup_self_tests(algorithm_factory());
@@ -321,8 +278,6 @@ void Library_State::initialize(bool thread_safe)
*/
Library_State::Library_State()
{
- mutex_factory = 0;
- allocator_lock = config_lock = 0;
cached_default_allocator = 0;
m_algorithm_factory = 0;
}
@@ -333,6 +288,7 @@ Library_State::Library_State()
Library_State::~Library_State()
{
delete m_algorithm_factory;
+ m_algorithm_factory = 0;
cached_default_allocator = 0;
@@ -341,10 +297,6 @@ Library_State::~Library_State()
allocators[j]->destroy();
delete allocators[j];
}
-
- delete allocator_lock;
- delete mutex_factory;
- delete config_lock;
}
}
diff --git a/src/libstate/libstate.h b/src/libstate/libstate.h
index 4674eca88..295fa1fa4 100644
--- a/src/libstate/libstate.h
+++ b/src/libstate/libstate.h
@@ -12,14 +12,13 @@
#include <botan/algo_factory.h>
#include <botan/rng.h>
+#include <mutex>
#include <string>
#include <vector>
#include <map>
namespace Botan {
-class Mutex;
-
/*
* Global State Container Base
*/
@@ -29,7 +28,10 @@ class BOTAN_DLL Library_State
Library_State();
~Library_State();
- void initialize(bool thread_safe);
+ Library_State(const Library_State&) = delete;
+ Library_State& operator=(const Library_State&) = delete;
+
+ void initialize();
/**
* @return the global Algorithm_Factory
@@ -45,7 +47,7 @@ class BOTAN_DLL Library_State
* @param name the name of the allocator
* @return allocator matching this name, or NULL
*/
- Allocator* get_allocator(const std::string& name = "") const;
+ Allocator* get_allocator(const std::string& name = "");
/**
* Add a new allocator to the list of available ones
@@ -66,7 +68,7 @@ class BOTAN_DLL Library_State
* @result the value of the parameter
*/
std::string get(const std::string& section,
- const std::string& key) const;
+ const std::string& key);
/**
* Check whether a certain parameter is set
@@ -77,7 +79,7 @@ class BOTAN_DLL Library_State
* false otherwise
*/
bool is_set(const std::string& section,
- const std::string& key) const;
+ const std::string& key);
/**
* Set a configuration parameter.
@@ -105,30 +107,20 @@ class BOTAN_DLL Library_State
* @param alias the alias to resolve.
* @return what the alias stands for
*/
- std::string deref_alias(const std::string& alias) const;
-
- /**
- * @return a newly created Mutex (free with delete)
- */
- Mutex* get_mutex() const;
+ std::string deref_alias(const std::string&);
private:
static RandomNumberGenerator* make_global_rng(Algorithm_Factory& af,
Mutex* mutex);
void load_default_config();
- Library_State(const Library_State&) {}
- Library_State& operator=(const Library_State&) { return (*this); }
-
- class Mutex_Factory* mutex_factory;
-
- Mutex* global_rng_lock;
+ std::mutex global_rng_lock;
RandomNumberGenerator* global_rng_ptr;
- Mutex* config_lock;
+ std::mutex config_lock;
std::map<std::string, std::string> config;
- Mutex* allocator_lock;
+ std::mutex allocator_lock;
std::string default_allocator_name;
std::map<std::string, Allocator*> alloc_factory;
mutable Allocator* cached_default_allocator;