Logo AND Algorithmique Numérique Distribuée

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