1 /* Public domain. */ 2 3 #ifndef _LINUX_KERNEL_H 4 #define _LINUX_KERNEL_H 5 6 #include <sys/stdint.h> 7 #include <sys/param.h> 8 #include <sys/systm.h> 9 #include <sys/stdarg.h> 10 #include <sys/malloc.h> 11 12 #include <linux/types.h> 13 #include <linux/compiler.h> 14 #include <linux/bitops.h> 15 #include <linux/log2.h> 16 #include <linux/linkage.h> 17 #include <linux/printk.h> 18 #include <linux/typecheck.h> 19 #include <asm/byteorder.h> 20 21 #define swap(a, b) \ 22 do { __typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while(0) 23 24 #define container_of(ptr, type, member) ({ \ 25 const __typeof( ((type *)0)->member ) *__mptr = (ptr); \ 26 (type *)( (char *)__mptr - offsetof(type,member) );}) 27 28 #define offsetofend(s, e) (offsetof(s, e) + sizeof((((s *)0)->e))) 29 30 #define typeof_member(s, e) typeof(((s *)0)->e) 31 32 #define S8_MAX INT8_MAX 33 #define S16_MAX INT16_MAX 34 #define S32_MAX INT32_MAX 35 #define S64_MAX INT64_MAX 36 37 #define U8_MAX UINT8_MAX 38 #define U16_MAX UINT16_MAX 39 #define U32_MAX UINT32_MAX 40 #define U64_C(x) UINT64_C(x) 41 #define U64_MAX UINT64_MAX 42 43 #define ARRAY_SIZE nitems 44 45 #define lower_32_bits(n) ((u32)(n)) 46 #define upper_32_bits(_val) ((u32)(((_val) >> 16) >> 16)) 47 48 #define scnprintf(str, size, fmt, arg...) snprintf(str, size, fmt, ## arg) 49 50 #define min_t(t, a, b) ({ \ 51 t __min_a = (a); \ 52 t __min_b = (b); \ 53 __min_a < __min_b ? __min_a : __min_b; }) 54 55 #define max_t(t, a, b) ({ \ 56 t __max_a = (a); \ 57 t __max_b = (b); \ 58 __max_a > __max_b ? __max_a : __max_b; }) 59 60 #define clamp_t(t, x, a, b) min_t(t, max_t(t, x, a), b) 61 #define clamp(x, a, b) clamp_t(__typeof(x), x, a, b) 62 #define clamp_val(x, a, b) clamp_t(__typeof(x), x, a, b) 63 64 #define min(a, b) MIN(a, b) 65 #define max(a, b) MAX(a, b) 66 #define min3(x, y, z) MIN(x, MIN(y, z)) 67 #define max3(x, y, z) MAX(x, MAX(y, z)) 68 69 #define mult_frac(x, n, d) (((x) * (n)) / (d)) 70 71 #define roundup2(x, y) (((x) + ((y) - 1)) & (~((__typeof(x))(y) - 1))) 72 #define round_up(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) 73 #define round_down(x, y) (((x) / (y)) * (y)) /* y is power of two */ 74 #define rounddown(x, y) (((x) / (y)) * (y)) /* arbitrary y */ 75 #define DIV_ROUND_UP(x, y) (((x) + ((y) - 1)) / (y)) 76 #define DIV_ROUND_UP_ULL(x, y) DIV_ROUND_UP(x, y) 77 #define DIV_ROUND_DOWN(x, y) ((x) / (y)) 78 #define DIV_ROUND_DOWN_ULL(x, y) DIV_ROUND_DOWN(x, y) 79 #define DIV_ROUND_CLOSEST(x, y) (((x) + ((y) / 2)) / (y)) 80 #define DIV_ROUND_CLOSEST_ULL(x, y) DIV_ROUND_CLOSEST(x, y) 81 82 #define IS_ALIGNED(x, y) (((x) & ((y) - 1)) == 0) 83 #define PTR_ALIGN(x, y) ((__typeof(x))roundup2((unsigned long)(x), (y))) 84 85 static inline char * 86 kvasprintf(int flags, const char *fmt, va_list ap) 87 { 88 char *buf; 89 size_t len; 90 va_list vl; 91 92 va_copy(vl, ap); 93 len = vsnprintf(NULL, 0, fmt, vl); 94 va_end(vl); 95 96 buf = malloc(len + 1, M_DRM, flags); 97 if (buf) { 98 vsnprintf(buf, len + 1, fmt, ap); 99 } 100 101 return buf; 102 } 103 104 static inline char * 105 kasprintf(int flags, const char *fmt, ...) 106 { 107 char *buf; 108 va_list ap; 109 110 va_start(ap, fmt); 111 buf = kvasprintf(flags, fmt, ap); 112 va_end(ap); 113 114 return buf; 115 } 116 117 static inline int 118 vscnprintf(char *buf, size_t size, const char *fmt, va_list ap) 119 { 120 int nc; 121 122 nc = vsnprintf(buf, size, fmt, ap); 123 if (nc > (size - 1)) 124 return (size - 1); 125 else 126 return nc; 127 } 128 129 static inline int 130 _in_dbg_master(void) 131 { 132 #ifdef DDB 133 return (db_active); 134 #endif 135 return (0); 136 } 137 138 #define oops_in_progress _in_dbg_master() 139 140 #define might_sleep() assertwaitok() 141 #define might_sleep_if(x) do { \ 142 if (x) \ 143 assertwaitok(); \ 144 } while (0) 145 146 #define add_taint(x, y) 147 #define TAINT_MACHINE_CHECK 0 148 #define TAINT_WARN 1 149 #define LOCKDEP_STILL_OK 0 150 151 #define u64_to_user_ptr(x) ((void *)(uintptr_t)(x)) 152 153 #define _RET_IP_ __builtin_return_address(0) 154 155 #define STUB() do { printf("%s: stub\n", __func__); } while(0) 156 157 #endif 158