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