t/nvmept_trim: increase transfer size for some tests
[fio.git] / oslib / libmtd_common.h
1 /*
2  * Copyright (c) Artem Bityutskiy, 2007, 2008
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12  * the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 /* Imported from mtd-utils by dehrenberg */
20
21 #ifndef __MTD_UTILS_COMMON_H__
22 #define __MTD_UTILS_COMMON_H__
23
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <features.h>
32 #include <inttypes.h>
33 #include <sys/sysmacros.h>
34
35 #ifndef PROGRAM_NAME
36 # error "You must define PROGRAM_NAME before including this header"
37 #endif
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 #ifndef MIN     /* some C lib headers define this for us */
44 #define MIN(a, b) ((a) < (b) ? (a) : (b))
45 #endif
46 #ifndef MAX
47 #define MAX(a, b) ((a) > (b) ? (a) : (b))
48 #endif
49 #define min(a, b) MIN(a, b) /* glue for linux kernel source */
50
51 #define ALIGN(x,a) __ALIGN_MASK(x,(__typeof__(x))(a)-1)
52 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
53
54 #define min_t(t,x,y) ({ \
55         __typeof__((x)) _x = (x); \
56         __typeof__((y)) _y = (y); \
57         (_x < _y) ? _x : _y; \
58 })
59
60 #define max_t(t,x,y) ({ \
61         __typeof__((x)) _x = (x); \
62         __typeof__((y)) _y = (y); \
63         (_x > _y) ? _x : _y; \
64 })
65
66 #ifndef O_CLOEXEC
67 #define O_CLOEXEC 0
68 #endif
69
70 /* define a print format specifier for off_t */
71 #ifdef __USE_FILE_OFFSET64
72 #define PRIxoff_t PRIx64
73 #define PRIdoff_t PRId64
74 #else
75 #define PRIxoff_t "l"PRIx32
76 #define PRIdoff_t "l"PRId32
77 #endif
78
79 /* Verbose messages */
80 #define bareverbose(verbose, fmt, ...) do {                        \
81         if (verbose)                                               \
82                 printf(fmt, ##__VA_ARGS__);                        \
83 } while(0)
84 #define verbose(verbose, fmt, ...) \
85         bareverbose(verbose, "%s: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__)
86
87 /* Normal messages */
88 #define normsg_cont(fmt, ...) do {                                 \
89         printf("%s: " fmt, PROGRAM_NAME, ##__VA_ARGS__);           \
90 } while(0)
91 #define normsg(fmt, ...) do {                                      \
92         normsg_cont(fmt "\n", ##__VA_ARGS__);                      \
93 } while(0)
94
95 /* Error messages */
96 #define errmsg(fmt, ...)  ({                                                \
97         fprintf(stderr, "%s: error!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \
98         -1;                                                                 \
99 })
100 #define errmsg_die(fmt, ...) do {                                           \
101         exit(errmsg(fmt, ##__VA_ARGS__));                                   \
102 } while(0)
103
104 /* System error messages */
105 #define sys_errmsg(fmt, ...)  ({                                            \
106         int _err = errno;                                                   \
107         errmsg(fmt, ##__VA_ARGS__);                                         \
108         fprintf(stderr, "%*serror %d (%s)\n", (int)sizeof(PROGRAM_NAME) + 1,\
109                 "", _err, strerror(_err));                                  \
110         -1;                                                                 \
111 })
112 #define sys_errmsg_die(fmt, ...) do {                                       \
113         exit(sys_errmsg(fmt, ##__VA_ARGS__));                               \
114 } while(0)
115
116 /* Warnings */
117 #define warnmsg(fmt, ...) do {                                                \
118         fprintf(stderr, "%s: warning!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \
119 } while(0)
120
121 static inline int is_power_of_2(unsigned long long n)
122 {
123         return (n != 0 && ((n & (n - 1)) == 0));
124 }
125
126 /**
127  * simple_strtoX - convert a hex/dec/oct string into a number
128  * @snum: buffer to convert
129  * @error: set to 1 when buffer isn't fully consumed
130  *
131  * These functions are similar to the standard strtoX() functions, but they are
132  * a little bit easier to use if you want to convert full string of digits into
133  * the binary form. The typical usage:
134  *
135  * int error = 0;
136  * unsigned long num;
137  *
138  * num = simple_strtoul(str, &error);
139  * if (error || ... if needed, your check that num is not out of range ...)
140  *      error_happened();
141  */
142 #define simple_strtoX(func, type) \
143 static inline type simple_##func(const char *snum, int *error) \
144 { \
145         char *endptr; \
146         type ret = func(snum, &endptr, 0); \
147  \
148         if (error && (!*snum || *endptr)) { \
149                 errmsg("%s: unable to parse the number '%s'", #func, snum); \
150                 *error = 1; \
151         } \
152  \
153         return ret; \
154 }
155 simple_strtoX(strtol, long int)
156 simple_strtoX(strtoll, long long int)
157 simple_strtoX(strtoul, unsigned long int)
158 simple_strtoX(strtoull, unsigned long long int)
159
160 /* Simple version-printing for utils */
161 #define common_print_version() \
162 do { \
163         printf("%s %s\n", PROGRAM_NAME, VERSION); \
164 } while (0)
165
166 #include "libmtd_xalloc.h"
167
168 #ifdef __cplusplus
169 }
170 #endif
171
172 #endif /* !__MTD_UTILS_COMMON_H__ */