Logo AND Algorithmique Numérique Distribuée

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