2 ** $Id: llimits.h,v 1.103 2013/02/20 14:08:56 roberto Exp $
3 ** Limits, basic types, and some other `installation-dependent' definitions
4 ** See Copyright Notice in lua.h
18 typedef unsigned LUA_INT32 lu_int32
;
20 typedef LUAI_UMEM lu_mem
;
22 typedef LUAI_MEM l_mem
;
26 /* chars used as small naturals (so that `char' is reserved for characters) */
27 typedef unsigned char lu_byte
;
30 #define MAX_SIZET ((size_t)(~(size_t)0)-2)
32 #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2)
34 #define MAX_LMEM ((l_mem) ((MAX_LUMEM >> 1) - 2))
37 #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
40 ** conversion of pointer to integer
41 ** this is for hashing only; there is no problem if the integer
42 ** cannot hold the whole pointer value
44 #define IntPoint(p) ((unsigned int)(lu_mem)(p))
48 /* type to ensure maximum alignment */
49 #if !defined(LUAI_USER_ALIGNMENT_T)
50 #define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; }
53 typedef LUAI_USER_ALIGNMENT_T L_Umaxalign
;
56 /* result of a `usual argument conversion' over lua_Number */
57 typedef LUAI_UACNUMBER l_uacNumber
;
60 /* internal assertions for in-house debugging */
61 #if defined(lua_assert)
62 #define check_exp(c,e) (lua_assert(c), (e))
63 /* to avoid problems with conditions too long */
64 #define lua_longassert(c) { if (!(c)) lua_assert(0); }
66 #define lua_assert(c) ((void)0)
67 #define check_exp(c,e) (e)
68 #define lua_longassert(c) ((void)0)
72 ** assertion for checking API calls
74 #if !defined(luai_apicheck)
76 #if defined(LUA_USE_APICHECK)
78 #define luai_apicheck(L,e) assert(e)
80 #define luai_apicheck(L,e) lua_assert(e)
85 #define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
89 #define UNUSED(x) ((void)(x)) /* to avoid warnings */
93 #define cast(t, exp) ((t)(exp))
95 #define cast_byte(i) cast(lu_byte, (i))
96 #define cast_num(i) cast(lua_Number, (i))
97 #define cast_int(i) cast(int, (i))
98 #define cast_uchar(i) cast(unsigned char, (i))
104 #if defined(__GNUC__)
105 #define l_noret void __attribute__((noreturn))
106 #elif defined(_MSC_VER)
107 #define l_noret void __declspec(noreturn)
115 ** maximum depth for nested C calls and syntactical nested non-terminals
116 ** in a program. (Value must fit in an unsigned short int.)
118 #if !defined(LUAI_MAXCCALLS)
119 #define LUAI_MAXCCALLS 200
123 ** maximum number of upvalues in a closure (both C and Lua). (Value
124 ** must fit in an unsigned char.)
126 #define MAXUPVAL UCHAR_MAX
130 ** type for virtual-machine instructions
131 ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
133 typedef lu_int32 Instruction
;
137 /* maximum stack for a Lua function */
142 /* minimum size for the string table (must be power of 2) */
143 #if !defined(MINSTRTABSIZE)
144 #define MINSTRTABSIZE 32
148 /* minimum size for string buffer */
149 #if !defined(LUA_MINBUFFER)
150 #define LUA_MINBUFFER 32
154 #if !defined(lua_lock)
155 #define lua_lock(L) ((void) 0)
156 #define lua_unlock(L) ((void) 0)
159 #if !defined(luai_threadyield)
160 #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
165 ** these macros allow user-specific actions on threads when you defined
166 ** LUAI_EXTRASPACE and need to do something extra when a thread is
167 ** created/deleted/resumed/yielded.
169 #if !defined(luai_userstateopen)
170 #define luai_userstateopen(L) ((void)L)
173 #if !defined(luai_userstateclose)
174 #define luai_userstateclose(L) ((void)L)
177 #if !defined(luai_userstatethread)
178 #define luai_userstatethread(L,L1) ((void)L)
181 #if !defined(luai_userstatefree)
182 #define luai_userstatefree(L,L1) ((void)L)
185 #if !defined(luai_userstateresume)
186 #define luai_userstateresume(L,n) ((void)L)
189 #if !defined(luai_userstateyield)
190 #define luai_userstateyield(L,n) ((void)L)
194 ** lua_number2int is a macro to convert lua_Number to int.
195 ** lua_number2integer is a macro to convert lua_Number to lua_Integer.
196 ** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned.
197 ** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number.
198 ** luai_hashnum is a macro to hash a lua_Number value into an integer.
199 ** The hash must be deterministic and give reasonable values for
200 ** both small and large values (outside the range of integers).
203 #if defined(MS_ASMTRICK) || defined(LUA_MSASMTRICK) /* { */
204 /* trick with Microsoft assembler for X86 */
206 #define lua_number2int(i,n) __asm {__asm fld n __asm fistp i}
207 #define lua_number2integer(i,n) lua_number2int(i, n)
208 #define lua_number2unsigned(i,n) \
209 {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;}
212 #elif defined(LUA_IEEE754TRICK) /* }{ */
213 /* the next trick should work on any machine using IEEE754 with
216 union luai_Cast
{ double l_d
; LUA_INT32 l_p
[2]; };
218 #if !defined(LUA_IEEEENDIAN) /* { */
219 #define LUAI_EXTRAIEEE \
220 static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)};
221 #define LUA_IEEEENDIANLOC (ieeeendian.l_p[1] == 33)
223 #define LUA_IEEEENDIANLOC LUA_IEEEENDIAN
224 #define LUAI_EXTRAIEEE /* empty */
227 #define lua_number2int32(i,n,t) \
229 volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \
230 (i) = (t)u.l_p[LUA_IEEEENDIANLOC]; }
232 #define luai_hashnum(i,n) \
233 { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \
234 (i) = u.l_p[0]; (i) += u.l_p[1]; } /* add double bits for his hash */
236 #define lua_number2int(i,n) lua_number2int32(i, n, int)
237 #define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned)
239 /* the trick can be expanded to lua_Integer when it is a 32-bit value */
240 #if defined(LUA_IEEELL)
241 #define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer)
247 /* the following definitions always work, but may be slow */
249 #if !defined(lua_number2int)
250 #define lua_number2int(i,n) ((i)=(int)(n))
253 #if !defined(lua_number2integer)
254 #define lua_number2integer(i,n) ((i)=(lua_Integer)(n))
257 #if !defined(lua_number2unsigned) /* { */
258 /* the following definition assures proper modulo behavior */
259 #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT)
261 #define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1)
262 #define lua_number2unsigned(i,n) \
263 ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED))
265 #define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n))
270 #if !defined(lua_unsigned2number)
271 /* on several machines, coercion from unsigned to double is slow,
272 so it may be worth to avoid */
273 #define lua_unsigned2number(u) \
274 (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u))
279 #if defined(ltable_c) && !defined(luai_hashnum)
284 #define luai_hashnum(i,n) { int e; \
285 n = l_mathop(frexp)(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \
286 lua_number2int(i, n); i += e; }
293 ** macro to control inclusion of some hard tests on stack reallocation
295 #if !defined(HARDSTACKTESTS)
296 #define condmovestack(L) ((void)0)
298 /* realloc stack keeping its size */
299 #define condmovestack(L) luaD_reallocstack((L), (L)->stacksize)
302 #if !defined(HARDMEMTESTS)
303 #define condchangemem(L) condmovestack(L)
305 #define condchangemem(L) \
306 ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1)))