diff options
Diffstat (limited to 'src/libstate/libstate.cpp')
-rw-r--r-- | src/libstate/libstate.cpp | 89 |
1 files changed, 23 insertions, 66 deletions
diff --git a/src/libstate/libstate.cpp b/src/libstate/libstate.cpp index a20fc76c6..0d7463306 100644 --- a/src/libstate/libstate.cpp +++ b/src/libstate/libstate.cpp @@ -11,8 +11,6 @@ #include <botan/cpuid.h> #include <botan/internal/defalloc.h> #include <botan/internal/core_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 +19,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 @@ -56,19 +46,11 @@ namespace Botan { /* -* 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); @@ -88,7 +70,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(); @@ -101,11 +83,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; } @@ -114,9 +96,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, ""); @@ -126,9 +108,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); } @@ -139,12 +121,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; @@ -161,7 +142,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)) @@ -184,7 +165,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(), @@ -196,46 +177,26 @@ RandomNumberGenerator& Library_State::global_rng() /* * Load a set of modules */ -void Library_State::initialize(bool thread_safe) +void Library_State::initialize() { CPUID::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(); - + 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(); - m_algorithm_factory = new Algorithm_Factory(*mutex_factory); + m_algorithm_factory = new Algorithm_Factory(); #if defined(BOTAN_HAS_ENGINE_GNU_MP) algorithm_factory().add_engine(new GMP_Engine); @@ -269,12 +230,9 @@ 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; - global_rng_lock = 0; global_rng_ptr = 0; } @@ -284,7 +242,11 @@ Library_State::Library_State() Library_State::~Library_State() { delete m_algorithm_factory; + m_algorithm_factory = 0; + delete global_rng_ptr; + global_rng_ptr = 0; + cached_default_allocator = 0; @@ -293,11 +255,6 @@ Library_State::~Library_State() allocators[j]->destroy(); delete allocators[j]; } - - delete global_rng_lock; - delete allocator_lock; - delete mutex_factory; - delete config_lock; } } |