Logo AND Algorithmique Numérique Distribuée

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