cdefs.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2003, 2006, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #ifndef _CDEFS_H_
  30. #define _CDEFS_H_
  31. /*
  32. * Some miscellaneous C language definitions and related matters.
  33. */
  34. /*
  35. * Build-time assertion. Doesn't generate any code. The error message
  36. * on failure is less than ideal, but you can't have everything.
  37. */
  38. #define COMPILE_ASSERT(x) ((void)sizeof(struct { unsigned : ((x)?1:-1); }))
  39. /*
  40. * Tell GCC how to check printf formats.
  41. */
  42. #ifdef __GNUC__
  43. #define __PF(a,b) __attribute__((__format__(__printf__, a, b)))
  44. #else
  45. #define __PF(a,b)
  46. #endif
  47. /*
  48. * Material for supporting inline functions.
  49. *
  50. * A function marked inline can be handled by the compiler in three
  51. * ways: in addition to possibly inlining into the code for other
  52. * functions, the compiler can (1) generate a file-static out-of-line
  53. * copy of the function, (2) generate a global out-of-line copy of the
  54. * function, or (3) generate no out-of-line copy of the function.
  55. *
  56. * None of these alone is thoroughly satisfactory. Since an inline
  57. * function may or may not be inlined at the compiler's discretion, if
  58. * no out-of-line copy exists the build may fail at link time with
  59. * undefined symbols. Meanwhile, if the compiler is told to generate a
  60. * global out-of-line copy, it will generate one such copy for every
  61. * source file where the inline definition is visible; since inline
  62. * functions tend to appear in header files, this leads to multiply
  63. * defined symbols and build failure. The file-static option isn't
  64. * really an improvement, either: one tends to get compiler warnings
  65. * about inline functions that haven't been used, which for any
  66. * particular source file tends to be at least some of the ones that
  67. * have been defined. Furthermore, this method leads to one
  68. * out-of-line copy of the inline function per source file that uses
  69. * it, which not only wastes space but makes debugging painful.
  70. *
  71. * Therefore, we use the following scheme.
  72. *
  73. * In the header file containing the inline functions for the module
  74. * "foo", we put
  75. *
  76. * #ifndef FOO_INLINE
  77. * #define FOO_INLINE INLINE
  78. * #endif
  79. *
  80. * where INLINE selects the compiler behavior that does *not* generate
  81. * an out-of-line version. Then we define the inline functions
  82. * themselves as FOO_INLINE. This allows the compiler to inline the
  83. * functions anywhere it sees fit with a minimum of hassles. Then,
  84. * when compiling foo.c, before including headers we put
  85. *
  86. * #define FOO_INLINE // empty
  87. *
  88. * which causes the inline functions to appear as ordinary function
  89. * definitions, not inline at all, when foo.c is compiled. This
  90. * ensures that an out-of-line definition appears, and furthermore
  91. * ensures that the out-of-line definition is the same as the inline
  92. * definition.
  93. *
  94. * The situation is complicated further because gcc is not compliant
  95. * with the C standard. In C99, "inline" means "do not generate an
  96. * out-of-line copy" and "extern inline" means "generate a global
  97. * out-of-line copy". In gcc, the meanings are reversed. In gcc
  98. * versions later than the one OS/161 currently uses, the standard
  99. * behavior can be requested; if so, __GNUC_STDC_INLINE__ is defined.
  100. * There does not appear to be any way to select this behavior with
  101. * gcc 4.1; however, the following definitions should be future-proof.
  102. *
  103. * (Note that inline functions that appear only within a single source
  104. * file can safely be declared "static inline".)
  105. */
  106. #if defined(__GNUC__) && !defined(__GNUC_STDC_INLINE__)
  107. /* gcc's non-C99 inline semantics */
  108. #define INLINE extern inline
  109. #elif defined(__STDC__) && __STDC_VERSION__ >= 199901L
  110. /* C99 */
  111. #define INLINE inline
  112. #else
  113. /* something else; static inline is safest */
  114. #define INLINE static inline
  115. #endif
  116. #endif /* _CDEFS_H_ */