diff options
author | lloyd <[email protected]> | 2010-08-06 18:13:09 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-08-06 18:13:09 +0000 |
commit | 789cee0888bbede838a2a3cf6221677c9f172872 (patch) | |
tree | faf2dc4e48afe3975e8f6be81a6eea5626c06f69 /src/libstate | |
parent | abc179a6549fedf1c14474342336f1dcb965e45f (diff) | |
parent | 22718fa6526bf5c9923d0091d662cd2def75c605 (diff) |
propagate from branch 'net.randombit.botan' (head c6dfcb1b2e25290f071169361fb07cc9bb602eac)
to branch 'net.randombit.botan.c++0x' (head 802b80d11c9eb4550d592439b773f6bc8b0ec045)
Diffstat (limited to 'src/libstate')
-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 9aba6b8dd..a017589fc 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 a65098d5a..5fd476994 100644 --- a/src/libstate/init.cpp +++ b/src/libstate/init.cpp @@ -7,6 +7,7 @@ #include <botan/init.h> #include <botan/libstate.h> +#include <botan/global_state.h> namespace Botan { @@ -24,7 +25,7 @@ void LibraryInitializer::initialize(const std::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(); } @@ -40,7 +41,7 @@ void LibraryInitializer::initialize(const std::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 7bb31b52c..0d7463306 100644 --- a/src/libstate/libstate.cpp +++ b/src/libstate/libstate.cpp @@ -46,50 +46,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(); - } - - 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) - { - auto old_state = global_lib_state; - global_lib_state = new_state; - return old_state; - } - -/* * Get an allocator by its name */ Allocator* Library_State::get_allocator(const std::string& type) diff --git a/src/libstate/libstate.h b/src/libstate/libstate.h index f3abdf87a..49908a1e3 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> @@ -129,25 +130,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 |