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