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   double u;
70   void *s;
71   lua_Integer i;
72   long l;
73 } L_Umaxalign;
74 #endif
75 
76 
77 
78 /* types of 'usual argument conversions' for lua_Number and lua_Integer */
79 typedef LUAI_UACNUMBER l_uacNumber;
80 typedef LUAI_UACINT l_uacInt;
81 
82 
83 /* internal assertions for in-house debugging */
84 #if defined(lua_assert)
85 #define check_exp(c,e)		(lua_assert(c), (e))
86 /* to avoid problems with conditions too long */
87 #define lua_longassert(c)	((c) ? (void)0 : lua_assert(0))
88 #else
89 #define lua_assert(c)		((void)0)
90 #define check_exp(c,e)		(e)
91 #define lua_longassert(c)	((void)0)
92 #endif
93 
94 /*
95 ** assertion for checking API calls
96 */
97 #if !defined(luai_apicheck)
98 #define luai_apicheck(l,e)	lua_assert(e)
99 #endif
100 
101 #define api_check(l,e,msg)	luai_apicheck(l,(e) && msg)
102 
103 
104 /* macro to avoid warnings about unused variables */
105 #if !defined(UNUSED)
106 #define UNUSED(x)	((void)(x))
107 #endif
108 
109 
110 /* type casts (a macro highlights casts in the code) */
111 #define cast(t, exp)	((t)(exp))
112 
113 #define cast_void(i)	cast(void, (i))
114 #define cast_byte(i)	cast(lu_byte, (i))
115 #define cast_num(i)	cast(lua_Number, (i))
116 #define cast_int(i)	cast(int, (i))
117 #define cast_uchar(i)	cast(unsigned char, (i))
118 
119 
120 /* cast a signed lua_Integer to lua_Unsigned */
121 #if !defined(l_castS2U)
122 #define l_castS2U(i)	((lua_Unsigned)(i))
123 #endif
124 
125 /*
126 ** cast a lua_Unsigned to a signed lua_Integer; this cast is
127 ** not strict ISO C, but two-complement architectures should
128 ** work fine.
129 */
130 #if !defined(l_castU2S)
131 #define l_castU2S(i)	((lua_Integer)(i))
132 #endif
133 
134 
135 /*
136 ** non-return type
137 */
138 #if defined(__GNUC__)
139 #define l_noret		void __attribute__((noreturn))
140 #elif defined(_MSC_VER) && _MSC_VER >= 1200
141 #define l_noret		void __declspec(noreturn)
142 #else
143 #define l_noret		void
144 #endif
145 
146 
147 
148 /*
149 ** maximum depth for nested C calls and syntactical nested non-terminals
150 ** in a program. (Value must fit in an unsigned short int.)
151 */
152 #if !defined(LUAI_MAXCCALLS)
153 #define LUAI_MAXCCALLS		200
154 #endif
155 
156 
157 
158 /*
159 ** type for virtual-machine instructions;
160 ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
161 */
162 #if LUAI_BITSINT >= 32
163 typedef unsigned int Instruction;
164 #else
165 typedef unsigned long Instruction;
166 #endif
167 
168 
169 
170 /*
171 ** Maximum length for short strings, that is, strings that are
172 ** internalized. (Cannot be smaller than reserved words or tags for
173 ** metamethods, as these strings must be internalized;
174 ** #("function") = 8, #("__newindex") = 10.)
175 */
176 #if !defined(LUAI_MAXSHORTLEN)
177 #define LUAI_MAXSHORTLEN	40
178 #endif
179 
180 
181 /*
182 ** Initial size for the string table (must be power of 2).
183 ** The Lua core alone registers ~50 strings (reserved words +
184 ** metaevent keys + a few others). Libraries would typically add
185 ** a few dozens more.
186 */
187 #if !defined(MINSTRTABSIZE)
188 #define MINSTRTABSIZE	128
189 #endif
190 
191 
192 /*
193 ** Size of cache for strings in the API. 'N' is the number of
194 ** sets (better be a prime) and "M" is the size of each set (M == 1
195 ** makes a direct cache.)
196 */
197 #if !defined(STRCACHE_N)
198 #define STRCACHE_N		53
199 #define STRCACHE_M		2
200 #endif
201 
202 
203 /* minimum size for string buffer */
204 #if !defined(LUA_MINBUFFER)
205 #define LUA_MINBUFFER	32
206 #endif
207 
208 
209 /*
210 ** macros that are executed whenever program enters the Lua core
211 ** ('lua_lock') and leaves the core ('lua_unlock')
212 */
213 #if !defined(lua_lock)
214 #define lua_lock(L)	((void) 0)
215 #define lua_unlock(L)	((void) 0)
216 #endif
217 
218 /*
219 ** macro executed during Lua functions at points where the
220 ** function can yield.
221 */
222 #if !defined(luai_threadyield)
223 #define luai_threadyield(L)	{lua_unlock(L); lua_lock(L);}
224 #endif
225 
226 
227 /*
228 ** these macros allow user-specific actions on threads when you defined
229 ** LUAI_EXTRASPACE and need to do something extra when a thread is
230 ** created/deleted/resumed/yielded.
231 */
232 #if !defined(luai_userstateopen)
233 #define luai_userstateopen(L)		((void)L)
234 #endif
235 
236 #if !defined(luai_userstateclose)
237 #define luai_userstateclose(L)		((void)L)
238 #endif
239 
240 #if !defined(luai_userstatethread)
241 #define luai_userstatethread(L,L1)	((void)L)
242 #endif
243 
244 #if !defined(luai_userstatefree)
245 #define luai_userstatefree(L,L1)	((void)L)
246 #endif
247 
248 #if !defined(luai_userstateresume)
249 #define luai_userstateresume(L,n)	((void)L)
250 #endif
251 
252 #if !defined(luai_userstateyield)
253 #define luai_userstateyield(L,n)	((void)L)
254 #endif
255 
256 
257 
258 /*
259 ** The luai_num* macros define the primitive operations over numbers.
260 */
261 
262 /* floor division (defined as 'floor(a/b)') */
263 #if !defined(luai_numidiv)
264 #define luai_numidiv(L,a,b)     ((void)L, l_floor(luai_numdiv(L,a,b)))
265 #endif
266 
267 /* float division */
268 #if !defined(luai_numdiv)
269 #define luai_numdiv(L,a,b)      ((a)/(b))
270 #endif
271 
272 /*
273 ** modulo: defined as 'a - floor(a/b)*b'; this definition gives NaN when
274 ** 'b' is huge, but the result should be 'a'. 'fmod' gives the result of
275 ** 'a - trunc(a/b)*b', and therefore must be corrected when 'trunc(a/b)
276 ** ~= floor(a/b)'. That happens when the division has a non-integer
277 ** negative result, which is equivalent to the test below.
278 */
279 #if !defined(luai_nummod)
280 #define luai_nummod(L,a,b,m)  \
281   { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); }
282 #endif
283 
284 /* exponentiation */
285 #if !defined(luai_numpow)
286 #define luai_numpow(L,a,b)      ((void)L, l_mathop(pow)(a,b))
287 #endif
288 
289 /* the others are quite standard operations */
290 #if !defined(luai_numadd)
291 #define luai_numadd(L,a,b)      ((a)+(b))
292 #define luai_numsub(L,a,b)      ((a)-(b))
293 #define luai_nummul(L,a,b)      ((a)*(b))
294 #define luai_numunm(L,a)        (-(a))
295 #define luai_numeq(a,b)         ((a)==(b))
296 #define luai_numlt(a,b)         ((a)<(b))
297 #define luai_numle(a,b)         ((a)<=(b))
298 #define luai_numisnan(a)        (!luai_numeq((a), (a)))
299 #endif
300 
301 
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,pre,pos)	((void)0)
310 #else
311 /* realloc stack keeping its size */
312 #define condmovestack(L,pre,pos)  \
313 	{ int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_); pos; }
314 #endif
315 
316 #if !defined(HARDMEMTESTS)
317 #define condchangemem(L,pre,pos)	((void)0)
318 #else
319 #define condchangemem(L,pre,pos)  \
320 	{ if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } }
321 #endif
322 
323 #endif
324