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 S8_MAX INT8_MAX 29 #define S16_MAX INT16_MAX 30 #define S32_MAX INT32_MAX 31 #define S64_MAX INT64_MAX 32 33 #define U8_MAX UINT8_MAX 34 #define U16_MAX UINT16_MAX 35 #define U32_MAX UINT32_MAX 36 #define U64_C(x) UINT64_C(x) 37 #define U64_MAX UINT64_MAX 38 39 #define IS_ALIGNED(x, y) (((x) & ((y) - 1)) == 0) 40 41 #define ARRAY_SIZE nitems 42 43 #define lower_32_bits(n) ((u32)(n)) 44 #define upper_32_bits(_val) ((u32)(((_val) >> 16) >> 16)) 45 46 #define scnprintf(str, size, fmt, arg...) snprintf(str, size, fmt, ## arg) 47 48 #define min_t(t, a, b) ({ \ 49 t __min_a = (a); \ 50 t __min_b = (b); \ 51 __min_a < __min_b ? __min_a : __min_b; }) 52 53 #define max_t(t, a, b) ({ \ 54 t __max_a = (a); \ 55 t __max_b = (b); \ 56 __max_a > __max_b ? __max_a : __max_b; }) 57 58 #define clamp_t(t, x, a, b) min_t(t, max_t(t, x, a), b) 59 #define clamp(x, a, b) clamp_t(__typeof(x), x, a, b) 60 #define clamp_val(x, a, b) clamp_t(__typeof(x), x, a, b) 61 62 #define min(a, b) MIN(a, b) 63 #define max(a, b) MAX(a, b) 64 #define min3(x, y, z) MIN(x, MIN(y, z)) 65 #define max3(x, y, z) MAX(x, MAX(y, z)) 66 67 #define mult_frac(x, n, d) (((x) * (n)) / (d)) 68 69 #define round_up(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) 70 #define round_down(x, y) (((x) / (y)) * (y)) /* y is power of two */ 71 #define rounddown(x, y) (((x) / (y)) * (y)) /* arbitary y */ 72 #define DIV_ROUND_UP(x, y) (((x) + ((y) - 1)) / (y)) 73 #define DIV_ROUND_UP_ULL(x, y) DIV_ROUND_UP(x, y) 74 #define DIV_ROUND_DOWN(x, y) ((x) / (y)) 75 #define DIV_ROUND_DOWN_ULL(x, y) DIV_ROUND_DOWN(x, y) 76 #define DIV_ROUND_CLOSEST(x, y) (((x) + ((y) / 2)) / (y)) 77 #define DIV_ROUND_CLOSEST_ULL(x, y) DIV_ROUND_CLOSEST(x, y) 78 79 static inline char * 80 kasprintf(int flags, const char *fmt, ...) 81 { 82 char *buf; 83 size_t len; 84 va_list ap; 85 86 va_start(ap, fmt); 87 len = vsnprintf(NULL, 0, fmt, ap); 88 va_end(ap); 89 90 buf = malloc(len + 1, M_DRM, flags); 91 if (buf) { 92 va_start(ap, fmt); 93 vsnprintf(buf, len + 1, fmt, ap); 94 va_end(ap); 95 } 96 97 return buf; 98 } 99 100 static inline char * 101 kvasprintf(int flags, const char *fmt, va_list ap) 102 { 103 char *buf; 104 size_t len; 105 106 len = vsnprintf(NULL, 0, fmt, ap); 107 108 buf = malloc(len + 1, M_DRM, flags); 109 if (buf) { 110 vsnprintf(buf, len + 1, fmt, ap); 111 } 112 113 return buf; 114 } 115 116 static inline int 117 _in_dbg_master(void) 118 { 119 #ifdef DDB 120 return (db_active); 121 #endif 122 return (0); 123 } 124 125 #define oops_in_progress _in_dbg_master() 126 127 #define might_sleep() 128 #define might_sleep_if(x) 129 130 #define add_taint(x, y) 131 #define TAINT_MACHINE_CHECK 0 132 #define LOCKDEP_STILL_OK 0 133 134 #define u64_to_user_ptr(x) ((void *)(uintptr_t)(x)) 135 136 #endif 137