1 /* xbt.h - Public interface to the xbt (simgrid's toolbox) */
3 /* Copyright (c) 2004-2014. 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. */
14 #include "simgrid_config.h"
16 /* Define _GNU_SOURCE for getline, isfinite, etc. */
21 /* Attributes are only in recent versions of GCC */
22 #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4))
23 # define _XBT_GNUC_PRINTF( format_idx, arg_idx ) \
24 __attribute__((__format__ (__printf__, format_idx, arg_idx)))
25 # define _XBT_GNUC_SCANF( format_idx, arg_idx ) \
26 __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
27 # define _XBT_GNUC_NORETURN __attribute__((__noreturn__))
28 # define _XBT_GNUC_UNUSED __attribute__((__unused__))
29 /* Constructor priorities exist since gcc 4.3. Apparently, they are however not
30 * supported on Macs. */
31 # if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__APPLE__)
32 # define _XBT_GNUC_CONSTRUCTOR(prio) __attribute__((__constructor__ (prio)))
33 # define _XBT_GNUC_DESTRUCTOR(prio) __attribute__((__destructor__ (prio)))
35 # define _XBT_GNUC_CONSTRUCTOR(prio) __attribute__((__constructor__))
36 # define _XBT_GNUC_DESTRUCTOR(prio) __attribute__((__destructor__))
38 # undef _XBT_NEED_INIT_PRAGMA
41 # define _XBT_GNUC_PRINTF( format_idx, arg_idx )
42 # define _XBT_GNUC_SCANF( format_idx, arg_idx )
43 # define _XBT_GNUC_NORETURN
44 # define _XBT_GNUC_UNUSED
45 # define _XBT_GNUC_CONSTRUCTOR(prio)
46 # define _XBT_GNUC_DESTRUCTOR(prio)
47 # define _XBT_NEED_INIT_PRAGMA 1
49 #endif /* !__GNUC__ */
51 /* inline and __FUNCTION__ are only in GCC when -ansi is off */
53 #if defined(__GNUC__) && ! defined(__STRICT_ANSI__)
54 # define _XBT_FUNCTION __FUNCTION__
55 #elif (defined(__STDC__) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
56 # define _XBT_FUNCTION __func__ /* ISO-C99 compliant */
58 # define _XBT_FUNCTION "function"
65 # if defined(__GNUC__) && ! defined(__STRICT_ANSI__)
66 # define XBT_INLINE inline
67 # elif (defined(__STDC__) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
68 # define XBT_INLINE inline
69 # elif defined(__BORLANDC__) && !defined(__STRICT_ANSI__)
70 # define XBT_INLINE __inline
75 # if defined (__VISUALC__)
76 # define XBT_INLINE __inline
78 # define XBT_INLINE inline
80 # endif /* __cplusplus */
83 /* improvable on gcc (by evaluating arguments only once), but wouldn't be portable */
87 #define MIN(a,b) ((a)<(b)?(a):(b))
92 #define MAX(a,b) ((a)>(b)?(a):(b))
95 * Expands to `one' if there is only one argument for the variadic part.
96 * Otherwise, expands to `more'.
97 * Works with up to 63 arguments, which is the maximum mandated by the C99
100 #define _XBT_IF_ONE_ARG(one, more, ...) \
101 _XBT_IF_ONE_ARG_(__VA_ARGS__, \
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, more, \
106 more, more, more, more, more, more, more, more, \
107 more, more, more, more, more, more, more, more, \
108 more, more, more, more, more, more, more, more, \
109 more, more, more, more, more, more, more, one)
110 #define _XBT_IF_ONE_ARG_(a64, a63, a62, a61, a60, a59, a58, a57, \
111 a56, a55, a54, a53, a52, a51, a50, a49, \
112 a48, a47, a46, a45, a44, a43, a42, a41, \
113 a40, a39, a38, a37, a36, a35, a34, a33, \
114 a32, a31, a30, a29, a28, a27, a26, a25, \
115 a24, a23, a22, a21, a20, a19, a18, a17, \
116 a16, a15, a14, a13, a12, a11, a10, a9, \
117 a8, a7, a6, a5, a4, a3, a2, a1, N, ...) N
120 * Function calling convention (not used for now)
125 # define _XBT_CALL __cdecl
131 /* Handle import/export stuff
133 * Rational of XBT_PUBLIC:
134 * * This is for library symbols visible from the application-land.
135 * Basically, any symbols defined in the include/directory must be
136 * like this (plus some other globals).
138 * UNIX coders should just think of it as a special way to say "extern".
140 * * If you build the DLL, define the DLL_EXPORT symbol so that all symbols
141 * actually get exported by this file.
143 * * If you do a static windows compilation, define DLL_STATIC, both when
144 * compiling the application files and when compiling the library.
146 * * If you link your application against the DLL or if you do a UNIX build,
147 * don't do anything special. This file will do the right thing for you
151 * Rational of XBT_EXPORT_NO_IMPORT: (windows-only cruft)
152 * * Symbols which must be exported in the DLL, but not imported from it.
154 * * This is obviously useful for initialized globals (which cannot be
155 * extern or similar).
156 * * This is also used in the log mecanism where a macro creates the
157 * variable automatically. When the macro is called from within SimGrid,
158 * the symbol must be exported, but when called from within the client
159 * code, it must not try to retrieve the symbol from the DLL since it's
162 * Rational of XBT_IMPORT_NO_EXPORT: (windows-only cruft)
163 * * Symbols which must be imported from the DLL, but not explicitely
166 * * The root log category is already exported, but not imported explicitely
167 * when creating a subcategory since we cannot import the parent category
168 * to deal with the fact that the parent may be in application space, not
173 #if defined(DLL_EXPORT)
174 # define XBT_PUBLIC(type) extern __declspec(dllexport) type
175 # define XBT_EXPORT_NO_IMPORT(type) __declspec(dllexport) type
176 # define XBT_IMPORT_NO_EXPORT(type) type
177 # define XBT_PUBLIC_DATA(type) extern __declspec(dllexport) type
178 # define XBT_PUBLIC_CLASS class __declspec(dllexport)
181 /* Pack everything up statically */
182 #elif defined(DLL_STATIC)
183 # define XBT_PUBLIC(type) extern type
184 # define XBT_EXPORT_NO_IMPORT(type) type
185 # define XBT_IMPORT_NO_EXPORT(type) type
186 # define XBT_PUBLIC_DATA(type) extern type
187 # define XBT_PUBLIC_CLASS class
190 /* Link against the DLL */
191 #elif (defined(_XBT_WIN32) && !defined(DLL_EXPORT) && !defined(DLL_STATIC))
192 # define XBT_PUBLIC(type) extern __declspec(dllimport) type
193 # define XBT_EXPORT_NO_IMPORT(type) type
194 # define XBT_IMPORT_NO_EXPORT(type) __declspec(dllimport) type
195 # define XBT_PUBLIC_DATA(type) extern __declspec(dllimport) type
196 # define XBT_PUBLIC_CLASS class __declspec(dllimport)
201 # define XBT_PUBLIC(type) extern type
202 # define XBT_EXPORT_NO_IMPORT(type) type
203 # define XBT_IMPORT_NO_EXPORT(type) type
204 # define XBT_PUBLIC_DATA(type) extern type
205 # define XBT_PUBLIC_CLASS class
212 #define XBT_INTERNAL __attribute__((visibility ("hidden")))
215 #if !defined (max) && !defined(__cplusplus)
216 # define max(a,b) (((a) > (b)) ? (a) : (b))
218 #if !defined (min) && !defined(__cplusplus)
219 # define min(a,b) (((a) < (b)) ? (a) : (b))
225 /*! C++ users need love */
226 #ifndef SG_BEGIN_DECL
228 # define SG_BEGIN_DECL() extern "C" {
230 # define SG_BEGIN_DECL()
236 # define SG_END_DECL() }
238 # define SG_END_DECL()
241 /* End of cruft for C++ */
245 /** Cache the size of a memory page for the current system. */
246 XBT_PUBLIC_DATA(int) xbt_pagesize;
248 /** Cache the number of bits of addresses inside a given page, log2(xbt_pagesize). */
249 XBT_PUBLIC_DATA(int) xbt_pagebits;
251 XBT_PUBLIC(const char *) xbt_procname(void);
253 /** Retrieves the version numbers of the used dynamic library (so, DLL or dynlib) , while
254 SIMGRID_VERSION_MAJOR and friends give the version numbers of the used header files */
255 XBT_PUBLIC(void) sg_version(int *major,int *minor,int *patch);
257 typedef struct xbt_dynar_s s_xbt_dynar_t, *xbt_dynar_t;
259 /** Contains all the parameters we got from the command line */
260 XBT_PUBLIC_DATA(xbt_dynar_t) sg_cmdline;
262 #define XBT_BACKTRACE_SIZE 10 /* FIXME: better place? Do document */
264 /* snprintf related functions */
265 /** @addtogroup XBT_str
267 /** @brief print to allocated string (reimplemented when not provided by the system)
269 * The functions asprintf() and vasprintf() are analogues of
270 * sprintf() and vsprintf(), except that they allocate a string large
271 * enough to hold the output including the terminating null byte, and
272 * return a pointer to it via the first parameter. This pointer
273 * should be passed to free(3) to release the allocated storage when
274 * it is no longer needed.
276 #if defined(SIMGRID_NEED_ASPRINTF)||defined(DOXYGEN)
277 XBT_PUBLIC(int) asprintf(char **ptr, const char *fmt, /*args */
278 ...) _XBT_GNUC_PRINTF(2, 3);
280 /** @brief print to allocated string (reimplemented when not provided by the system)
284 #if defined(SIMGRID_NEED_VASPRINTF)||defined(DOXYGEN)
285 XBT_PUBLIC(int) vasprintf(char **ptr, const char *fmt, va_list ap);
288 /** @brief print to allocated string
290 * Works just like vasprintf(), but returns a pointer to the newly
291 * created string, or aborts on error.
293 XBT_PUBLIC(char *) bvprintf(const char *fmt, va_list ap);
294 /** @brief print to allocated string
296 * Works just like asprintf(), but returns a pointer to the newly
297 * created string, or aborts on error.
299 XBT_PUBLIC(char *) bprintf(const char *fmt, ...) _XBT_GNUC_PRINTF(1, 2);
304 #endif /* XBT_MISC_H */