Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a48059e4b1edf21d57b9979be52af49905867ae8
[simgrid.git] / include / xbt / misc.h
1 /* xbt.h - Public interface to the xbt (gras's toolbox)                     */
2
3 /* Copyright (c) 2004 Martin Quinson.                                       */
4 /* Copyright (c) 2004 Arnaud Legrand.                                       */
5 /* All rights reserved.                                                     */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #ifndef XBT_MISC_H
11 #define XBT_MISC_H
12
13 /* Attributes are only in recent versions of GCC */
14 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
15 # define _XBT_GNUC_PRINTF( format_idx, arg_idx )    \
16            __attribute__((__format__ (__printf__, format_idx, arg_idx)))
17 # define _XBT_GNUC_SCANF( format_idx, arg_idx )     \
18                __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
19 # define _XBT_GNUC_FORMAT( arg_idx )                \
20                    __attribute__((__format_arg__ (arg_idx)))
21 # define _XBT_GNUC_NORETURN __attribute__((__noreturn__))
22 # define _XBT_GNUC_UNUSED  __attribute__((unused))
23
24 #else /* !__GNUC__ */
25 # define _XBT_GNUC_PRINTF( format_idx, arg_idx )
26 # define _XBT_GNUC_SCANF( format_idx, arg_idx )
27 # define _XBT_GNUC_FORMAT( arg_idx )
28 # define _XBT_GNUC_NORETURN
29 # define _XBT_GNUC_UNUSED
30
31 #endif /* !__GNUC__ */
32
33 /* inline and __FUNCTION__ are only in GCC when -ansi is off */
34
35 #if defined(__GNUC__) && ! defined(__STRICT_ANSI__)
36 # define _XBT_FUNCTION __FUNCTION__
37 #elif (defined(__STDC__) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
38 # define _XBT_FUNC__ __func__   /* ISO-C99 compliant */
39 #else
40 # define _XBT_FUNCTION "function"
41 #endif
42
43 #ifndef __cplusplus
44 #    if defined(__GNUC__) && ! defined(__STRICT_ANSI__)
45 #        define XBT_INLINE inline
46 #    elif (defined(__STDC__) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
47 #        define XBT_INLINE inline
48 #    elif defined(__BORLANDC__) && !defined(__STRICT_ANSI__)
49 #        define XBT_INLINE __inline
50 #    else
51 #        define XBT_INLINE
52 #    endif
53 # else
54 #    define XBT_INLINE  inline
55 #endif
56
57 /* improvable on gcc (by evaluating arguments only once), but wouldn't be portable */
58 #ifdef MIN
59 # undef MIN
60 #endif
61 #define MIN(a,b) ((a)<(b)?(a):(b))
62
63 #ifdef MAX
64 # undef MAX
65 #endif
66 #define MAX(a,b) ((a)>(b)?(a):(b))
67
68
69 /* 
70  * Function calling convention (not used for now) 
71  */
72
73 #ifdef _WIN32
74 #  ifndef _XBT_CALL
75 #    define _XBT_CALL __cdecl
76 #   endif
77 #else
78 #  define _XBT_CALL
79 #endif
80
81 /* Handle import/export stuff
82  * 
83  * Rational of XBT_PUBLIC: 
84  *   * This is for library symbols visible from the application-land.
85  *     Basically, any symbols defined in the include/directory must be 
86  *     like this (plus some other globals). 
87  *
88  *     UNIX coders should just think of it as a special way to say "extern".
89  *
90  *   * If you build the DLL, define the DLL_EXPORT symbol so that all symbols
91  *     actually get exported by this file.
92
93  *   * If you do a static windows compilation, define DLL_STATIC, both when
94  *     compiling the application files and when compiling the library.
95  *
96  *   * If you link your application against the DLL or if you do a UNIX build,
97  *     don't do anything special. This file will do the right thing for you 
98  *     by default.
99  *
100  * 
101  * Rational of XBT_EXPORT_NO_IMPORT: (windows-only cruft)
102  *   * Symbols which must be exported in the DLL, but not imported from it.
103  * 
104  *   * This is obviously useful for initialized globals (which cannot be 
105  *     extern or similar).
106  *   * This is also used in the log mecanism where a macro creates the 
107  *     variable automatically. When the macro is called from within SimGrid,
108  *     the symbol must be exported, but when called  from within the client
109  *     code, it must not try to retrieve the symbol from the DLL since it's
110  *      not in there.
111  * 
112  * Rational of XBT_IMPORT_NO_EXPORT: (windows-only cruft)
113  *   * Symbols which must be imported from the DLL, but not explicitely 
114  *     exported from it.
115  * 
116  *   * The root log category is already exported, but not imported explicitely 
117  *     when creating a subcategory since we cannot import the parent category 
118  *     to deal with the fact that the parent may be in application space, not 
119  *     DLL space.
120  */
121
122
123 /* Build the DLL */
124 #if defined(DLL_EXPORT)
125 #  define XBT_PUBLIC(type)            __declspec(dllexport) type
126 #  define XBT_EXPORT_NO_IMPORT(type)  __declspec(dllexport) type
127 #  define XBT_IMPORT_NO_EXPORT(type)  type
128 #  define XBT_PUBLIC_DATA(type)       __declspec(dllexport) type
129
130 /* Pack everything up statically */
131 #elif defined(DLL_STATIC)
132 #  define XBT_PUBLIC(type)           extern type
133 #  define XBT_EXPORT_NO_IMPORT(type)  type
134 #  define XBT_IMPORT_NO_EXPORT(type)  type
135 #  define XBT_PUBLIC_DATA(type)       extern type
136
137
138 /* Link against the DLL */
139 #elif (defined(_WIN32) && !defined(DLL_EXPORT) && !defined(DLL_STATIC))
140 #  define XBT_PUBLIC(type)              __declspec(dllimport) type
141 #  define XBT_EXPORT_NO_IMPORT(type)    type
142 #  define XBT_IMPORT_NO_EXPORT(type)    __declspec(dllimport) type
143 #  define XBT_PUBLIC_DATA(type)         __declspec(dllimport) type
144
145 /* UNIX build */
146 #else
147 #  define XBT_PUBLIC(type)            extern type
148 #  define XBT_EXPORT_NO_IMPORT(type)  type
149 #  define XBT_IMPORT_NO_EXPORT(type)  type
150 #  define XBT_PUBLIC_DATA(type)       extern type
151 #endif
152
153 #if !defined (max) && !defined(__cplusplus)
154 #  define max(a,b)      (((a) > (b)) ? (a) : (b))
155 #endif
156 #if !defined (min) && !defined(__cplusplus)
157 #  define min(a,b)      (((a) < (b)) ? (a) : (b))
158 #endif
159
160 #define TRUE  1
161 #define FALSE 0
162
163 #define XBT_MAX_CHANNEL 10      /* FIXME: killme */
164 /*! C++ users need love */
165 #ifndef SG_BEGIN_DECL
166 # ifdef __cplusplus
167 #  define SG_BEGIN_DECL() extern "C" {
168 # else
169 #  define SG_BEGIN_DECL()
170 # endif
171 #endif
172
173 #ifndef SG_END_DECL
174 # ifdef __cplusplus
175 #  define SG_END_DECL() }
176 # else
177 #  define SG_END_DECL()
178 # endif
179 #endif
180 /* End of cruft for C++ */
181
182 SG_BEGIN_DECL()
183
184 XBT_PUBLIC(const char *) xbt_procname(void);
185
186 #define XBT_BACKTRACE_SIZE 10   /* FIXME: better place? Do document */
187
188 SG_END_DECL()
189 #endif /* XBT_MISC_H */