diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/libstate/global_state.cpp | 88 | ||||
-rw-r--r-- | src/libstate/global_state.h | 69 | ||||
-rw-r--r-- | src/libstate/info.txt | 6 | ||||
-rw-r--r-- | src/libstate/init.cpp | 5 | ||||
-rw-r--r-- | src/libstate/libstate.cpp | 44 | ||||
-rw-r--r-- | src/libstate/libstate.h | 20 |
6 files changed, 165 insertions, 67 deletions
diff --git a/src/libstate/global_state.cpp b/src/libstate/global_state.cpp new file mode 100644 index 000000000..0ffb49fdc --- /dev/null +++ b/src/libstate/global_state.cpp @@ -0,0 +1,88 @@ +/* +* Global State Management +* (C) 2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/global_state.h> +#include <botan/libstate.h> + +namespace Botan { + +/* +* @todo There should probably be a lock to avoid racy manipulation +* of the state among different threads +*/ + +namespace Global_State_Management { + +/* +* Botan's global state +*/ +namespace { + +Library_State* global_lib_state = 0; + +} + +/* +* Access the global state object +*/ +Library_State& global_state() + { + /* Lazy initialization. Botan still needs to be deinitialized later + on or memory might leak. + */ + if(!global_lib_state) + { + global_lib_state = new Library_State; + global_lib_state->initialize(true); + } + + return (*global_lib_state); + } + +/* +* Set a new global state object +*/ +void set_global_state(Library_State* new_state) + { + delete swap_global_state(new_state); + } + +/* +* Set a new global state object unless one already existed +*/ +bool set_global_state_unless_set(Library_State* new_state) + { + if(global_lib_state) + { + delete new_state; + return false; + } + else + delete swap_global_state(new_state); + } + +/* +* Swap two global state objects +*/ +Library_State* swap_global_state(Library_State* new_state) + { + Library_State* old_state = global_lib_state; + global_lib_state = new_state; + return old_state; + } + +/* +* Query if library is initialized +*/ +bool global_state_exists() + { + return (global_lib_state); + } + +} + +} diff --git a/src/libstate/global_state.h b/src/libstate/global_state.h new file mode 100644 index 000000000..486aed17e --- /dev/null +++ b/src/libstate/global_state.h @@ -0,0 +1,69 @@ +/* +* Global State Management +* (C) 2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_GLOBAL_STATE_H__ +#define BOTAN_GLOBAL_STATE_H__ + +#include <botan/build.h> + +namespace Botan { + +/* +* Forward declare to avoid recursive dependency between this header +* and libstate.h +*/ +class Library_State; + +/** +* Namespace for management of the global state +*/ +namespace Global_State_Management { + +/** +* Access the global library state +* @return reference to the global library state +*/ +BOTAN_DLL Library_State& global_state(); + +/** +* Set the global state object +* @param state the new global state to use +*/ +BOTAN_DLL void set_global_state(Library_State* state); + +/** +* Set the global state object unless it is already set +* @param state the new global state to use +* @return true if the state parameter is now being used as the global +* state, or false if one was already set, in which case the +* parameter was deleted immediately +*/ +BOTAN_DLL bool set_global_state_unless_set(Library_State* state); + +/** +* Swap the current state for another +* @param new_state the new state object to use +* @return previous state (or NULL if none) +*/ +BOTAN_DLL Library_State* swap_global_state(Library_State* new_state); + +/** +* Query if the library is currently initialized +* @return true iff the library is initialized +*/ +BOTAN_DLL bool global_state_exists(); + +} + +/* +* Insert into Botan ns for convenience/backwards compatability +*/ +using Global_State_Management::global_state; + +} + +#endif diff --git a/src/libstate/info.txt b/src/libstate/info.txt index 22095d1a3..94ae8101a 100644 --- a/src/libstate/info.txt +++ b/src/libstate/info.txt @@ -4,8 +4,9 @@ define LIBSTATE_MODULE <header:public> botan.h -libstate.h +global_state.h init.h +libstate.h look_pk.h lookup.h scan_name.h @@ -13,8 +14,9 @@ scan_name.h <source> get_enc.cpp -init.cpp global_rng.cpp +global_state.cpp +init.cpp libstate.cpp lookup.cpp policy.cpp diff --git a/src/libstate/init.cpp b/src/libstate/init.cpp index 386767b04..e2139f42e 100644 --- a/src/libstate/init.cpp +++ b/src/libstate/init.cpp @@ -8,6 +8,7 @@ #include <botan/init.h> #include <botan/parsing.h> #include <botan/libstate.h> +#include <botan/global_state.h> namespace Botan { @@ -53,7 +54,7 @@ void LibraryInitializer::initialize(const std::string& arg_string) allocators and so for, so global_state() has to be a valid reference before initialize() can be called. Yeah, gross. */ - set_global_state(new Library_State); + Global_State_Management::set_global_state(new Library_State); global_state().initialize(thread_safe); } @@ -69,7 +70,7 @@ void LibraryInitializer::initialize(const std::string& arg_string) */ void LibraryInitializer::deinitialize() { - set_global_state(0); + Global_State_Management::set_global_state(0); } } diff --git a/src/libstate/libstate.cpp b/src/libstate/libstate.cpp index 8d8634bab..a20fc76c6 100644 --- a/src/libstate/libstate.cpp +++ b/src/libstate/libstate.cpp @@ -56,50 +56,6 @@ namespace Botan { /* -* Botan's global state -*/ -namespace { - -Library_State* global_lib_state = 0; - -} - -/* -* Access the global state object -*/ -Library_State& global_state() - { - /* Lazy initialization. Botan still needs to be deinitialized later - on or memory might leak. - */ - if(!global_lib_state) - { - global_lib_state = new Library_State; - global_lib_state->initialize(true); - } - - return (*global_lib_state); - } - -/* -* Set a new global state object -*/ -void set_global_state(Library_State* new_state) - { - delete swap_global_state(new_state); - } - -/* -* Swap two global state objects -*/ -Library_State* swap_global_state(Library_State* new_state) - { - Library_State* old_state = global_lib_state; - global_lib_state = new_state; - return old_state; - } - -/* * Get a new mutex object */ Mutex* Library_State::get_mutex() const diff --git a/src/libstate/libstate.h b/src/libstate/libstate.h index eeb38287a..aa957c8c9 100644 --- a/src/libstate/libstate.h +++ b/src/libstate/libstate.h @@ -8,6 +8,7 @@ #ifndef BOTAN_LIB_STATE_H__ #define BOTAN_LIB_STATE_H__ +#include <botan/global_state.h> #include <botan/allocate.h> #include <botan/algo_factory.h> #include <botan/rng.h> @@ -140,25 +141,6 @@ class BOTAN_DLL Library_State Algorithm_Factory* m_algorithm_factory; }; -/** -* Access the global library state -* @return reference to the global library state -*/ -BOTAN_DLL Library_State& global_state(); - -/** -* Set the global state object -* @param state the new global state to use -*/ -BOTAN_DLL void set_global_state(Library_State* state); - -/** -* Swap the current state for another -* @param new_state the new state object to use -* @return previous state (or NULL if none) -*/ -BOTAN_DLL Library_State* swap_global_state(Library_State* new_state); - } #endif |