The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
llimits.h
Go to the documentation of this file.
1 /*
2 ** Limits, basic types, and some other `installation-dependent' definitions
3 ** See Copyright Notice in lua.h
4 */
5 
6 #ifndef llimits_h
7 #define llimits_h
8 
9 
10 #include <limits.h>
11 #include <stddef.h>
12 
13 
14 #include "lua.h"
15 
16 
17 typedef unsigned LUA_INT32 lu_int32;
18 
19 typedef LUAI_UMEM lu_mem;
20 
21 typedef LUAI_MEM l_mem;
22 
23 
24 
25 /* chars used as small naturals (so that `char' is reserved for characters) */
26 typedef unsigned char lu_byte;
27 
28 
29 #define MAX_SIZET ((size_t)(~(size_t)0)-2)
30 
31 #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2)
32 
33 #define MAX_LMEM ((l_mem) ((MAX_LUMEM >> 1) - 2))
34 
35 
36 #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
37 
38 /*
39 ** conversion of pointer to integer
40 ** this is for hashing only; there is no problem if the integer
41 ** cannot hold the whole pointer value
42 */
43 #define IntPoint(p) ((unsigned int)(lu_mem)(p))
44 
45 
46 
47 /* type to ensure maximum alignment */
48 #if !defined(LUAI_USER_ALIGNMENT_T)
49 #define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; }
50 #endif
51 
53 
54 
55 /* result of a `usual argument conversion' over lua_Number */
57 
58 
59 /* internal assertions for in-house debugging */
60 #if defined(lua_assert)
61 #define check_exp(c,e) (lua_assert(c), (e))
62 /* to avoid problems with conditions too long */
63 #define lua_longassert(c) { if (!(c)) lua_assert(0); }
64 #else
65 #define lua_assert(c) ((void)0)
66 #define check_exp(c,e) (e)
67 #define lua_longassert(c) ((void)0)
68 #endif
69 
70 /*
71 ** assertion for checking API calls
72 */
73 #if !defined(luai_apicheck)
74 
75 #if defined(LUA_USE_APICHECK)
76 #include <assert.h>
77 #define luai_apicheck(L,e) assert(e)
78 #else
79 #define luai_apicheck(L,e) lua_assert(e)
80 #endif
81 
82 #endif
83 
84 #define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
85 
86 
87 #if !defined(UNUSED)
88 #define UNUSED(x) ((void)(x)) /* to avoid warnings */
89 #endif
90 
91 
92 #define cast(t, exp) ((t)(exp))
93 
94 #define cast_byte(i) cast(lu_byte, (i))
95 #define cast_num(i) cast(lua_Number, (i))
96 #define cast_int(i) cast(int, (i))
97 #define cast_uchar(i) cast(unsigned char, (i))
98 
99 
100 /*
101 ** non-return type
102 */
103 #if defined(__GNUC__)
104 #define l_noret void __attribute__((noreturn))
105 #elif defined(_MSC_VER)
106 #define l_noret void __declspec(noreturn)
107 #else
108 #define l_noret void
109 #endif
110 
111 
112 
113 /*
114 ** maximum depth for nested C calls and syntactical nested non-terminals
115 ** in a program. (Value must fit in an unsigned short int.)
116 */
117 #if !defined(LUAI_MAXCCALLS)
118 #define LUAI_MAXCCALLS 200
119 #endif
120 
121 /*
122 ** maximum number of upvalues in a closure (both C and Lua). (Value
123 ** must fit in an unsigned char.)
124 */
125 #define MAXUPVAL UCHAR_MAX
126 
127 
128 /*
129 ** type for virtual-machine instructions
130 ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
131 */
133 
134 
135 
136 /* maximum stack for a Lua function */
137 #define MAXSTACK 250
138 
139 
140 
141 /* minimum size for the string table (must be power of 2) */
142 #if !defined(MINSTRTABSIZE)
143 #define MINSTRTABSIZE 32
144 #endif
145 
146 
147 /* minimum size for string buffer */
148 #if !defined(LUA_MINBUFFER)
149 #define LUA_MINBUFFER 32
150 #endif
151 
152 
153 #if !defined(lua_lock)
154 #define lua_lock(L) ((void) 0)
155 #define lua_unlock(L) ((void) 0)
156 #endif
157 
158 #if !defined(luai_threadyield)
159 #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
160 #endif
161 
162 
163 /*
164 ** these macros allow user-specific actions on threads when you defined
165 ** LUAI_EXTRASPACE and need to do something extra when a thread is
166 ** created/deleted/resumed/yielded.
167 */
168 #if !defined(luai_userstateopen)
169 #define luai_userstateopen(L) ((void)L)
170 #endif
171 
172 #if !defined(luai_userstateclose)
173 #define luai_userstateclose(L) ((void)L)
174 #endif
175 
176 #if !defined(luai_userstatethread)
177 #define luai_userstatethread(L,L1) ((void)L)
178 #endif
179 
180 #if !defined(luai_userstatefree)
181 #define luai_userstatefree(L,L1) ((void)L)
182 #endif
183 
184 #if !defined(luai_userstateresume)
185 #define luai_userstateresume(L,n) ((void)L)
186 #endif
187 
188 #if !defined(luai_userstateyield)
189 #define luai_userstateyield(L,n) ((void)L)
190 #endif
191 
192 /*
193 ** lua_number2int is a macro to convert lua_Number to int.
194 ** lua_number2integer is a macro to convert lua_Number to lua_Integer.
195 ** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned.
196 ** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number.
197 ** luai_hashnum is a macro to hash a lua_Number value into an integer.
198 ** The hash must be deterministic and give reasonable values for
199 ** both small and large values (outside the range of integers).
200 */
201 
202 #if defined(MS_ASMTRICK) || defined(LUA_MSASMTRICK) /* { */
203 /* trick with Microsoft assembler for X86 */
204 
205 #define lua_number2int(i,n) __asm {__asm fld n __asm fistp i}
206 #define lua_number2integer(i,n) lua_number2int(i, n)
207 #define lua_number2unsigned(i,n) \
208  {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;}
209 
210 
211 #elif defined(LUA_IEEE754TRICK) /* }{ */
212 /* the next trick should work on any machine using IEEE754 with
213  a 32-bit int type */
214 
215 union luai_Cast { double l_d; LUA_INT32 l_p[2]; };
216 
217 #if !defined(LUA_IEEEENDIAN) /* { */
218 #define LUAI_EXTRAIEEE \
219  static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)};
220 #define LUA_IEEEENDIANLOC (ieeeendian.l_p[1] == 33)
221 #else
222 #define LUA_IEEEENDIANLOC LUA_IEEEENDIAN
223 #define LUAI_EXTRAIEEE /* empty */
224 #endif /* } */
225 
226 #define lua_number2int32(i,n,t) \
227  { LUAI_EXTRAIEEE \
228  volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \
229  (i) = (t)u.l_p[LUA_IEEEENDIANLOC]; }
230 
231 #define luai_hashnum(i,n) \
232  { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \
233  (i) = u.l_p[0]; (i) += u.l_p[1]; } /* add double bits for his hash */
234 
235 #define lua_number2int(i,n) lua_number2int32(i, n, int)
236 #define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned)
237 
238 /* the trick can be expanded to lua_Integer when it is a 32-bit value */
239 #if defined(LUA_IEEELL)
240 #define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer)
241 #endif
242 
243 #endif /* } */
244 
245 
246 /* the following definitions always work, but may be slow */
247 
248 #if !defined(lua_number2int)
249 #define lua_number2int(i,n) ((i)=(int)(n))
250 #endif
251 
252 #if !defined(lua_number2integer)
253 #define lua_number2integer(i,n) ((i)=(lua_Integer)(n))
254 #endif
255 
256 #if !defined(lua_number2unsigned) /* { */
257 /* the following definition assures proper modulo behavior */
258 #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT)
259 #include <math.h>
260 #define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1)
261 #define lua_number2unsigned(i,n) \
262  ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED))
263 #else
264 #define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n))
265 #endif
266 #endif /* } */
267 
268 
269 #if !defined(lua_unsigned2number)
270 /* on several machines, coercion from unsigned to double is slow,
271  so it may be worth to avoid */
272 #define lua_unsigned2number(u) \
273  (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u))
274 #endif
275 
276 
277 
278 #if defined(ltable_c) && !defined(luai_hashnum)
279 
280 #include <float.h>
281 #include <math.h>
282 
283 #define luai_hashnum(i,n) { int e; \
284  n = l_mathop(frexp)(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \
285  lua_number2int(i, n); i += e; }
286 
287 #endif
288 
289 
290 
291 /*
292 ** macro to control inclusion of some hard tests on stack reallocation
293 */
294 #if !defined(HARDSTACKTESTS)
295 #define condmovestack(L) ((void)0)
296 #else
297 /* realloc stack keeping its size */
298 #define condmovestack(L) luaD_reallocstack((L), (L)->stacksize)
299 #endif
300 
301 #if !defined(HARDMEMTESTS)
302 #define condchangemem(L) condmovestack(L)
303 #else
304 #define condchangemem(L) \
305  ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1)))
306 #endif
307 
308 #endif
#define LUA_INT32
Definition: luaconf.h:354
#define LUAI_UMEM
Definition: luaconf.h:355
LUAI_MEM l_mem
Definition: llimits.h:21
#define LUAI_UACNUMBER
Definition: luaconf.h:403
unsigned LUA_INT32 lu_int32
Definition: llimits.h:17
unsigned char lu_byte
Definition: llimits.h:26
double l_d
Definition: llimits.h:215
LUAI_UMEM lu_mem
Definition: llimits.h:19
#define LUAI_MEM
Definition: luaconf.h:356
#define LUAI_USER_ALIGNMENT_T
Definition: llimits.h:49
LUAI_UACNUMBER l_uacNumber
Definition: llimits.h:56
lu_int32 Instruction
Definition: llimits.h:132
LUA_INT32 l_p[2]
Definition: llimits.h:215
LUAI_USER_ALIGNMENT_T L_Umaxalign
Definition: llimits.h:52