XenevaOS
Loading...
Searching...
No Matches
util_macros.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_HELPER_MACROS_H_
3#define _LINUX_HELPER_MACROS_H_
4
6#include <linux/math.h>
7#include <linux/typecheck.h>
8#include <linux/stddef.h>
9
23#define for_each_if(condition) if (!(condition)) {} else
24
36#define find_closest(x, a, as) \
37({ \
38 typeof(as) __fc_i, __fc_as = (as) - 1; \
39 long __fc_mid_x, __fc_x = (x); \
40 long __fc_left, __fc_right; \
41 typeof(*a) const *__fc_a = (a); \
42 for (__fc_i = 0; __fc_i < __fc_as; __fc_i++) { \
43 __fc_mid_x = (__fc_a[__fc_i] + __fc_a[__fc_i + 1]) / 2; \
44 if (__fc_x <= __fc_mid_x) { \
45 __fc_left = __fc_x - __fc_a[__fc_i]; \
46 __fc_right = __fc_a[__fc_i + 1] - __fc_x; \
47 if (__fc_right < __fc_left) \
48 __fc_i++; \
49 break; \
50 } \
51 } \
52 (__fc_i); \
53})
54
66#define find_closest_descending(x, a, as) \
67({ \
68 typeof(as) __fc_i, __fc_as = (as) - 1; \
69 long __fc_mid_x, __fc_x = (x); \
70 long __fc_left, __fc_right; \
71 typeof(*a) const *__fc_a = (a); \
72 for (__fc_i = __fc_as; __fc_i >= 1; __fc_i--) { \
73 __fc_mid_x = (__fc_a[__fc_i] + __fc_a[__fc_i - 1]) / 2; \
74 if (__fc_x <= __fc_mid_x) { \
75 __fc_left = __fc_x - __fc_a[__fc_i]; \
76 __fc_right = __fc_a[__fc_i - 1] - __fc_x; \
77 if (__fc_right < __fc_left) \
78 __fc_i--; \
79 break; \
80 } \
81 } \
82 (__fc_i); \
83})
84
136#define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL)
137
145#define u64_to_user_ptr(x) \
146({ \
147 typecheck(u64, (x)); \
148 (void __user *)(uintptr_t)(x); \
149})
150
159#define is_insidevar(ptr, var) \
160 ((uintptr_t)(ptr) >= (uintptr_t)(var) && \
161 (uintptr_t)(ptr) < (uintptr_t)(var) + sizeof(var))
162
163#endif