Restore type checking in calc_thread_status()
[fio.git] / oslib / libmtd_common.h
CommitLineData
94e9e396
DE
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
fa07eaa6 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
94e9e396
DE
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>
250e878a 33#include <sys/sysmacros.h>
94e9e396
DE
34
35#ifndef PROGRAM_NAME
36# error "You must define PROGRAM_NAME before including this header"
37#endif
38
39#ifdef __cplusplus
40extern "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#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
51
3376ecf4 52#define ALIGN(x,a) __ALIGN_MASK(x,(__typeof__(x))(a)-1)
94e9e396
DE
53#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
54
55#define min_t(t,x,y) ({ \
3376ecf4
KC
56 __typeof__((x)) _x = (x); \
57 __typeof__((y)) _y = (y); \
94e9e396
DE
58 (_x < _y) ? _x : _y; \
59})
60
61#define max_t(t,x,y) ({ \
3376ecf4
KC
62 __typeof__((x)) _x = (x); \
63 __typeof__((y)) _y = (y); \
94e9e396
DE
64 (_x > _y) ? _x : _y; \
65})
66
67#ifndef O_CLOEXEC
68#define O_CLOEXEC 0
69#endif
70
71/* define a print format specifier for off_t */
72#ifdef __USE_FILE_OFFSET64
73#define PRIxoff_t PRIx64
74#define PRIdoff_t PRId64
75#else
76#define PRIxoff_t "l"PRIx32
77#define PRIdoff_t "l"PRId32
78#endif
79
80/* Verbose messages */
81#define bareverbose(verbose, fmt, ...) do { \
82 if (verbose) \
83 printf(fmt, ##__VA_ARGS__); \
84} while(0)
85#define verbose(verbose, fmt, ...) \
86 bareverbose(verbose, "%s: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__)
87
88/* Normal messages */
89#define normsg_cont(fmt, ...) do { \
90 printf("%s: " fmt, PROGRAM_NAME, ##__VA_ARGS__); \
91} while(0)
92#define normsg(fmt, ...) do { \
93 normsg_cont(fmt "\n", ##__VA_ARGS__); \
94} while(0)
95
96/* Error messages */
97#define errmsg(fmt, ...) ({ \
98 fprintf(stderr, "%s: error!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \
99 -1; \
100})
101#define errmsg_die(fmt, ...) do { \
102 exit(errmsg(fmt, ##__VA_ARGS__)); \
103} while(0)
104
105/* System error messages */
106#define sys_errmsg(fmt, ...) ({ \
107 int _err = errno; \
108 errmsg(fmt, ##__VA_ARGS__); \
109 fprintf(stderr, "%*serror %d (%s)\n", (int)sizeof(PROGRAM_NAME) + 1,\
110 "", _err, strerror(_err)); \
111 -1; \
112})
113#define sys_errmsg_die(fmt, ...) do { \
114 exit(sys_errmsg(fmt, ##__VA_ARGS__)); \
115} while(0)
116
117/* Warnings */
118#define warnmsg(fmt, ...) do { \
119 fprintf(stderr, "%s: warning!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \
120} while(0)
121
94e9e396
DE
122static inline int is_power_of_2(unsigned long long n)
123{
124 return (n != 0 && ((n & (n - 1)) == 0));
125}
126
127/**
128 * simple_strtoX - convert a hex/dec/oct string into a number
129 * @snum: buffer to convert
130 * @error: set to 1 when buffer isn't fully consumed
131 *
132 * These functions are similar to the standard strtoX() functions, but they are
133 * a little bit easier to use if you want to convert full string of digits into
134 * the binary form. The typical usage:
135 *
136 * int error = 0;
137 * unsigned long num;
138 *
139 * num = simple_strtoul(str, &error);
140 * if (error || ... if needed, your check that num is not out of range ...)
141 * error_happened();
142 */
143#define simple_strtoX(func, type) \
144static inline type simple_##func(const char *snum, int *error) \
145{ \
146 char *endptr; \
147 type ret = func(snum, &endptr, 0); \
148 \
149 if (error && (!*snum || *endptr)) { \
150 errmsg("%s: unable to parse the number '%s'", #func, snum); \
151 *error = 1; \
152 } \
153 \
154 return ret; \
155}
156simple_strtoX(strtol, long int)
157simple_strtoX(strtoll, long long int)
158simple_strtoX(strtoul, unsigned long int)
159simple_strtoX(strtoull, unsigned long long int)
160
161/* Simple version-printing for utils */
162#define common_print_version() \
163do { \
164 printf("%s %s\n", PROGRAM_NAME, VERSION); \
165} while (0)
166
167#include "libmtd_xalloc.h"
168
169#ifdef __cplusplus
170}
171#endif
172
173#endif /* !__MTD_UTILS_COMMON_H__ */