Coverage Report

Created: 2026-07-16 06:59

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__ >= 13) && !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-use-after-free\"") \
36
  _Pragma("GCC diagnostic ignored \"-Wanalyzer-out-of-bounds\"")
37
38
# define GCC_UNSUPPRESS_WARNINGS \
39
  _Pragma("GCC diagnostic pop")
40
#else
41
# define GCC_SUPPRESS_FD_WARNINGS
42
# define GCC_SUPPRESS_LEAK_WARNING
43
# define GCC_SUPPRESS_BUFFER_WARNING
44
# define GCC_UNSUPPRESS_WARNINGS
45
#endif
46
47
#define min(a, b) ({ __typeof__(a) _a = (a); \
48
         __typeof__(b) _b = (b); \
49
         (void) (&_a == &_b); \
50
         _a < _b ? _a : _b; })
51
52
#define max(a, b) ({ __typeof__(a) _a = (a); \
53
         __typeof__(b) _b = (b); \
54
         (void) (&_a == &_b); \
55
         _a > _b ? _a : _b; })
56
57
#define is_power_of_2(n) ((n) && !((n) & ((n) - 1)))
58
59
/* Use wrapper for checked results */
60
static inline __attribute__((warn_unused_result))
61
  int _dm_strncpy(char *dest, const char *src, size_t n)
62
0
{
63
0
  return dm_strncpy(dest, src, n);
64
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
65
66
/*
67
 * GCC 3.4 adds a __builtin_clz, which uses the count leading zeros (clz)
68
 * instruction on arches that have one. Provide a fallback using shifts
69
 * and comparisons for older compilers.
70
 */
71
#ifdef HAVE___BUILTIN_CLZ
72
0
#define clz(x) ((x) ? __builtin_clz((x)) : 32)
73
#else /* ifdef HAVE___BUILTIN_CLZ */
74
static inline unsigned _dm_clz(unsigned x)
75
{
76
  unsigned n;
77
78
  /* branchless: if (!x) return 32; if (x >> 31) return 0; */
79
  if ((int)x <= 0) return (~x >> 26) & 32;
80
81
  n = 1;
82
83
  if ((x >> 16) == 0) {
84
    n = n + 16;
85
    x = x << 16;
86
  }
87
88
  if ((x >> 24) == 0) {
89
    n = n + 8;
90
    x = x << 8;
91
  }
92
93
  if ((x >> 28) == 0) {
94
    n = n + 4;
95
    x = x << 4;
96
  }
97
98
  if ((x >> 30) == 0) {
99
    n = n + 2;
100
    x = x << 2;
101
  }
102
  n = n - (x >> 31);
103
  return n;
104
}
105
#define clz(x) _dm_clz((x))
106
#endif /* ifdef HAVE___BUILTIN_CLZ */
107
108
#ifdef HAVE___BUILTIN_CLZLL
109
#define clzll(x) ((x) ? __builtin_clzll((x)) : 64)
110
#else /* ifdef HAVE___BUILTIN_CLZLL */
111
static inline unsigned _dm_clzll(unsigned long long x)
112
{
113
  if (x <= 0xffffffff)
114
    return 32 + clz((unsigned) (x & 0xffffffff));
115
116
  return clz((unsigned)(x >> 32));
117
}
118
#define clzll(x) _dm_clzll((x))
119
#endif /* ifdef HAVE___BUILTIN_CLZLL */
120
121
#ifndef HAVE_FFS
122
#ifdef HAVE___BUILTIN_FFS
123
#define ffs(x) __builtin_ffs((x))
124
#else
125
#error ffs() not implemented!
126
#endif /* ifdef HAVE___BUILTIN_FFS */
127
#endif /* ifndef HAVE_FFS */
128
129
0
#define KERNEL_VERSION(major, minor, release) (((major) << 16) + ((minor) << 8) + (release))
130
131
/* Define some portable printing types */
132
#define PRIsize_t "zu"
133
#define PRIssize_t "zd"
134
#define PRIptrdiff_t "td"
135
#define PRIpid_t PRId32
136
137
/* For convenience */
138
#define FMTsize_t "%" PRIsize_t
139
#define FMTssize_t "%" PRIssize_t
140
#define FMTptrdiff_t "%" PRIptrdiff_t
141
#define FMTpid_t "%" PRIpid_t
142
143
#define FMTd8  "%" PRId8
144
#define FMTd16 "%" PRId16
145
#define FMTd32 "%" PRId32
146
#define FMTd64 "%" PRId64
147
148
#define FMTi8  "%" PRIi8
149
#define FMTi16 "%" PRIi16
150
#define FMTi32 "%" PRIi32
151
#define FMTi64 "%" PRIi64
152
153
#define FMTo8  "%" PRIo8
154
#define FMTo16 "%" PRIo16
155
#define FMTo32 "%" PRIo32
156
#define FMTo64 "%" PRIo64
157
158
#define FMTu8  "%" PRIu8
159
#define FMTu16 "%" PRIu16
160
0
#define FMTu32 "%" PRIu32
161
#define FMTu64 "%" PRIu64
162
163
#define FMTx8  "%" PRIx8
164
#define FMTx16 "%" PRIx16
165
#define FMTx32 "%" PRIx32
166
#define FMTx64 "%" PRIx64
167
168
#define FMTVGID "%." DM_TO_STRING(ID_LEN) "s"
169
170
#endif