/src/lvm2/libdm/dm-tools/util.h
Line  | Count  | Source (jump to first uncovered line)  | 
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 _LVM_UTIL_H  | 
16  |  | #define _LVM_UTIL_H  | 
17  |  |  | 
18  |  | #include "libdm/libdevmapper.h"  | 
19  |  |  | 
20  |  | /* Use wrapper for checked results */  | 
21  |  | static inline __attribute__((warn_unused_result))  | 
22  |  |   int _dm_strncpy(char *dest, const char *src, size_t n)  | 
23  | 0  | { | 
24  | 0  |   return dm_strncpy(dest, src, n);  | 
25  | 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  | 
26  |  |  | 
27  |  | #define min(a, b) ({ typeof(a) _a = (a); \ | 
28  |  |          typeof(b) _b = (b); \  | 
29  |  |          (void) (&_a == &_b); \  | 
30  |  |          _a < _b ? _a : _b; })  | 
31  |  |  | 
32  |  | #define max(a, b) ({ typeof(a) _a = (a); \ | 
33  |  |          typeof(b) _b = (b); \  | 
34  |  |          (void) (&_a == &_b); \  | 
35  |  |          _a > _b ? _a : _b; })  | 
36  |  |  | 
37  |  | #define is_power_of_2(n) ((n) && !((n) & ((n) - 1)))  | 
38  |  |  | 
39  |  | #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 6)  | 
40  |  | #define uninitialized_var(x) x  | 
41  |  | #else  | 
42  |  | #define uninitialized_var(x) x = x  | 
43  |  | #endif  | 
44  |  |  | 
45  | 0  | #define KERNEL_VERSION(major, minor, release) (((major) << 16) + ((minor) << 8) + (release))  | 
46  |  |  | 
47  |  | /* Define some portable printing types */  | 
48  |  | #define PRIsize_t "zu"  | 
49  |  | #define PRIssize_t "zd"  | 
50  |  | #define PRIptrdiff_t "td"  | 
51  |  | #define PRIpid_t PRId32  | 
52  |  |  | 
53  |  | /* For convenience */  | 
54  |  | #define FMTsize_t "%" PRIsize_t  | 
55  |  | #define FMTssize_t "%" PRIssize_t  | 
56  |  | #define FMTptrdiff_t "%" PRIptrdiff_t  | 
57  |  | #define FMTpid_t "%" PRIpid_t  | 
58  |  |  | 
59  |  | #define FMTd8  "%" PRId8  | 
60  |  | #define FMTd16 "%" PRId16  | 
61  |  | #define FMTd32 "%" PRId32  | 
62  |  | #define FMTd64 "%" PRId64  | 
63  |  |  | 
64  |  | #define FMTi8  "%" PRIi8  | 
65  |  | #define FMTi16 "%" PRIi16  | 
66  |  | #define FMTi32 "%" PRIi32  | 
67  |  | #define FMTi64 "%" PRIi64  | 
68  |  |  | 
69  |  | #define FMTo8  "%" PRIo8  | 
70  |  | #define FMTo16 "%" PRIo16  | 
71  |  | #define FMTo32 "%" PRIo32  | 
72  |  | #define FMTo64 "%" PRIo64  | 
73  |  |  | 
74  |  | #define FMTu8  "%" PRIu8  | 
75  |  | #define FMTu16 "%" PRIu16  | 
76  | 0  | #define FMTu32 "%" PRIu32  | 
77  |  | #define FMTu64 "%" PRIu64  | 
78  |  |  | 
79  |  | #define FMTx8  "%" PRIx8  | 
80  |  | #define FMTx16 "%" PRIx16  | 
81  |  | #define FMTx32 "%" PRIx32  | 
82  |  | #define FMTx64 "%" PRIx64  | 
83  |  |  | 
84  |  | #define FMTVGID "%." DM_TO_STRING(ID_LEN) "s"  | 
85  |  |  | 
86  |  | /*  | 
87  |  |  * GCC 3.4 adds a __builtin_clz, which uses the count leading zeros (clz)  | 
88  |  |  * instruction on arches that have one. Provide a fallback using shifts  | 
89  |  |  * and comparisons for older compilers.  | 
90  |  |  */  | 
91  |  | #ifdef HAVE___BUILTIN_CLZ  | 
92  | 0  | #define clz(x) __builtin_clz((x))  | 
93  |  | #else /* ifdef HAVE___BUILTIN_CLZ */  | 
94  |  | static unsigned _dm_clz(unsigned x)  | 
95  |  | { | 
96  |  |   int n;  | 
97  |  |  | 
98  |  |   if ((int)x <= 0) return (~x >> 26) & 32;  | 
99  |  |  | 
100  |  |   n = 1;  | 
101  |  |  | 
102  |  |   if ((x >> 16) == 0) { | 
103  |  |     n = n + 16;  | 
104  |  |     x = x << 16;  | 
105  |  |   }  | 
106  |  |  | 
107  |  |   if ((x >> 24) == 0) { | 
108  |  |     n = n + 8;  | 
109  |  |     x = x << 8;  | 
110  |  |   }  | 
111  |  |  | 
112  |  |   if ((x >> 28) == 0) { | 
113  |  |     n = n + 4;  | 
114  |  |     x = x << 4;  | 
115  |  |   }  | 
116  |  |  | 
117  |  |   if ((x >> 30) == 0) { | 
118  |  |     n = n + 2;  | 
119  |  |     x = x << 2;  | 
120  |  |   }  | 
121  |  |   n = n - (x >> 31);  | 
122  |  |   return n;  | 
123  |  | }  | 
124  |  | #define clz(x) _dm_clz((x))  | 
125  |  | #endif /* ifdef HAVE___BUILTIN_CLZ */  | 
126  |  |  | 
127  |  | #endif  |