Logo AND Algorithmique Numérique Distribuée

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