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