3 ** $Id: llimits.h,v 1.103.1.1 2013/04/12 18:48:47 roberto Exp $
4 ** Limits, basic types, and some other `installation-dependent' definitions
5 ** See Copyright Notice in lua.h
12 #include <sys/lua/lua.h>
15 typedef unsigned LUA_INT32 lu_int32;
17 typedef LUAI_UMEM lu_mem;
19 typedef LUAI_MEM l_mem;
23 /* chars used as small naturals (so that `char' is reserved for characters) */
24 typedef unsigned char lu_byte;
27 #define MAX_SIZET ((size_t)(~(size_t)0)-2)
29 #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2)
31 #define MAX_LMEM ((l_mem) ((MAX_LUMEM >> 1) - 2))
34 #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
37 ** conversion of pointer to integer
38 ** this is for hashing only; there is no problem if the integer
39 ** cannot hold the whole pointer value
41 #define IntPoint(p) ((unsigned int)(lu_mem)(p))
45 /* type to ensure maximum alignment */
46 #if !defined(LUAI_USER_ALIGNMENT_T)
47 #define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; }
50 typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
53 /* result of a `usual argument conversion' over lua_Number */
54 typedef LUAI_UACNUMBER l_uacNumber;
57 /* internal assertions for in-house debugging */
58 #if defined(lua_assert)
59 #define check_exp(c,e) (lua_assert(c), (e))
60 /* to avoid problems with conditions too long */
61 #define lua_longassert(c) { if (!(c)) lua_assert(0); }
63 #define lua_assert(c) ((void)0)
64 #define check_exp(c,e) (e)
65 #define lua_longassert(c) ((void)0)
69 ** assertion for checking API calls
71 #if !defined(luai_apicheck)
73 #if defined(LUA_USE_APICHECK)
75 #define luai_apicheck(L,e) assert(e)
77 #define luai_apicheck(L,e) lua_assert(e)
82 #define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
86 #define UNUSED(x) ((void)(x)) /* to avoid warnings */
90 #define cast(t, exp) ((t)(exp))
92 #define cast_byte(i) cast(lu_byte, (i))
93 #define cast_num(i) cast(lua_Number, (i))
94 #define cast_int(i) cast(int, (i))
95 #define cast_uchar(i) cast(unsigned char, (i))
101 ** Suppress noreturn attribute in kernel builds to avoid objtool check warnings
103 #if defined(__GNUC__) && !defined(_KERNEL)
104 #define l_noret void __attribute__((noreturn))
105 #elif defined(_MSC_VER)
106 #define l_noret void __declspec(noreturn)
114 ** maximum depth for nested C calls and syntactical nested non-terminals
115 ** in a program. (Value must fit in an unsigned short int.)
117 ** Note: On amd64 platform, the limit has been measured to be 45. We set
118 ** the maximum lower to give a margin for changing the amount of stack
119 ** used by various functions involved in parsing and executing code.
121 #if !defined(LUAI_MAXCCALLS)
122 #define LUAI_MAXCCALLS 20
126 * Minimum amount of available stack space (in bytes) to make a C call. With
127 * gsub() recursion, the stack space between each luaD_call() is 1256 bytes.
129 #define LUAI_MINCSTACK 4096
132 ** maximum number of upvalues in a closure (both C and Lua). (Value
133 ** must fit in an unsigned char.)
135 #define MAXUPVAL UCHAR_MAX
139 ** type for virtual-machine instructions
140 ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
142 typedef lu_int32 Instruction;
146 /* maximum stack for a Lua function */
151 /* minimum size for the string table (must be power of 2) */
152 #if !defined(MINSTRTABSIZE)
153 #define MINSTRTABSIZE 32
157 /* minimum size for string buffer */
158 #if !defined(LUA_MINBUFFER)
159 #define LUA_MINBUFFER 32
163 #if !defined(lua_lock)
164 #define lua_lock(L) ((void) 0)
165 #define lua_unlock(L) ((void) 0)
168 #if !defined(luai_threadyield)
169 #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
174 ** these macros allow user-specific actions on threads when you defined
175 ** LUAI_EXTRASPACE and need to do something extra when a thread is
176 ** created/deleted/resumed/yielded.
178 #if !defined(luai_userstateopen)
179 #define luai_userstateopen(L) ((void)L)
182 #if !defined(luai_userstateclose)
183 #define luai_userstateclose(L) ((void)L)
186 #if !defined(luai_userstatethread)
187 #define luai_userstatethread(L,L1) ((void)L)
190 #if !defined(luai_userstatefree)
191 #define luai_userstatefree(L,L1) ((void)L)
194 #if !defined(luai_userstateresume)
195 #define luai_userstateresume(L,n) ((void)L)
198 #if !defined(luai_userstateyield)
199 #define luai_userstateyield(L,n) ((void)L)
203 ** lua_number2int is a macro to convert lua_Number to int.
204 ** lua_number2integer is a macro to convert lua_Number to lua_Integer.
205 ** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned.
206 ** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number.
207 ** luai_hashnum is a macro to hash a lua_Number value into an integer.
208 ** The hash must be deterministic and give reasonable values for
209 ** both small and large values (outside the range of integers).
212 #if defined(MS_ASMTRICK) || defined(LUA_MSASMTRICK) /* { */
213 /* trick with Microsoft assembler for X86 */
215 #define lua_number2int(i,n) __asm {__asm fld n __asm fistp i}
216 #define lua_number2integer(i,n) lua_number2int(i, n)
217 #define lua_number2unsigned(i,n) \
218 {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;}
221 #elif defined(LUA_IEEE754TRICK) /* }{ */
222 /* the next trick should work on any machine using IEEE754 with
225 union luai_Cast { double l_d; LUA_INT32 l_p[2]; };
227 #if !defined(LUA_IEEEENDIAN) /* { */
228 #define LUAI_EXTRAIEEE \
229 static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)};
230 #define LUA_IEEEENDIANLOC (ieeeendian.l_p[1] == 33)
232 #define LUA_IEEEENDIANLOC LUA_IEEEENDIAN
233 #define LUAI_EXTRAIEEE /* empty */
236 #define lua_number2int32(i,n,t) \
238 volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \
239 (i) = (t)u.l_p[LUA_IEEEENDIANLOC]; }
241 #define luai_hashnum(i,n) \
242 { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \
243 (i) = u.l_p[0]; (i) += u.l_p[1]; } /* add double bits for his hash */
245 #define lua_number2int(i,n) lua_number2int32(i, n, int)
246 #define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned)
248 /* the trick can be expanded to lua_Integer when it is a 32-bit value */
249 #if defined(LUA_IEEELL)
250 #define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer)
256 /* the following definitions always work, but may be slow */
258 #if !defined(lua_number2int)
259 #define lua_number2int(i,n) ((i)=(int)(n))
262 #if !defined(lua_number2integer)
263 #define lua_number2integer(i,n) ((i)=(lua_Integer)(n))
266 #if !defined(lua_number2unsigned) /* { */
267 /* the following definition assures proper modulo behavior */
268 #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT)
270 #define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1)
271 #define lua_number2unsigned(i,n) \
272 ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED))
274 #define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n))
279 #if !defined(lua_unsigned2number)
280 /* on several machines, coercion from unsigned to double is slow,
281 so it may be worth to avoid */
282 #define lua_unsigned2number(u) \
283 (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u))
288 #if defined(ltable_c) && !defined(luai_hashnum)
290 #define luai_hashnum(i,n) (i = lcompat_hashnum(n))
297 ** macro to control inclusion of some hard tests on stack reallocation
299 #if !defined(HARDSTACKTESTS)
300 #define condmovestack(L) ((void)0)
302 /* realloc stack keeping its size */
303 #define condmovestack(L) luaD_reallocstack((L), (L)->stacksize)
306 #if !defined(HARDMEMTESTS)
307 #define condchangemem(L) condmovestack(L)
309 #define condchangemem(L) \
310 ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1)))