Logo AND Algorithmique Numérique Distribuée

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