Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5a23b7355ab65b8fb30b4fd04dfe37bff84d11ea
[simgrid.git] / include / xbt / misc.h
1 /* xbt.h - Public interface to the xbt (simgrid's toolbox)                     */
2
3 /* Copyright (c) 2004-2014. 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 #include <stdarg.h>
13
14 #include "simgrid_config.h"
15
16 /* Define _GNU_SOURCE for getline, isfinite, etc. */
17 #ifndef _GNU_SOURCE
18         #define _GNU_SOURCE
19 #endif
20
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)))
34 # else
35 #  define _XBT_GNUC_CONSTRUCTOR(prio) __attribute__((__constructor__))
36 #  define _XBT_GNUC_DESTRUCTOR(prio) __attribute__((__destructor__))
37 # endif
38 # undef _XBT_NEED_INIT_PRAGMA
39
40 #else                           /* !__GNUC__ */
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
48
49 #endif                          /* !__GNUC__ */
50
51 /* inline and __FUNCTION__ are only in GCC when -ansi is off */
52
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 */
57 #else
58 # define _XBT_FUNCTION "function"
59 #endif
60
61 #ifdef DOXYGEN
62 #  define XBT_INLINE
63 #else
64 #  ifndef __cplusplus
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
71 #    else
72 #        define XBT_INLINE
73 #    endif
74 #  else
75 #     if defined (__VISUALC__)
76 #       define XBT_INLINE __inline
77 #     else
78 #       define XBT_INLINE  inline
79 #     endif
80 #  endif /* __cplusplus */
81 #endif
82
83 /* improvable on gcc (by evaluating arguments only once), but wouldn't be portable */
84 #ifdef MIN
85 # undef MIN
86 #endif
87 #define MIN(a,b) ((a)<(b)?(a):(b))
88
89 #ifdef MAX
90 # undef MAX
91 #endif
92 #define MAX(a,b) ((a)>(b)?(a):(b))
93
94 /*
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
98  * standard.
99  */
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
118
119 /* 
120  * Function calling convention (not used for now) 
121  */
122
123 #ifdef _XBT_WIN32
124 #  ifndef _XBT_CALL
125 #    define _XBT_CALL __cdecl
126 #   endif
127 #else
128 #  define _XBT_CALL
129 #endif
130
131 /* Handle import/export stuff
132  * 
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). 
137  *
138  *     UNIX coders should just think of it as a special way to say "extern".
139  *
140  *   * If you build the DLL, define the DLL_EXPORT symbol so that all symbols
141  *     actually get exported by this file.
142
143  *   * If you do a static windows compilation, define DLL_STATIC, both when
144  *     compiling the application files and when compiling the library.
145  *
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 
148  *     by default.
149  *
150  * 
151  * Rational of XBT_EXPORT_NO_IMPORT: (windows-only cruft)
152  *   * Symbols which must be exported in the DLL, but not imported from it.
153  * 
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
160  *      not in there.
161  * 
162  * Rational of XBT_IMPORT_NO_EXPORT: (windows-only cruft)
163  *   * Symbols which must be imported from the DLL, but not explicitely 
164  *     exported from it.
165  * 
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 
169  *     DLL space.
170  */
171
172 /* Build the DLL */
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)
179
180
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
188
189
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)
197
198
199 /* UNIX build */
200 #else
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
206
207 #endif
208
209 #ifdef _XBT_WIN32
210 #define XBT_INTERNAL
211 #else
212 #define XBT_INTERNAL __attribute__((visibility ("hidden")))
213 #endif
214
215 #if !defined (max) && !defined(__cplusplus)
216 #  define max(a,b)  (((a) > (b)) ? (a) : (b))
217 #endif
218 #if !defined (min) && !defined(__cplusplus)
219 #  define min(a,b)  (((a) < (b)) ? (a) : (b))
220 #endif
221
222 #define TRUE  1
223 #define FALSE 0
224
225 /*! C++ users need love */
226 #ifndef SG_BEGIN_DECL
227 # ifdef __cplusplus
228 #  define SG_BEGIN_DECL() extern "C" {
229 # else
230 #  define SG_BEGIN_DECL()
231 # endif
232 #endif
233
234 #ifndef SG_END_DECL
235 # ifdef __cplusplus
236 #  define SG_END_DECL() }
237 # else
238 #  define SG_END_DECL()
239 # endif
240 #endif
241 /* End of cruft for C++ */
242
243 SG_BEGIN_DECL()
244
245 /** Cache the size of a memory page for the current system. */
246 XBT_PUBLIC_DATA(int) xbt_pagesize;
247
248 /** Cache the number of bits of addresses inside a given page, log2(xbt_pagesize). */
249 XBT_PUBLIC_DATA(int) xbt_pagebits;
250
251 XBT_PUBLIC(const char *) xbt_procname(void);
252
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);
256
257 typedef struct xbt_dynar_s s_xbt_dynar_t, *xbt_dynar_t;
258
259 /** Contains all the parameters we got from the command line */
260 XBT_PUBLIC_DATA(xbt_dynar_t) sg_cmdline;
261
262 #define XBT_BACKTRACE_SIZE 10   /* FIXME: better place? Do document */
263
264 /* snprintf related functions */
265 /** @addtogroup XBT_str
266   * @{ */
267 /** @brief print to allocated string (reimplemented when not provided by the system)
268  *
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.
275  */
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);
279 #endif
280 /** @brief print to allocated string (reimplemented when not provided by the system)
281  *
282  * See asprintf()
283  */
284 #if defined(SIMGRID_NEED_VASPRINTF)||defined(DOXYGEN)
285 XBT_PUBLIC(int) vasprintf(char **ptr, const char *fmt, va_list ap);
286 #endif
287
288 /** @brief print to allocated string
289  *
290  * Works just like vasprintf(), but returns a pointer to the newly
291  * created string, or aborts on error.
292  */
293 XBT_PUBLIC(char *) bvprintf(const char *fmt, va_list ap);
294 /** @brief print to allocated string
295  *
296  * Works just like asprintf(), but returns a pointer to the newly
297  * created string, or aborts on error.
298  */
299 XBT_PUBLIC(char *) bprintf(const char *fmt, ...) _XBT_GNUC_PRINTF(1, 2);
300 /** @} */
301
302 SG_END_DECL()
303
304 #endif                          /* XBT_MISC_H */