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