macros.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Macros Header - Various useful macro definitions
  5. This source is placed in the Public Domain, do with it what you will
  6. It was originally written by Brian C. White.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Private header
  10. #ifndef MACROS_H
  11. #define MACROS_H
  12. // MIN_VAL(SINT16) will return -0x8000 and MAX_VAL(SINT16) = 0x7FFF
  13. #define MIN_VAL(t) (((t)(-1) > 0) ? (t)( 0) : (t)(((1L<<(sizeof(t)*8-1)) )))
  14. #define MAX_VAL(t) (((t)(-1) > 0) ? (t)(-1) : (t)(((1L<<(sizeof(t)*8-1))-1)))
  15. // Min/Max functions
  16. #if !defined(MIN)
  17. #if defined(__HIGHC__)
  18. #define MIN(x,y) _min(x,y)
  19. #define MAX(x,y) _max(x,y)
  20. #endif
  21. // GNU C++ has a min/max operator <coolio>
  22. #if defined(__GNUG__)
  23. #define MIN(A,B) ((A) <? (B))
  24. #define MAX(A,B) ((A) >? (B))
  25. #endif
  26. /* Templates tend to mess up existing code that uses min/max because of the
  27. strict matching requirements */
  28. #if !defined(MIN)
  29. #define MIN(A,B) ((A) < (B)?(A):(B))
  30. #define MAX(A,B) ((A) > (B)?(A):(B))
  31. #endif
  32. #endif
  33. /* Bound functions, bound will return the value b within the limits a-c
  34. bounv will change b so that it is within the limits of a-c. */
  35. #define _bound(a,b,c) MIN(c,MAX(b,a))
  36. #define _boundv(a,b,c) b = _bound(a,b,c)
  37. #define ABS(a) (((a) < (0)) ?-(a) : (a))
  38. /* Useful count macro, use on an array of things and it will return the
  39. number of items in the array */
  40. #define _count(a) (sizeof(a)/sizeof(a[0]))
  41. // Flag Macros
  42. #define FLAG(f) (1L << (f))
  43. #define SETFLAG(v,f) ((v) |= FLAG(f))
  44. #define CLRFLAG(v,f) ((v) &=~FLAG(f))
  45. #define CHKFLAG(v,f) ((v) & FLAG(f) ? true : false)
  46. #ifdef __GNUC__
  47. #define APT_GCC_VERSION (__GNUC__ << 8 | __GNUC_MINOR__)
  48. #else
  49. #define APT_GCC_VERSION 0
  50. #endif
  51. /* likely() and unlikely() can be used to mark boolean expressions
  52. as (not) likely true which will help the compiler to optimise */
  53. #if APT_GCC_VERSION >= 0x0300
  54. #define likely(x) __builtin_expect (!!(x), 1)
  55. #define unlikely(x) __builtin_expect (!!(x), 0)
  56. #else
  57. #define likely(x) (x)
  58. #define unlikely(x) (x)
  59. #endif
  60. #if APT_GCC_VERSION >= 0x0300
  61. #define APT_DEPRECATED __attribute__ ((deprecated))
  62. #define APT_DEPRECATED_MSG(X) __attribute__ ((deprecated(X)))
  63. #define APT_CONST __attribute__((const))
  64. #define APT_PURE __attribute__((pure))
  65. #define APT_NORETURN __attribute__((noreturn))
  66. #define APT_PRINTF(n) __attribute__((format(printf, n, n + 1)))
  67. #define APT_WEAK __attribute__((weak));
  68. #else
  69. #define APT_DEPRECATED
  70. #define APT_DEPRECATED_MSG
  71. #define APT_CONST
  72. #define APT_PURE
  73. #define APT_NORETURN
  74. #define APT_PRINTF(n)
  75. #define APT_WEAK
  76. #endif
  77. #if APT_GCC_VERSION > 0x0302
  78. #define APT_NONNULL(...) __attribute__((nonnull(__VA_ARGS__)))
  79. #define APT_MUSTCHECK __attribute__((warn_unused_result))
  80. #else
  81. #define APT_NONNULL(...)
  82. #define APT_MUSTCHECK
  83. #endif
  84. #if APT_GCC_VERSION >= 0x0400
  85. #define APT_SENTINEL __attribute__((sentinel))
  86. #define APT_PUBLIC __attribute__ ((visibility ("default")))
  87. #define APT_HIDDEN __attribute__ ((visibility ("hidden")))
  88. #else
  89. #define APT_SENTINEL
  90. #define APT_PUBLIC
  91. #define APT_HIDDEN
  92. #endif
  93. // cold functions are unlikely() to be called
  94. #if APT_GCC_VERSION >= 0x0403
  95. #define APT_COLD __attribute__ ((__cold__))
  96. #define APT_HOT __attribute__ ((__hot__))
  97. #else
  98. #define APT_COLD
  99. #define APT_HOT
  100. #endif
  101. #ifndef APT_10_CLEANER_HEADERS
  102. #if APT_GCC_VERSION >= 0x0300
  103. #define __must_check __attribute__ ((warn_unused_result))
  104. #define __deprecated __attribute__((deprecated))
  105. #define __attrib_const __attribute__ ((__const__))
  106. #define __like_printf(n) __attribute__((format(printf, n, n + 1)))
  107. #else
  108. #define __must_check /* no warn_unused_result */
  109. #define __deprecated /* no deprecated */
  110. #define __attrib_const /* no const attribute */
  111. #define __like_printf(n) /* no like-printf */
  112. #endif
  113. #if APT_GCC_VERSION >= 0x0403
  114. #define __cold __attribute__ ((__cold__))
  115. #define __hot __attribute__ ((__hot__))
  116. #else
  117. #define __cold /* no cold marker */
  118. #define __hot /* no hot marker */
  119. #endif
  120. #endif
  121. #if __GNUC__ >= 4
  122. #define APT_IGNORE_DEPRECATED_PUSH \
  123. _Pragma("GCC diagnostic push") \
  124. _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  125. #define APT_IGNORE_DEPRECATED_POP \
  126. _Pragma("GCC diagnostic pop")
  127. #define APT_IGNORE_DEPRECATED(XXX) \
  128. APT_IGNORE_DEPRECATED_PUSH \
  129. XXX \
  130. APT_IGNORE_DEPRECATED_POP
  131. #else
  132. #define APT_IGNORE_DEPRECATED_PUSH
  133. #define APT_IGNORE_DEPRECATED_POP
  134. #define APT_IGNORE_DEPRECATED(XXX) XXX
  135. #endif
  136. #if __cplusplus >= 201103L
  137. #define APT_OVERRIDE override
  138. #else
  139. #define APT_OVERRIDE /* no c++11 standard */
  140. #endif
  141. // These lines are extracted by the makefiles and the buildsystem
  142. // Increasing MAJOR or MINOR results in the need of recompiling all
  143. // reverse-dependencies of libapt-pkg against the new SONAME.
  144. // Non-ABI-Breaks should only increase RELEASE number.
  145. // See also buildlib/libversion.mak
  146. #define APT_PKG_MAJOR 5
  147. #define APT_PKG_MINOR 0
  148. #define APT_PKG_RELEASE 1
  149. #define APT_PKG_ABI ((APT_PKG_MAJOR * 100) + APT_PKG_MINOR)
  150. #endif