xref: /netbsd-src/external/mit/lua/dist/src/llimits.h (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: llimits.h,v 1.9 2017/04/26 13:17:33 mbalmer Exp $	*/
2 
3 /*
4 ** Id: llimits.h,v 1.141 2015/11/19 19:16:22 roberto Exp
5 ** Limits, basic types, and some other 'installation-dependent' definitions
6 ** See Copyright Notice in lua.h
7 */
8 
9 #ifndef llimits_h
10 #define llimits_h
11 
12 
13 #ifndef _KERNEL
14 #include <limits.h>
15 #include <stddef.h>
16 #endif /* _KERNEL */
17 
18 
19 #include "lua.h"
20 
21 /*
22 ** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count
23 ** the total memory used by Lua (in bytes). Usually, 'size_t' and
24 ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
25 */
26 #if defined(LUAI_MEM)		/* { external definitions? */
27 typedef LUAI_UMEM lu_mem;
28 typedef LUAI_MEM l_mem;
29 #elif LUAI_BITSINT >= 32	/* }{ */
30 typedef size_t lu_mem;
31 typedef ptrdiff_t l_mem;
32 #else  /* 16-bit ints */	/* }{ */
33 typedef unsigned long lu_mem;
34 typedef long l_mem;
35 #endif				/* } */
36 
37 
38 /* chars used as small naturals (so that 'char' is reserved for characters) */
39 typedef unsigned char lu_byte;
40 
41 
42 /* maximum value for size_t */
43 #define MAX_SIZET	((size_t)(~(size_t)0))
44 
45 /* maximum size visible for Lua (must be representable in a lua_Integer */
46 #define MAX_SIZE	(sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
47                           : (size_t)(LUA_MAXINTEGER))
48 
49 
50 #define MAX_LUMEM	((lu_mem)(~(lu_mem)0))
51 
52 #define MAX_LMEM	((l_mem)(MAX_LUMEM >> 1))
53 
54 
55 #define MAX_INT		INT_MAX  /* maximum value of an int */
56 
57 
58 /*
59 ** conversion of pointer to unsigned integer:
60 ** this is for hashing only; there is no problem if the integer
61 ** cannot hold the whole pointer value
62 */
63 #define point2uint(p)	((unsigned int)((size_t)(p) & UINT_MAX))
64 
65 
66 
67 /* type to ensure maximum alignment */
68 #if defined(LUAI_USER_ALIGNMENT_T)
69 typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
70 #else
71 typedef union {
72 #ifndef _KERNEL
73   lua_Number n;
74   double u;
75 #endif /* _KERNEL */
76   void *s;
77   lua_Integer i;
78   long l;
79 } L_Umaxalign;
80 #endif
81 
82 
83 
84 /* types of 'usual argument conversions' for lua_Number and lua_Integer */
85 #ifndef _KERNEL
86 typedef LUAI_UACNUMBER l_uacNumber;
87 #endif /* _KERNEL */
88 typedef LUAI_UACINT l_uacInt;
89 
90 
91 /* internal assertions for in-house debugging */
92 #if defined(lua_assert)
93 #define check_exp(c,e)		(lua_assert(c), (e))
94 /* to avoid problems with conditions too long */
95 #define lua_longassert(c)	((c) ? (void)0 : lua_assert(0))
96 #else
97 #define lua_assert(c)		((void)0)
98 #define check_exp(c,e)		(e)
99 #define lua_longassert(c)	((void)0)
100 #endif
101 
102 /*
103 ** assertion for checking API calls
104 */
105 #if !defined(luai_apicheck)
106 #define luai_apicheck(l,e)	lua_assert(e)
107 #endif
108 
109 #define api_check(l,e,msg)	luai_apicheck(l,(e) && msg)
110 
111 
112 /* macro to avoid warnings about unused variables */
113 #if !defined(UNUSED)
114 #define UNUSED(x)	((void)(x))
115 #endif
116 
117 
118 /* type casts (a macro highlights casts in the code) */
119 #define cast(t, exp)	((t)(exp))
120 
121 #define cast_void(i)	cast(void, (i))
122 #define cast_byte(i)	cast(lu_byte, (i))
123 #define cast_num(i)	cast(lua_Number, (i))
124 #define cast_int(i)	cast(int, (i))
125 #define cast_uchar(i)	cast(unsigned char, (i))
126 
127 
128 /* cast a signed lua_Integer to lua_Unsigned */
129 #if !defined(l_castS2U)
130 #define l_castS2U(i)	((lua_Unsigned)(i))
131 #endif
132 
133 /*
134 ** cast a lua_Unsigned to a signed lua_Integer; this cast is
135 ** not strict ISO C, but two-complement architectures should
136 ** work fine.
137 */
138 #if !defined(l_castU2S)
139 #define l_castU2S(i)	((lua_Integer)(i))
140 #endif
141 
142 
143 /*
144 ** non-return type
145 */
146 #if defined(__GNUC__)
147 #define l_noret		void __attribute__((noreturn))
148 #elif defined(_MSC_VER) && _MSC_VER >= 1200
149 #define l_noret		void __declspec(noreturn)
150 #else
151 #define l_noret		void
152 #endif
153 
154 
155 
156 /*
157 ** maximum depth for nested C calls and syntactical nested non-terminals
158 ** in a program. (Value must fit in an unsigned short int.)
159 */
160 #if !defined(LUAI_MAXCCALLS)
161 #define LUAI_MAXCCALLS		200
162 #endif
163 
164 
165 
166 /*
167 ** type for virtual-machine instructions;
168 ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
169 */
170 #if LUAI_BITSINT >= 32
171 typedef unsigned int Instruction;
172 #else
173 typedef unsigned long Instruction;
174 #endif
175 
176 
177 
178 /*
179 ** Maximum length for short strings, that is, strings that are
180 ** internalized. (Cannot be smaller than reserved words or tags for
181 ** metamethods, as these strings must be internalized;
182 ** #("function") = 8, #("__newindex") = 10.)
183 */
184 #if !defined(LUAI_MAXSHORTLEN)
185 #define LUAI_MAXSHORTLEN	40
186 #endif
187 
188 
189 /*
190 ** Initial size for the string table (must be power of 2).
191 ** The Lua core alone registers ~50 strings (reserved words +
192 ** metaevent keys + a few others). Libraries would typically add
193 ** a few dozens more.
194 */
195 #if !defined(MINSTRTABSIZE)
196 #define MINSTRTABSIZE	128
197 #endif
198 
199 
200 /*
201 ** Size of cache for strings in the API. 'N' is the number of
202 ** sets (better be a prime) and "M" is the size of each set (M == 1
203 ** makes a direct cache.)
204 */
205 #if !defined(STRCACHE_N)
206 #define STRCACHE_N		53
207 #define STRCACHE_M		2
208 #endif
209 
210 
211 /* minimum size for string buffer */
212 #if !defined(LUA_MINBUFFER)
213 #define LUA_MINBUFFER	32
214 #endif
215 
216 
217 /*
218 ** macros that are executed whenever program enters the Lua core
219 ** ('lua_lock') and leaves the core ('lua_unlock')
220 */
221 #if !defined(lua_lock)
222 #define lua_lock(L)	((void) 0)
223 #define lua_unlock(L)	((void) 0)
224 #endif
225 
226 /*
227 ** macro executed during Lua functions at points where the
228 ** function can yield.
229 */
230 #if !defined(luai_threadyield)
231 #define luai_threadyield(L)	{lua_unlock(L); lua_lock(L);}
232 #endif
233 
234 
235 /*
236 ** these macros allow user-specific actions on threads when you defined
237 ** LUAI_EXTRASPACE and need to do something extra when a thread is
238 ** created/deleted/resumed/yielded.
239 */
240 #if !defined(luai_userstateopen)
241 #define luai_userstateopen(L)		((void)L)
242 #endif
243 
244 #if !defined(luai_userstateclose)
245 #define luai_userstateclose(L)		((void)L)
246 #endif
247 
248 #if !defined(luai_userstatethread)
249 #define luai_userstatethread(L,L1)	((void)L)
250 #endif
251 
252 #if !defined(luai_userstatefree)
253 #define luai_userstatefree(L,L1)	((void)L)
254 #endif
255 
256 #if !defined(luai_userstateresume)
257 #define luai_userstateresume(L,n)	((void)L)
258 #endif
259 
260 #if !defined(luai_userstateyield)
261 #define luai_userstateyield(L,n)	((void)L)
262 #endif
263 
264 
265 
266 /*
267 ** The luai_num* macros define the primitive operations over numbers.
268 */
269 
270 /* floor division (defined as 'floor(a/b)') */
271 #if !defined(luai_numidiv)
272 #define luai_numidiv(L,a,b)     ((void)L, l_floor(luai_numdiv(L,a,b)))
273 #endif
274 
275 /* float division */
276 #if !defined(luai_numdiv)
277 #define luai_numdiv(L,a,b)      ((a)/(b))
278 #endif
279 
280 /*
281 ** modulo: defined as 'a - floor(a/b)*b'; this definition gives NaN when
282 ** 'b' is huge, but the result should be 'a'. 'fmod' gives the result of
283 ** 'a - trunc(a/b)*b', and therefore must be corrected when 'trunc(a/b)
284 ** ~= floor(a/b)'. That happens when the division has a non-integer
285 ** negative result, which is equivalent to the test below.
286 */
287 #if !defined(luai_nummod)
288 #define luai_nummod(L,a,b,m)  \
289   { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); }
290 #endif
291 
292 /* exponentiation */
293 #if !defined(luai_numpow)
294 #define luai_numpow(L,a,b)      ((void)L, l_mathop(pow)(a,b))
295 #endif
296 
297 /* the others are quite standard operations */
298 #if !defined(luai_numadd)
299 #define luai_numadd(L,a,b)      ((a)+(b))
300 #define luai_numsub(L,a,b)      ((a)-(b))
301 #define luai_nummul(L,a,b)      ((a)*(b))
302 #define luai_numunm(L,a)        (-(a))
303 #define luai_numeq(a,b)         ((a)==(b))
304 #define luai_numlt(a,b)         ((a)<(b))
305 #define luai_numle(a,b)         ((a)<=(b))
306 #define luai_numisnan(a)        (!luai_numeq((a), (a)))
307 #endif
308 
309 
310 
311 
312 
313 /*
314 ** macro to control inclusion of some hard tests on stack reallocation
315 */
316 #if !defined(HARDSTACKTESTS)
317 #define condmovestack(L,pre,pos)	((void)0)
318 #else
319 /* realloc stack keeping its size */
320 #define condmovestack(L,pre,pos)  \
321 	{ int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_); pos; }
322 #endif
323 
324 #if !defined(HARDMEMTESTS)
325 #define condchangemem(L,pre,pos)	((void)0)
326 #else
327 #define condchangemem(L,pre,pos)  \
328 	{ if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } }
329 #endif
330 
331 #endif
332