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