Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot//simgrid/simgrid
[simgrid.git] / include / xbt / misc.h
1 /* xbt.h - Public interface to the xbt (gras's toolbox)                     */
2
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #ifndef XBT_MISC_H
10 #define XBT_MISC_H
11
12 /* Attributes are only in recent versions of GCC */
13 #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4))
14 # define _XBT_GNUC_PRINTF( format_idx, arg_idx )    \
15            __attribute__((__format__ (__printf__, format_idx, arg_idx)))
16 # define _XBT_GNUC_SCANF( format_idx, arg_idx )     \
17                __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
18 # define _XBT_GNUC_NORETURN __attribute__((__noreturn__))
19 # define _XBT_GNUC_UNUSED  __attribute__((__unused__))
20 # define _XBT_GNUC_CONSTRUCTOR __attribute__((__constructor__))
21 # define _XBT_GNUC_DESTRUCTOR __attribute__((__destructor__))
22 # undef _XBT_NEED_INIT_PRAGMA
23
24 #else                           /* !__GNUC__ */
25 # define _XBT_GNUC_PRINTF( format_idx, arg_idx )
26 # define _XBT_GNUC_SCANF( format_idx, arg_idx )
27 # define _XBT_GNUC_NORETURN
28 # define _XBT_GNUC_UNUSED
29 # define _XBT_GNUC_CONSTRUCTOR
30 # define _XBT_GNUC_DESTRUCTOR
31 # define  _XBT_NEED_INIT_PRAGMA 1
32
33 #endif                          /* !__GNUC__ */
34
35 /* inline and __FUNCTION__ are only in GCC when -ansi is off */
36
37 #if defined(__GNUC__) && ! defined(__STRICT_ANSI__)
38 # define _XBT_FUNCTION __FUNCTION__
39 #elif (defined(__STDC__) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
40 # define _XBT_FUNCTION __func__ /* ISO-C99 compliant */
41 #else
42 # define _XBT_FUNCTION "function"
43 #endif
44
45 #ifdef DOXYGEN
46 #  define XBT_INLINE
47 #else
48 #  ifndef __cplusplus
49 #    if defined(__GNUC__) && ! defined(__STRICT_ANSI__)
50 #        define XBT_INLINE inline
51 #    elif (defined(__STDC__) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
52 #        define XBT_INLINE inline
53 #    elif defined(__BORLANDC__) && !defined(__STRICT_ANSI__)
54 #        define XBT_INLINE __inline
55 #    else
56 #        define XBT_INLINE
57 #    endif
58 #  else
59 #          if defined (__VISUALC__)
60 #                  define XBT_INLINE __inline
61 #          else
62 #        define XBT_INLINE  inline
63 #          endif
64 #  endif /* __cplusplus */
65 #endif
66
67 /* improvable on gcc (by evaluating arguments only once), but wouldn't be portable */
68 #ifdef MIN
69 # undef MIN
70 #endif
71 #define MIN(a,b) ((a)<(b)?(a):(b))
72
73 #ifdef MAX
74 # undef MAX
75 #endif
76 #define MAX(a,b) ((a)>(b)?(a):(b))
77
78 /*
79  * Expands to `one' if there is only one argument for the variadic part.
80  * Otherwise, expands to `more'.
81  * Works with up to 63 arguments, which is the maximum mandated by the C99
82  * standard.
83  */
84 #define _XBT_IF_ONE_ARG(one, more, ...)                                 \
85     _XBT_IF_ONE_ARG_(__VA_ARGS__,                                       \
86                      more, more, more, more, more, more, more, more,    \
87                      more, more, more, more, more, more, more, more,    \
88                      more, more, more, more, more, more, more, more,    \
89                      more, more, more, more, more, more, more, more,    \
90                      more, more, more, more, more, more, more, more,    \
91                      more, more, more, more, more, more, more, more,    \
92                      more, more, more, more, more, more, more, more,    \
93                      more, more, more, more, more, more, more, one)
94 #define _XBT_IF_ONE_ARG_(a64, a63, a62, a61, a60, a59, a58, a57,        \
95                          a56, a55, a54, a53, a52, a51, a50, a49,        \
96                          a48, a47, a46, a45, a44, a43, a42, a41,        \
97                          a40, a39, a38, a37, a36, a35, a34, a33,        \
98                          a32, a31, a30, a29, a28, a27, a26, a25,        \
99                          a24, a23, a22, a21, a20, a19, a18, a17,        \
100                          a16, a15, a14, a13, a12, a11, a10, a9,         \
101                          a8, a7, a6, a5, a4, a3, a2, a1, N, ...) N
102
103 /* 
104  * Function calling convention (not used for now) 
105  */
106
107 #ifdef _XBT_WIN32
108 #  ifndef _XBT_CALL
109 #    define _XBT_CALL __cdecl
110 #   endif
111 #else
112 #  define _XBT_CALL
113 #endif
114
115 /* Handle import/export stuff
116  * 
117  * Rational of XBT_PUBLIC: 
118  *   * This is for library symbols visible from the application-land.
119  *     Basically, any symbols defined in the include/directory must be 
120  *     like this (plus some other globals). 
121  *
122  *     UNIX coders should just think of it as a special way to say "extern".
123  *
124  *   * If you build the DLL, define the DLL_EXPORT symbol so that all symbols
125  *     actually get exported by this file.
126
127  *   * If you do a static windows compilation, define DLL_STATIC, both when
128  *     compiling the application files and when compiling the library.
129  *
130  *   * If you link your application against the DLL or if you do a UNIX build,
131  *     don't do anything special. This file will do the right thing for you 
132  *     by default.
133  *
134  * 
135  * Rational of XBT_EXPORT_NO_IMPORT: (windows-only cruft)
136  *   * Symbols which must be exported in the DLL, but not imported from it.
137  * 
138  *   * This is obviously useful for initialized globals (which cannot be 
139  *     extern or similar).
140  *   * This is also used in the log mecanism where a macro creates the 
141  *     variable automatically. When the macro is called from within SimGrid,
142  *     the symbol must be exported, but when called  from within the client
143  *     code, it must not try to retrieve the symbol from the DLL since it's
144  *      not in there.
145  * 
146  * Rational of XBT_IMPORT_NO_EXPORT: (windows-only cruft)
147  *   * Symbols which must be imported from the DLL, but not explicitely 
148  *     exported from it.
149  * 
150  *   * The root log category is already exported, but not imported explicitely 
151  *     when creating a subcategory since we cannot import the parent category 
152  *     to deal with the fact that the parent may be in application space, not 
153  *     DLL space.
154  */
155
156
157 /* Build the DLL */
158 #if defined(DLL_EXPORT)
159 #  define XBT_PUBLIC(type)            __declspec(dllexport) type
160 #  define XBT_EXPORT_NO_IMPORT(type)  __declspec(dllexport) type
161 #  define XBT_IMPORT_NO_EXPORT(type)  type
162 #  define XBT_PUBLIC_DATA(type)       __declspec(dllexport) type
163
164 /* Pack everything up statically */
165 #elif defined(DLL_STATIC)
166 #  define XBT_PUBLIC(type)           extern type
167 #  define XBT_EXPORT_NO_IMPORT(type)  type
168 #  define XBT_IMPORT_NO_EXPORT(type)  type
169 #  define XBT_PUBLIC_DATA(type)       extern type
170
171 /* Link against the DLL */
172 #elif (defined(_XBT_WIN32) && !defined(DLL_EXPORT) && !defined(DLL_STATIC))
173 #  define XBT_PUBLIC(type)              __declspec(dllimport) type
174 #  define XBT_EXPORT_NO_IMPORT(type)    type
175 #  define XBT_IMPORT_NO_EXPORT(type)    __declspec(dllimport) type
176 #  define XBT_PUBLIC_DATA(type)         __declspec(dllimport) type
177
178 /* UNIX build */
179 #else
180 #  define XBT_PUBLIC(type)            extern type
181 #  define XBT_EXPORT_NO_IMPORT(type)  type
182 #  define XBT_IMPORT_NO_EXPORT(type)  type
183 #  define XBT_PUBLIC_DATA(type)       extern type
184 #endif
185
186 #if !defined (max) && !defined(__cplusplus)
187 #  define max(a,b)      (((a) > (b)) ? (a) : (b))
188 #endif
189 #if !defined (min) && !defined(__cplusplus)
190 #  define min(a,b)      (((a) < (b)) ? (a) : (b))
191 #endif
192
193 #define TRUE  1
194 #define FALSE 0
195
196 #define XBT_MAX_CHANNEL 10      /* FIXME: killme */
197 /*! C++ users need love */
198 #ifndef SG_BEGIN_DECL
199 # ifdef __cplusplus
200 #  define SG_BEGIN_DECL() extern "C" {
201 # else
202 #  define SG_BEGIN_DECL()
203 # endif
204 #endif
205
206 #ifndef SG_END_DECL
207 # ifdef __cplusplus
208 #  define SG_END_DECL() }
209 # else
210 #  define SG_END_DECL()
211 # endif
212 #endif
213 /* End of cruft for C++ */
214
215 SG_BEGIN_DECL()
216
217 XBT_PUBLIC(const char *) xbt_procname(void);
218
219 #define XBT_BACKTRACE_SIZE 10   /* FIXME: better place? Do document */
220
221 /** @brief File datatype
222     @ingroup m_datatypes_management_details */
223 typedef struct m_file {
224   char *name;                   /**< @brief file name */
225   void *content;                   /**< @brief user data */
226 } s_m_file_t;
227 /** @brief File datatype
228     @ingroup m_datatypes_management_details */
229 typedef struct m_file *m_file_t;
230
231
232 SG_END_DECL()
233 #endif                          /* XBT_MISC_H */