Logo AND Algorithmique Numérique Distribuée

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