Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cleanup around the free-like functions used as dealloc callbacks in dynar and dicts...
[simgrid.git] / src / xbt / xbt_main.c
1 /* $Id$ */
2
3 /* module handling                                                          */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "time.h" /* to seed the random generator */
11
12 #include "xbt/sysdep.h"
13 #include "xbt/log.h"
14 #include "xbt/dynar.h"
15 #include "xbt/config.h"
16
17 #include "xbt/module.h" /* this module */
18
19 #include "xbt_modinter.h"  /* prototype of other module's init/exit in XBT */
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(module,xbt, "module handling");
22
23 char *xbt_binary_name=NULL; /* Mandatory to retrieve neat backtraces */
24 int xbt_initialized=0;
25
26 XBT_LOG_EXTERNAL_CATEGORY(graphxml_parse);
27 XBT_LOG_EXTERNAL_CATEGORY(log);
28 XBT_LOG_EXTERNAL_CATEGORY(module);
29 XBT_LOG_EXTERNAL_CATEGORY(peer);
30 XBT_LOG_EXTERNAL_CATEGORY(strbuff);
31 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
32 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
33 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict_add);
34 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict_collapse);
35 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict_cursor);
36 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict_elm);
37 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict_multi);
38 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict_remove);
39 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict_search);
40 XBT_LOG_EXTERNAL_CATEGORY(xbt_dyn);
41 XBT_LOG_EXTERNAL_CATEGORY(xbt_ex);
42 XBT_LOG_EXTERNAL_CATEGORY(xbt_fifo);
43 XBT_LOG_EXTERNAL_CATEGORY(xbt_graph);
44 XBT_LOG_EXTERNAL_CATEGORY(xbt_matrix);
45 XBT_LOG_EXTERNAL_CATEGORY(xbt_queue);
46 XBT_LOG_EXTERNAL_CATEGORY(xbt_set);
47 XBT_LOG_EXTERNAL_CATEGORY(xbt_sync_os);
48
49 /** @brief Initialize the xbt mechanisms. */
50 void 
51 xbt_init(int *argc, char **argv) {
52   xbt_initialized++;
53
54   if (xbt_initialized!=1)
55     return;
56
57   /* Connect our log channels: that must be done manually under windows */
58   XBT_LOG_CONNECT(graphxml_parse, xbt);
59   XBT_LOG_CONNECT(log, xbt);
60   XBT_LOG_CONNECT(module, xbt);
61   XBT_LOG_CONNECT(peer, xbt);
62   XBT_LOG_CONNECT(strbuff, xbt);
63   XBT_LOG_CONNECT(xbt_cfg, xbt);
64   XBT_LOG_CONNECT(xbt_dict, xbt);
65     XBT_LOG_CONNECT(xbt_dict_add, xbt_dict);
66     XBT_LOG_CONNECT(xbt_dict_collapse, xbt_dict);
67     XBT_LOG_CONNECT(xbt_dict_cursor, xbt_dict);
68     XBT_LOG_CONNECT(xbt_dict_elm, xbt_dict);
69     XBT_LOG_CONNECT(xbt_dict_multi, xbt_dict);
70     XBT_LOG_CONNECT(xbt_dict_remove, xbt_dict);
71     XBT_LOG_CONNECT(xbt_dict_search, xbt_dict);
72   XBT_LOG_CONNECT(xbt_dyn, xbt);
73   XBT_LOG_CONNECT(xbt_ex, xbt);
74   XBT_LOG_CONNECT(xbt_fifo, xbt);
75   XBT_LOG_CONNECT(xbt_graph, xbt);
76   XBT_LOG_CONNECT(xbt_matrix, xbt);
77   XBT_LOG_CONNECT(xbt_queue, xbt);
78   XBT_LOG_CONNECT(xbt_set, xbt);
79   XBT_LOG_CONNECT(xbt_sync_os, xbt);
80    
81   xbt_binary_name = xbt_strdup(argv[0]);
82   srand((unsigned int)time(NULL));
83   VERB0("Initialize XBT");
84   
85   xbt_backtrace_init();
86   xbt_log_init(argc,argv);
87   xbt_os_thread_mod_init();
88   xbt_context_mod_init();
89 }
90
91 /** @brief Finalize the xbt mechanisms. */
92 void 
93 xbt_exit(){
94   xbt_initialized--;
95   if (xbt_initialized == 0) {
96     xbt_fifo_exit();
97     xbt_dict_exit();
98     xbt_context_mod_exit();
99     xbt_os_thread_mod_exit();
100     xbt_log_exit();
101     xbt_backtrace_exit();
102   }
103    
104   if (xbt_initialized == 0)
105     free(xbt_binary_name);
106 }
107
108
109 /* these two functions belong to xbt/sysdep.h, which have no corresponding .c file */
110 /** @brief like free, but you can be sure that it is a function  */
111 XBT_PUBLIC(void) xbt_free_f(void* p) {
112    free(p);
113 }
114
115 /** @brief should be given a pointer to pointer, and frees the second one */
116 XBT_PUBLIC(void) xbt_free_ref(void *d){
117    free(*(void**)d);
118 }
119