Logo AND Algorithmique Numérique Distribuée

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