eina_safety_checks_init()
[profile/ivi/eina.git] / src / lib / eina_main.c
1 /* EINA - EFL data type library
2  * Copyright (C) 2008 Cedric Bail
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library;
16  * if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stdio.h>
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "eina_config.h"
26 #include "eina_types.h"
27 #include "eina_main.h"
28 #include "eina_error.h"
29 #include "eina_safety_checks.h"
30 #include "eina_log.h"
31 #include "eina_hash.h"
32 #include "eina_stringshare.h"
33 #include "eina_list.h"
34 #include "eina_array.h"
35 #include "eina_counter.h"
36 #include "eina_benchmark.h"
37 #include "eina_magic.h"
38 #include "eina_rectangle.h"
39
40 /*============================================================================*
41  *                                  Local                                     *
42  *============================================================================*/
43
44 /**
45  * @cond LOCAL
46  */
47
48 static int _eina_main_count = 0;
49
50 /**
51  * @endcond
52  */
53
54 /*============================================================================*
55  *                                 Global                                     *
56  *============================================================================*/
57
58 /*============================================================================*
59  *                                   API                                      *
60  *============================================================================*/
61
62 /**
63  * @addtogroup Eina_Main_Group Main
64  *
65  * @brief These functions provide general initialisation and shut down
66  * functions.
67  *
68  * @{
69  */
70
71 /**
72  * @brief Initialize the Eina library.
73  *
74  * @return 1 or greater on success, 0 on error.
75  *
76  * This function sets up all the eina modules. It returns 0 on
77  * failure (that is, when one of the module fails to initialize),
78  * otherwise it returns the number of times it has already been
79  * called. The list of initialisation functions that are called are
80  * (in that order):
81  *
82  * @li eina_error_init()
83  * @li eina_log_init()
84  * @li eina_safety_checks_init()
85  * @li eina_hash_init()
86  * @li eina_stringshare_init()
87  * @li eina_list_init()
88  * @li eina_array_init()
89  * @li eina_counter_init()
90  * @li eina_benchmark_init()
91  * @li eina_magic_string_init()
92  * @li eina_rectangle_init()
93  *
94  * When Eina is not used anymore, call eina_shutdown() to shut down
95  * the Eina library.
96  */
97 EAPI int
98 eina_init(void)
99 {
100    if (_eina_main_count) goto finish_init;
101
102    if (!eina_error_init())
103      {
104         fprintf(stderr, "Could not initialize eina error module.\n");
105         return 0;
106      }
107
108    if (!eina_log_init())
109      {
110         fprintf(stderr, "Could not initialize eina log module.\n");
111         goto log_init_error;
112      }
113
114    if (!eina_safety_checks_init())
115      {
116         EINA_ERROR_PERR("Could not initialize eina safety checks module.\n");
117         goto safety_checks_init_error;
118      }
119
120    if (!eina_hash_init())
121      {
122         EINA_ERROR_PERR("Could not initialize eina hash module.\n");
123         goto hash_init_error;
124      }
125    if (!eina_stringshare_init())
126      {
127         EINA_ERROR_PERR("Could not initialize eina stringshare module.\n");
128         goto stringshare_init_error;
129      }
130    if (!eina_list_init())
131      {
132         EINA_ERROR_PERR("Could not initialize eina list module.\n");
133         goto list_init_error;
134      }
135    if (!eina_array_init())
136      {
137         EINA_ERROR_PERR("Could not initialize eina array module.\n");
138         goto array_init_error;
139      }
140    if (!eina_counter_init())
141      {
142         EINA_ERROR_PERR("Could not initialize eina counter module.\n");
143         goto counter_init_error;
144      }
145    if (!eina_benchmark_init())
146      {
147         EINA_ERROR_PERR("Could not initialize eina benchmark module.\n");
148         goto benchmark_init_error;
149      }
150    if (!eina_magic_string_init())
151      {
152         EINA_ERROR_PERR("Could not initialize eina magic string module.\n");
153         goto magic_string_init_error;
154      }
155    if (!eina_rectangle_init())
156      {
157         EINA_ERROR_PERR("Could not initialize eina rectangle module.\n");
158         goto rectangle_init_error;
159      }
160
161  finish_init:
162    return ++_eina_main_count;
163
164  rectangle_init_error:
165    eina_magic_string_shutdown();
166  magic_string_init_error:
167    eina_benchmark_shutdown();
168  benchmark_init_error:
169    eina_counter_shutdown();
170  counter_init_error:
171    eina_array_shutdown();
172  array_init_error:
173    eina_list_shutdown();
174  list_init_error:
175    eina_stringshare_shutdown();
176  stringshare_init_error:
177    eina_hash_shutdown();
178  hash_init_error:
179    eina_log_shutdown();
180  safety_checks_init_error:
181    eina_safety_checks_shutdown();
182  log_init_error:
183    eina_error_shutdown();
184
185    return 0;
186 }
187
188 /**
189  * @brief Shut down the Eina library.
190  *
191  * @return 0 when all the modules is completely shut down, 1 or
192  * greater otherwise.
193  *
194  * This function shuts down the Eina library. It returns 0 when it has
195  * been called the same number of times than eina_init(). In that case
196  * it shut down all the Eina modules. The list of shut down functions
197  * that are called are (in that order):
198  *
199  * @li eina_rectangle_shutdown()
200  * @li eina_magic_string_shutdown()
201  * @li eina_benchmark_shutdown()
202  * @li eina_counter_shutdown()
203  * @li eina_array_shutdown()
204  * @li eina_list_shutdown()
205  * @li eina_stringshare_shutdown()
206  * @li eina_hash_shutdown()
207  * @li eina_safety_checks_shutdown()
208  * @li eina_log_shutdown()
209  * @li eina_error_shutdown()
210  *
211  * Once this function succeeds (that is, @c 0 is returned), you must
212  * not call any of the Eina function anymore. You must call
213  * eina_init() again to use the Eina functions again.
214  */
215 EAPI int
216 eina_shutdown(void)
217 {
218    if (_eina_main_count != 1) goto finish_shutdown;
219
220    eina_rectangle_shutdown();
221    eina_magic_string_shutdown();
222    eina_benchmark_shutdown();
223    eina_counter_shutdown();
224    eina_array_shutdown();
225    eina_list_shutdown();
226    eina_stringshare_shutdown();
227    eina_hash_shutdown();
228    eina_safety_checks_shutdown();
229    eina_log_shutdown();
230    eina_error_shutdown();
231
232  finish_shutdown:
233    return --_eina_main_count;
234 }
235
236 /**
237  * @}
238  */