1 /**
2 * Global State Management
3 *
4 * Copyright:
5 * (C) 2010 Jack Lloyd
6 * (C) 2014-2015 Etienne Cimon
7 *
8 * License:
9 * Botan is released under the Simplified BSD License (see LICENSE.md)
10 */11 modulebotan.libstate.global_state;
12 importbotan.constants;
13 importbotan.libstate.libstate;
14 importcore.thread : Thread;
15 /// Thread-Local, no locks needed.16 privateLibraryStateg_lib_state;
17 18 static ~this() {
19 if (g_lib_state)
20 g_lib_state.destroy();
21 22 if (Thread.getThis() != gs_ctor) return;
23 if (gs_global_prng)
24 gs_global_prng.destroy();
25 if (gs_sources.length > 0) {
26 gs_sources.clear();
27 gs_sources.destroy();
28 }
29 }
30 31 /**
32 * Access the global library state
33 * Returns: reference to the global library state
34 */35 LibraryStateglobalState()
36 {
37 if (!g_lib_state) {
38 39 g_lib_state = newLibraryState;
40 /* Lazy initialization. Botan still needs to be deinitialized later
41 on or memory might leak.
42 */43 g_lib_state.initialize();
44 }
45 returng_lib_state;
46 }
47 48 /**
49 * Set the global state object
50 * Params:
51 * new_state = the new global state to use
52 */53 voidsetGlobalState(LibraryStatenew_state)
54 {
55 if (g_lib_state) destroy(g_lib_state);
56 g_lib_state = new_state;
57 }
58 59 60 /**
61 * Set the global state object unless it is already set
62 * Params:
63 * new_state = the new global state to use
64 * Returns: true if the state parameter is now being used as the global
65 * state, or false if one was already set, in which case the
66 * parameter was deleted immediately
67 */68 boolsetGlobalStateUnlessSet(LibraryStatenew_state)
69 {
70 if (g_lib_state)
71 {
72 returnfalse;
73 }
74 else75 {
76 g_lib_state = new_state;
77 returntrue;
78 }
79 }
80 81 /**
82 * Query if the library is currently initialized
83 * Returns: true iff the library is initialized
84 */85 boolglobalStateExists()
86 {
87 return (g_lib_state !isLibraryState.init);
88 }