apt: apt-pkg/contrib/macros.h Source File

apt 3.3.2 commandline package manager

macros.h
1// -*- mode: cpp; mode: fold -*-
2// SPDX-License-Identifier: GPL-2.0+
3// Description /*{{{*/
4/* ######################################################################
5
6 Macros Header - Various useful macro definitions
7
8 This file had this historic note, but now includes further changes
9 under the GPL-2.0+:
10
11 This source is placed in the Public Domain, do with it what you will
12 It was originally written by Brian C. White.
13
14 ##################################################################### */
15 /*}}}*/
16// Private header
17#ifndef MACROS_H
18#define MACROS_H
19
20#ifdef __GNUC__
21#define APT_GCC_VERSION (__GNUC__ << 8 | __GNUC_MINOR__)
22#else
23#define APT_GCC_VERSION 0
24#endif
25
26#ifdef APT_COMPILING_APT
27/* likely() and unlikely() can be used to mark boolean expressions
28 as (not) likely true which will help the compiler to optimise */
29#if APT_GCC_VERSION >= 0x0300
30 #define likely(x) __builtin_expect (!!(x), 1)
31 #define unlikely(x) __builtin_expect (!!(x), 0)
32#else
33 #define likely(x) (x)
34 #define unlikely(x) (x)
35#endif
36
37// Asserts that the result of expr is true
38#define must_succeed(expr) ({ \
39 auto result = (expr); \
40 if (unlikely(not result)) \
41 fprintf(stderr, "%s:%d: %s: Assertion `%s` failed.\n", __FILE__, __LINE__, __FUNCTION__, #expr), abort(); \
42 result; \
43})
44#endif
45
46#if APT_GCC_VERSION >= 0x0300
47 // __attribute__((const)) is too dangerous for us, we end up using it wrongly
48 #define APT_PURE __attribute__((pure))
49 #define APT_PRINTF(n) __attribute__((format(printf, n, n + 1)))
50 #define APT_WEAK __attribute__((weak));
51#else
52 #define APT_PURE
53 #define APT_PRINTF(n)
54 #define APT_WEAK
55#endif
56
57#if APT_GCC_VERSION > 0x0302
58 #define APT_NONNULL(...) __attribute__((nonnull(__VA_ARGS__)))
59#else
60 #define APT_NONNULL(...)
61#endif
62
63#if APT_GCC_VERSION >= 0x0400
64 #define APT_SENTINEL __attribute__((sentinel))
65 #define APT_PUBLIC __attribute__ ((visibility ("default")))
66 #define APT_HIDDEN __attribute__ ((visibility ("hidden")))
67#else
68 #define APT_SENTINEL
69 #define APT_PUBLIC
70 #define APT_HIDDEN
71#endif
72
73// cold functions are unlikely() to be called
74#if APT_GCC_VERSION >= 0x0403
75 #define APT_COLD __attribute__ ((__cold__))
76 #define APT_HOT __attribute__ ((__hot__))
77#else
78 #define APT_COLD
79 #define APT_HOT
80#endif
81
82
83#if __GNUC__ >= 4
84 #define APT_IGNORE_DEPRECATED_PUSH \
85 _Pragma("GCC diagnostic push") \
86 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
87 #define APT_IGNORE_DEPRECATED_POP \
88 _Pragma("GCC diagnostic pop")
89#else
90 #define APT_IGNORE_DEPRECATED_PUSH
91 #define APT_IGNORE_DEPRECATED_POP
92#endif
93
94// These lines are extracted by the makefiles and the buildsystem
95// Increasing MAJOR or MINOR results in the need of recompiling all
96// reverse-dependencies of libapt-pkg against the new SONAME.
97// Non-ABI-Breaks should only increase RELEASE number.
98// See also buildlib/libversion.mak
99#define APT_PKG_MAJOR 7
100#define APT_PKG_MINOR 0
101#define APT_PKG_RELEASE 0
102#define APT_PKG_ABI ((APT_PKG_MAJOR * 100) + APT_PKG_MINOR)
103
104/* Should be a multiple of the common page size (4096) */
105static constexpr unsigned long long APT_BUFFER_SIZE = 64 * 1024;
106
107template <class F>
108 struct AptScopeWrapper {
109 F func;
110 ~AptScopeWrapper() { func(); }
111};
112template <class F>
113 AptScopeWrapper(F) -> AptScopeWrapper<F>;
114#define APT_PASTE2(a, b) a##b
115#define APT_PASTE(a, b) APT_PASTE2(a, b)
116#define DEFER(lambda) AptScopeWrapper APT_PASTE(defer, __LINE__){lambda};
117
118
119#ifndef APT_COMPILING_APT
120#endif
121#endif
AptScopeWrapper
Definition macros.h:108