00001 /* 00002 * Copyright (c) 2003, 2006, 2008, 2009 00003 * The President and Fellows of Harvard College. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the University nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00023 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00024 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00027 * SUCH DAMAGE. 00028 */ 00029 00030 #ifndef _CDEFS_H_ 00031 #define _CDEFS_H_ 00032 00033 /* 00034 * Some miscellaneous C language definitions and related matters. 00035 */ 00036 00037 00038 /* 00039 * Build-time assertion. Doesn't generate any code. The error message 00040 * on failure is less than ideal, but you can't have everything. 00041 */ 00042 #define COMPILE_ASSERT(x) ((void)sizeof(struct { unsigned : ((x)?1:-1); })) 00043 00044 00045 /* 00046 * Tell GCC how to check printf formats. 00047 */ 00048 #ifdef __GNUC__ 00049 #define __PF(a,b) __attribute__((__format__(__printf__, a, b))) 00050 #else 00051 #define __PF(a,b) 00052 #endif 00053 00054 00055 /* 00056 * Material for supporting inline functions. 00057 * 00058 * A function marked inline can be handled by the compiler in three 00059 * ways: in addition to possibly inlining into the code for other 00060 * functions, the compiler can (1) generate a file-static out-of-line 00061 * copy of the function, (2) generate a global out-of-line copy of the 00062 * function, or (3) generate no out-of-line copy of the function. 00063 * 00064 * None of these alone is thoroughly satisfactory. Since an inline 00065 * function may or may not be inlined at the compiler's discretion, if 00066 * no out-of-line copy exists the build may fail at link time with 00067 * undefined symbols. Meanwhile, if the compiler is told to generate a 00068 * global out-of-line copy, it will generate one such copy for every 00069 * source file where the inline definition is visible; since inline 00070 * functions tend to appear in header files, this leads to multiply 00071 * defined symbols and build failure. The file-static option isn't 00072 * really an improvement, either: one tends to get compiler warnings 00073 * about inline functions that haven't been used, which for any 00074 * particular source file tends to be at least some of the ones that 00075 * have been defined. Furthermore, this method leads to one 00076 * out-of-line copy of the inline function per source file that uses 00077 * it, which not only wastes space but makes debugging painful. 00078 * 00079 * Therefore, we use the following scheme. 00080 * 00081 * In the header file containing the inline functions for the module 00082 * "foo", we put 00083 * 00084 * #ifndef FOO_INLINE 00085 * #define FOO_INLINE INLINE 00086 * #endif 00087 * 00088 * where INLINE selects the compiler behavior that does *not* generate 00089 * an out-of-line version. Then we define the inline functions 00090 * themselves as FOO_INLINE. This allows the compiler to inline the 00091 * functions anywhere it sees fit with a minimum of hassles. Then, 00092 * when compiling foo.c, before including headers we put 00093 * 00094 * #define FOO_INLINE // empty 00095 * 00096 * which causes the inline functions to appear as ordinary function 00097 * definitions, not inline at all, when foo.c is compiled. This 00098 * ensures that an out-of-line definition appears, and furthermore 00099 * ensures that the out-of-line definition is the same as the inline 00100 * definition. 00101 * 00102 * The situation is complicated further because gcc is not compliant 00103 * with the C standard. In C99, "inline" means "do not generate an 00104 * out-of-line copy" and "extern inline" means "generate a global 00105 * out-of-line copy". In gcc, the meanings are reversed. In gcc 00106 * versions later than the one OS/161 currently uses, the standard 00107 * behavior can be requested; if so, __GNUC_STDC_INLINE__ is defined. 00108 * There does not appear to be any way to select this behavior with 00109 * gcc 4.1; however, the following definitions should be future-proof. 00110 * 00111 * (Note that inline functions that appear only within a single source 00112 * file can safely be declared "static inline".) 00113 */ 00114 #if defined(__GNUC__) && !defined(__GNUC_STDC_INLINE__) 00115 /* gcc's non-C99 inline semantics */ 00116 #define INLINE extern inline 00117 00118 #elif defined(__STDC__) && __STDC_VERSION__ >= 199901L 00119 /* C99 */ 00120 #define INLINE inline 00121 00122 #else 00123 /* something else; static inline is safest */ 00124 #define INLINE static inline 00125 #endif 00126 00127 00128 #endif /* _CDEFS_H_ */