Coverage Report

Created: 2026-05-30 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/lvm2/libdm/dm-tools/util.h
Line
Count
Source
1
/*
2
 * Copyright (C) 2007 Red Hat, Inc. All rights reserved.
3
 *
4
 * This file is part of LVM2.
5
 *
6
 * This copyrighted material is made available to anyone wishing to use,
7
 * modify, copy, or redistribute it subject to the terms and conditions
8
 * of the GNU Lesser General Public License v.2.1.
9
 *
10
 * You should have received a copy of the GNU Lesser General Public License
11
 * along with this program; if not, write to the Free Software Foundation,
12
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
13
 */
14
15
#ifndef DM_TOOLS_UTIL_H
16
#define DM_TOOLS_UTIL_H
17
18
#include "libdm/libdevmapper.h"
19
20
/*
21
 * GCC -fanalyzer has limitations in certain code patterns.
22
 */
23
#if defined(__GNUC__) && (__GNUC__ >= 10) && !defined(__clang__)
24
# define GCC_SUPPRESS_FD_WARNINGS \
25
  _Pragma("GCC diagnostic push") \
26
  _Pragma("GCC diagnostic ignored \"-Wanalyzer-fd-leak\"") \
27
  _Pragma("GCC diagnostic ignored \"-Wanalyzer-fd-use-without-check\"")
28
29
# define GCC_SUPPRESS_LEAK_WARNING \
30
  _Pragma("GCC diagnostic push") \
31
  _Pragma("GCC diagnostic ignored \"-Wanalyzer-malloc-leak\"")
32
33
# define GCC_SUPPRESS_BUFFER_WARNING \
34
  _Pragma("GCC diagnostic push") \
35
  _Pragma("GCC diagnostic ignored \"-Wanalyzer-out-of-bounds\"")
36
37
# define GCC_UNSUPPRESS_WARNINGS \
38
  _Pragma("GCC diagnostic pop")
39
#else
40
# define GCC_SUPPRESS_FD_WARNINGS
41
# define GCC_SUPPRESS_LEAK_WARNING
42
# define GCC_SUPPRESS_BUFFER_WARNING
43
# define GCC_UNSUPPRESS_WARNINGS
44
#endif
45
46
#define min(a, b) ({ __typeof__(a) _a = (a); \
47
         __typeof__(b) _b = (b); \
48
         (void) (&_a == &_b); \
49
         _a < _b ? _a : _b; })
50
51
#define max(a, b) ({ __typeof__(a) _a = (a); \
52
         __typeof__(b) _b = (b); \
53
         (void) (&_a == &_b); \
54
         _a > _b ? _a : _b; })
55
56
#define is_power_of_2(n) ((n) && !((n) & ((n) - 1)))
57
58
/* Use wrapper for checked results */
59
static inline __attribute__((warn_unused_result))
60
  int _dm_strncpy(char *dest, const char *src, size_t n)
61
0
{
62
0
  return dm_strncpy(dest, src, n);
63
0
}
Unexecuted instantiation: libdm-common.c:_dm_strncpy
Unexecuted instantiation: libdm-file.c:_dm_strncpy
Unexecuted instantiation: libdm-string.c:_dm_strncpy
Unexecuted instantiation: dbg_malloc.c:_dm_strncpy
Unexecuted instantiation: pool.c:_dm_strncpy
Unexecuted instantiation: libdm-iface.c:_dm_strncpy
Unexecuted instantiation: bitset.c:_dm_strncpy
Unexecuted instantiation: list.c:_dm_strncpy
Unexecuted instantiation: libdm-timestamp.c:_dm_strncpy
64
65
/*
66
 * GCC 3.4 adds a __builtin_clz, which uses the count leading zeros (clz)
67
 * instruction on arches that have one. Provide a fallback using shifts
68
 * and comparisons for older compilers.
69
 */
70
#ifdef HAVE___BUILTIN_CLZ
71
0
#define clz(x) ((x) ? __builtin_clz((x)) : 32)
72
#else /* ifdef HAVE___BUILTIN_CLZ */
73
static inline unsigned _dm_clz(unsigned x)
74
{
75
  unsigned n;
76
77
  /* branchless: if (!x) return 32; if (x >> 31) return 0; */
78
  if ((int)x <= 0) return (~x >> 26) & 32;
79
80
  n = 1;
81
82
  if ((x >> 16) == 0) {
83
    n = n + 16;
84
    x = x << 16;
85
  }
86
87
  if ((x >> 24) == 0) {
88
    n = n + 8;
89
    x = x << 8;
90
  }
91
92
  if ((x >> 28) == 0) {
93
    n = n + 4;
94
    x = x << 4;
95
  }
96
97
  if ((x >> 30) == 0) {
98
    n = n + 2;
99
    x = x << 2;
100
  }
101
  n = n - (x >> 31);
102
  return n;
103
}
104
#define clz(x) _dm_clz((x))
105
#endif /* ifdef HAVE___BUILTIN_CLZ */
106
107
#ifdef HAVE___BUILTIN_CLZLL
108
#define clzll(x) ((x) ? __builtin_clzll((x)) : 64)
109
#else /* ifdef HAVE___BUILTIN_CLZLL */
110
static inline unsigned _dm_clzll(unsigned long long x)
111
{
112
  if (x <= 0xffffffff)
113
    return 32 + clz((unsigned) (x & 0xffffffff));
114
115
  return clz((unsigned)(x >> 32));
116
}
117
#define clzll(x) _dm_clzll((x))
118
#endif /* ifdef HAVE___BUILTIN_CLZLL */
119
120
#ifndef HAVE_FFS
121
#ifdef HAVE___BUILTIN_FFS
122
#define ffs(x) __builtin_ffs((x))
123
#else
124
#error ffs() not implemented!
125
#endif /* ifdef HAVE___BUILTIN_FFS */
126
#endif /* ifndef HAVE_FFS */
127
128
0
#define KERNEL_VERSION(major, minor, release) (((major) << 16) + ((minor) << 8) + (release))
129
130
/* Define some portable printing types */
131
#define PRIsize_t "zu"
132
#define PRIssize_t "zd"
133
#define PRIptrdiff_t "td"
134
#define PRIpid_t PRId32
135
136
/* For convenience */
137
#define FMTsize_t "%" PRIsize_t
138
#define FMTssize_t "%" PRIssize_t
139
#define FMTptrdiff_t "%" PRIptrdiff_t
140
#define FMTpid_t "%" PRIpid_t
141
142
#define FMTd8  "%" PRId8
143
#define FMTd16 "%" PRId16
144
#define FMTd32 "%" PRId32
145
#define FMTd64 "%" PRId64
146
147
#define FMTi8  "%" PRIi8
148
#define FMTi16 "%" PRIi16
149
#define FMTi32 "%" PRIi32
150
#define FMTi64 "%" PRIi64
151
152
#define FMTo8  "%" PRIo8
153
#define FMTo16 "%" PRIo16
154
#define FMTo32 "%" PRIo32
155
#define FMTo64 "%" PRIo64
156
157
#define FMTu8  "%" PRIu8
158
#define FMTu16 "%" PRIu16
159
0
#define FMTu32 "%" PRIu32
160
#define FMTu64 "%" PRIu64
161
162
#define FMTx8  "%" PRIx8
163
#define FMTx16 "%" PRIx16
164
#define FMTx32 "%" PRIx32
165
#define FMTx64 "%" PRIx64
166
167
#define FMTVGID "%." DM_TO_STRING(ID_LEN) "s"
168
169
#endif