File: util.h

package info (click to toggle)
nilfs-tools 2.2.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,852 kB
  • sloc: ansic: 15,594; sh: 4,374; perl: 4,174; makefile: 140
file content (58 lines) | stat: -rw-r--r-- 1,406 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * util.h - utility definitions
 */
#ifndef __UTIL_H__
#define __UTIL_H__

#include <assert.h>
#include <stdlib.h>
#include "compat.h"

/* likely() and unlikely() macros */
#define likely(x)	__builtin_expect(!!(x), 1)
#define unlikely(x)	__builtin_expect(!!(x), 0)

#ifndef BUG
#define BUG()	abort()
#endif

#define BUG_ON(x)	   assert(!(x))
/* Force a compilation error if the condition is true */
#define BUILD_BUG_ON(condition) ((void)sizeof(struct { int: -!!(condition); }))

#define typecheck(type, x)					\
	({							\
	   type __dummy;					\
	   typeof(x) __dummy2;					\
	   (void)(&__dummy == &__dummy2);			\
	   1;							\
	})


#define DIV_ROUND_UP(n, m)	(((n) + (m) - 1) / (m))
#define ARRAY_SIZE(arr)		(sizeof(arr) / sizeof((arr)[0]))


#define min_t(type, x, y) \
	({ type __x = (x); type __y = (y); __x < __y ? __x : __y; })
#define max_t(type, x, y) \
	({ type __x = (x); type __y = (y); __x > __y ? __x : __y; })


#define cnt64_gt(a, b)						\
	(typecheck(__u64, a) && typecheck(__u64, b) &&		\
	 ((__s64)(b) - (__s64)(a) < 0))
#define cnt64_ge(a, b)						\
	(typecheck(__u64, a) && typecheck(__u64, b) &&		\
	 ((__s64)(a) - (__s64)(b) >= 0))
#define cnt64_lt(a, b)		cnt64_gt(b, a)
#define cnt64_le(a, b)		cnt64_ge(b, a)


#define timeval_to_timespec(tv, ts)		\
do {						\
	(ts)->tv_sec = (tv)->tv_sec;		\
	(ts)->tv_nsec = (tv)->tv_usec * 1000;	\
} while (0)

#endif /* __UTIL_H__ */