Botan: src/libstate/global_state.cpp Source File

Botan 1.10.17

global_state.cpp
Go to the documentation of this file.
1 /*
2 * Global State Management
3 * (C) 2010 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/global_state.h>
9 #include <botan/libstate.h>
10
11 namespace Botan {
12
13 /*
14 * @todo There should probably be a lock to avoid racy manipulation
15 * of the state among different threads
16 */
17
18 namespace Global_State_Management {
19
20 /*
21 * Botan's global state
22 */
23 namespace {
24
25 Library_State* global_lib_state = 0;
26
27 }
28
29 /*
30 * Access the global state object
31 */
32 Library_State& global_state()
33 {
34 /* Lazy initialization. Botan still needs to be deinitialized later
35 on or memory might leak.
36 */
37 if(!global_lib_state)
38 {
39 global_lib_state = new Library_State;
40 global_lib_state->initialize(true);
41 }
42
43 return (*global_lib_state);
44 }
45
46 /*
47 * Set a new global state object
48 */
49 void set_global_state(Library_State* new_state)
50 {
51 delete swap_global_state(new_state);
52 }
53
54 /*
55 * Set a new global state object unless one already existed
56 */
57 bool set_global_state_unless_set(Library_State* new_state)
58 {
59 if(global_lib_state)
60 {
61 delete new_state;
62 return false;
63 }
64 else
65 {
66 delete swap_global_state(new_state);
67 return true;
68 }
69 }
70
71 /*
72 * Swap two global state objects
73 */
74 Library_State* swap_global_state(Library_State* new_state)
75 {
76 Library_State* old_state = global_lib_state;
77 global_lib_state = new_state;
78 return old_state;
79 }
80
81 /*
82 * Query if library is initialized
83 */
84 bool global_state_exists()
85 {
86 return (global_lib_state != 0);
87 }
88
89 }
90
91 }
Botan::Library_State
Definition: libstate.h:27
Botan::Global_State_Management::set_global_state
void set_global_state(Library_State *new_state)
Definition: global_state.cpp:49
Botan::Global_State_Management::global_state
Library_State & global_state()
Definition: global_state.cpp:32
Botan::Library_State::initialize
void initialize(bool thread_safe)
Definition: libstate.cpp:197
Botan
Definition: algo_base.h:14
Botan::Global_State_Management::swap_global_state
Library_State * swap_global_state(Library_State *new_state)
Definition: global_state.cpp:74
Botan::Global_State_Management::global_state_exists
bool global_state_exists()
Definition: global_state.cpp:84
Botan::Global_State_Management::set_global_state_unless_set
bool set_global_state_unless_set(Library_State *new_state)
Definition: global_state.cpp:57