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