1 /* xbt.h - Public interface to the xbt (gras's toolbox) */
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
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)))
26 # define _XBT_GNUC_CONSTRUCTOR(prio) __attribute__((__constructor__))
27 # define _XBT_GNUC_DESTRUCTOR(prio) __attribute__((__destructor__))
29 # undef _XBT_NEED_INIT_PRAGMA
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
40 #endif /* !__GNUC__ */
42 /* inline and __FUNCTION__ are only in GCC when -ansi is off */
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 */
49 # define _XBT_FUNCTION "function"
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
66 # if defined (__VISUALC__)
67 # define XBT_INLINE __inline
69 # define XBT_INLINE inline
71 # endif /* __cplusplus */
74 /* improvable on gcc (by evaluating arguments only once), but wouldn't be portable */
78 #define MIN(a,b) ((a)<(b)?(a):(b))
83 #define MAX(a,b) ((a)>(b)?(a):(b))
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
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
111 * Function calling convention (not used for now)
116 # define _XBT_CALL __cdecl
122 /* Handle import/export stuff
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).
129 * UNIX coders should just think of it as a special way to say "extern".
131 * * If you build the DLL, define the DLL_EXPORT symbol so that all symbols
132 * actually get exported by this file.
134 * * If you do a static windows compilation, define DLL_STATIC, both when
135 * compiling the application files and when compiling the library.
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
142 * Rational of XBT_EXPORT_NO_IMPORT: (windows-only cruft)
143 * * Symbols which must be exported in the DLL, but not imported from it.
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
153 * Rational of XBT_IMPORT_NO_EXPORT: (windows-only cruft)
154 * * Symbols which must be imported from the DLL, but not explicitely
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
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
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
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
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
193 #if !defined (max) && !defined(__cplusplus)
194 # define max(a,b) (((a) > (b)) ? (a) : (b))
196 #if !defined (min) && !defined(__cplusplus)
197 # define min(a,b) (((a) < (b)) ? (a) : (b))
203 /*! C++ users need love */
204 #ifndef SG_BEGIN_DECL
206 # define SG_BEGIN_DECL() extern "C" {
208 # define SG_BEGIN_DECL()
214 # define SG_END_DECL() }
216 # define SG_END_DECL()
219 /* End of cruft for C++ */
223 XBT_PUBLIC(const char *) xbt_procname(void);
225 #define XBT_BACKTRACE_SIZE 10 /* FIXME: better place? Do document */
228 #endif /* XBT_MISC_H */