common.h (1797B)
1 #ifndef __COMMON_H__ 2 #define __COMMON_H__ 3 4 #include <stdio.h> 5 #include <stdint.h> 6 #include <sys/types.h> 7 8 #if __STDC_VERSION__ < 199901L 9 #if __GNUC__ >= 2 10 #define __func__ __FUNCTION__ 11 #else 12 #define __func__ "<unknown>" 13 #endif 14 #endif 15 16 #define BINCLUDE_DEBUG_CODE 17 #if defined(BINCLUDE_DEBUG_CODE) 18 #define DEBUG_CODE(statement) statement 19 #else 20 #define DEBUG_CODE(statement) 21 #endif 22 23 #define BSYSCALL_TRACE 24 #if 1 25 #undef BSYSCALL_TRACE 26 #endif 27 #if defined(BSYSCALL_TRACE) 28 #define TRACE_SYSCALL(statement) statement 29 #else 30 #define TRACE_SYSCALL(statement) 31 #endif 32 33 #define MIN_ERRNO 1 34 #define MAX_ERRNO 44 35 #define SETERRNO_AND_RET(err, ret) ({ errno = (err); return (ret); }) 36 #define stringify(s) #s 37 #define info(format, ...) printf(format, ##__VA_ARGS__) 38 #define likely(x) __builtin_expect(!!(x), 1) 39 #define unlikely(x) __builtin_expect(!!(x), 0) 40 #define assert(cond) ({ if (unlikely(!(cond))) panic("Assertion '%s' failed!", #cond); }) 41 #define len(array) (sizeof(array) / sizeof(array[0])) 42 #define offsetof(TYPE, MEMBER) ((size_t)__builtin_offsetof(TYPE, MEMBER)) 43 #define container_of(ptr, type, member) ({ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) ); }) 44 #define panic(format, ...) panic_internal(__FILE__, __func__, __LINE__, format, ##__VA_ARGS__) 45 46 typedef enum { false, true } bool; 47 typedef uint32_t addr_t; 48 49 extern void panic_internal(const char *file, const char *fn, uint32_t line, const char *fmt, ...); 50 extern void hexdump(const void *ptr, size_t len); 51 extern void stacktrace(int depth); 52 53 static inline int 54 IS_ERR(const void *ptr) 55 { 56 return (int)ptr >= -MAX_ERRNO && (int)ptr <= -MIN_ERRNO; 57 } 58 59 static inline void * 60 ERR_PTR(int error) 61 { 62 return (void *)error; 63 } 64 65 static inline int 66 PTR_ERR(const void *ptr) 67 { 68 return (int)ptr; 69 } 70 71 #endif 72