1 #ifndef _MAGIC_DEF_H 2 #define _MAGIC_DEF_H 3 4 #if defined(_MINIX) || defined(_MINIX_SYSTEM) 5 #ifndef __MINIX 6 #define __MINIX 1 7 #endif 8 #endif 9 10 #include <limits.h> 11 12 /* Type macros. */ 13 #ifdef __MINIX 14 #define MAGIC_LONG_LONG_SUPPORTED 1 15 #define MAGIC_LONG_DOUBLE_SUPPORTED 0 16 #else 17 #ifdef LLONG_MAX 18 #define MAGIC_LONG_LONG_SUPPORTED 1 19 #endif 20 #ifdef __LDBL_MAX__ 21 #define MAGIC_LONG_DOUBLE_SUPPORTED 1 22 #endif 23 #endif 24 25 /* Modifier macros. */ 26 #define EXTERN extern 27 #define PRIVATE static 28 #ifdef __SHARED__ 29 #define PUBLIC EXTERN 30 #else 31 #define PUBLIC 32 #endif 33 34 #ifdef __MINIX 35 #define INLINE __inline__ 36 #define THREAD_LOCAL 37 #undef UNUSED 38 #define FUNCTION_BLOCK(B) B 39 #else 40 #define INLINE inline 41 #define THREAD_LOCAL __thread 42 #ifdef __cplusplus 43 #define FUNCTION_BLOCK(B) extern "C"{ B } 44 #else 45 #define FUNCTION_BLOCK(B) B 46 #endif 47 #endif 48 49 #define UNUSED(VAR) VAR __attribute__((unused)) 50 #define USED __attribute__((used)) 51 #define VOLATILE volatile 52 53 /* Magic macros. */ 54 #define MAGIC_VAR USED 55 #define MAGIC_FUNC PUBLIC USED __attribute__((noinline)) 56 #define MAGIC_FUNC_BODY() __asm__("") 57 #define MAGIC_HOOK PUBLIC USED __attribute__((always_inline)) inline 58 #define MAGIC_MACRO_FUNC static __attribute__((always_inline)) 59 60 #define TRUE 1 61 #define FALSE 0 62 63 #ifdef __MINIX 64 #define SYS_PAGESIZE 4096 65 #else 66 #define SYS_PAGESIZE sysconf(_SC_PAGESIZE) 67 #endif 68 #define MAGIC_ROUND_DOWN(val, round) ((val) & ~((round) - 1)) 69 #define MAGIC_ROUND_UP(val, round) (MAGIC_ROUND_DOWN(val, round) == \ 70 (val) ? (val) : MAGIC_ROUND_DOWN((val) + (round), (round))) 71 #define MAGIC_ROUND_DOWN_TO_PAGESIZE(addr) MAGIC_ROUND_DOWN(addr, SYS_PAGESIZE) 72 #define MAGIC_ROUND_UP_TO_PAGESIZE(addr) MAGIC_ROUND_UP(addr, SYS_PAGESIZE) 73 74 #ifdef __MINIX 75 #define _MAGIC_CAS(P, O, N) (*(P) == (O) ? *(P)=(N) : (N)+1) 76 #define MAGIC_CAS(P, O, N) (_MAGIC_CAS(P, O, N) == (N) ? (O) : *(P)) 77 #define _MAGIC_PCAS(P, O, N) (*(P) == (O) ? *(P)=(N) : (void *)((intptr_t)(N)+1)) 78 #define MAGIC_PCAS(P, O, N) (_MAGIC_PCAS(P, O, N) == (N) ? (O) : *(P)) 79 #define MAGIC_FAA(P, V) (((*P)+=V)-V) 80 #define MAGIC_FAS(P, V) (((*P)-=V)+V) 81 #else 82 #define MAGIC_CAS(P, O, N) __sync_val_compare_and_swap((P), (O), (N)) 83 #define MAGIC_PCAS(P, O, N) MAGIC_CAS(P, O, N) 84 #define MAGIC_FAA(P, V) __sync_fetch_and_add(P, V) 85 #define MAGIC_FAS(P, V) __sync_fetch_and_sub(P, V) 86 #endif 87 88 /* Magic arch-specific macros. */ 89 #define MAGIC_FRAMEADDR_TO_RETADDR_PTR(F) (((char*)(F))+4) 90 91 /* Magic ranges. */ 92 #define MAGIC_LINKER_VAR_NAMES "end", "etext", "edata", NULL 93 94 #ifdef __MINIX 95 #define MAGIC_TEXT_START ((void*)(0x1000)) 96 #define MAGIC_STACK_GAP (4*1024) 97 #else 98 #define MAGIC_TEXT_START ((void*)(0x08048000)) 99 #define MAGIC_STACK_GAP (4*1024*1024) 100 #endif 101 102 #define MAGIC_TEXT_END 0 /* 0 if right before data. */ 103 #define MAGIC_HEAP_START 0 /* 0 if right after data. */ 104 #define MAGIC_HEAP_GAP (4*1024) 105 #define MAGIC_RANGE_ROUND_DATA 1 106 #define MAGIC_RANGE_ROUND_TEXT 1 107 #define MAGIC_RANGE_ROUND_STACK 1 108 109 /* Magic IDs */ 110 #define MAGIC_ID_NONE 0 111 #define MAGIC_ID_FORCE_LONG 1 112 #if defined(__MINIX) || MAGIC_ID_FORCE_LONG 113 typedef unsigned long _magic_id_t; 114 #define MAGIC_ID_MAX ULONG_MAX 115 #define MAGIC_ID_FORMAT "%lu" 116 #else 117 typedef unsigned long long _magic_id_t; 118 #define MAGIC_ID_MAX ULLONG_MAX 119 #define MAGIC_ID_FORMAT "%llu" 120 #endif 121 122 /* Magic error codes. */ 123 #define MAGIC_ENOENT (-100) 124 #define MAGIC_EBADENT (-101) 125 #define MAGIC_EBADMSTATE (-102) 126 #define MAGIC_EINVAL (-103) 127 #define MAGIC_EGENERIC (-104) 128 #define MAGIC_EBADWALK (-105) 129 #define MAGIC_ERANGE (-106) 130 #define MAGIC_ESIGN (-107) 131 #define MAGIC_ENOMEM (-108) 132 #define MAGIC_ENOPTR ((void*)-1) 133 134 /* 135 * Basic return type definitions. Not really needed, but they make 136 * the code easier to read. 137 */ 138 #ifndef OK 139 #define OK 0 140 #endif 141 #ifndef EGENERIC 142 #define EGENERIC -1 143 #endif 144 145 /* Magic printf. */ 146 #ifdef __MINIX 147 #define MAGIC_PRINTF_DEFAULT printf 148 #else 149 #define MAGIC_PRINTF_DEFAULT magic_err_printf 150 #endif 151 152 FUNCTION_BLOCK( 153 154 typedef int (*printf_ptr_t) (char const *str, ...); 155 EXTERN printf_ptr_t _magic_printf; 156 EXTERN void magic_assert_failed(const char *assertion, const char *file, 157 const char *function, const int line); 158 159 ) 160 161 /* assert() override. */ 162 #define ENABLE_ASSERTIONS 1 163 #ifndef __MINIX 164 #define CUSTOM_ASSERTIONS 0 165 #else 166 #define CUSTOM_ASSERTIONS 1 167 #endif 168 169 #include <assert.h> 170 171 #if CUSTOM_ASSERTIONS 172 #ifdef assert 173 #undef assert 174 #endif 175 #ifndef __ASSERT_FUNCTION 176 #define __ASSERT_FUNCTION "" 177 #endif 178 179 #if ENABLE_ASSERTIONS 180 #define assert(X) do{ \ 181 if(!(X)) { \ 182 magic_assert_failed(#X, __FILE__, __ASSERT_FUNCTION, __LINE__); \ 183 } \ 184 } while(0) 185 #else 186 # define assert(X) 187 #endif 188 #endif 189 190 #endif 191 192