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