From 22718fa6526bf5c9923d0091d662cd2def75c605 Mon Sep 17 00:00:00 2001 From: lloyd Date: Fri, 6 Aug 2010 15:38:55 +0000 Subject: Move the functions that directly manipulate the global state singleton into global_state.{h,cpp}. Move all of the functions into a new namespace Global_State_Management, though exposing global_state() into the Botan namespace for compatability. Also add new functions global_state_exists and set_global_state_unless_set which may be helpful in certain tricky initialization scenarios (eg when an application using botan also uses a library which may or may not itself use botan). --- src/libstate/global_state.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++ src/libstate/global_state.h | 69 +++++++++++++++++++++++++++++++++ src/libstate/info.txt | 6 ++- src/libstate/init.cpp | 5 ++- src/libstate/libstate.cpp | 44 ---------------------- src/libstate/libstate.h | 20 +--------- 6 files changed, 165 insertions(+), 67 deletions(-) create mode 100644 src/libstate/global_state.cpp create mode 100644 src/libstate/global_state.h (limited to 'src') 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 +#include + +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 + +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 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 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 #include #include +#include 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 @@ -55,50 +55,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 */ 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 #include #include #include @@ -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 -- cgit v1.2.3