xref: /freebsd/contrib/lua/src/llimits.h (revision a9490b81)
1 /*
2 ** $Id: llimits.h $
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 /*
19 ** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count
20 ** the total memory used by Lua (in bytes). Usually, 'size_t' and
21 ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
22 */
23 #if defined(LUAI_MEM)		/* { external definitions? */
24 typedef LUAI_UMEM lu_mem;
25 typedef LUAI_MEM l_mem;
26 #elif LUAI_IS32INT	/* }{ */
27 typedef size_t lu_mem;
28 typedef ptrdiff_t l_mem;
29 #else  /* 16-bit ints */	/* }{ */
30 typedef unsigned long lu_mem;
31 typedef long l_mem;
32 #endif				/* } */
33 
34 
35 /* chars used as small naturals (so that 'char' is reserved for characters) */
36 typedef unsigned char lu_byte;
37 typedef signed char ls_byte;
38 
39 
40 /* maximum value for size_t */
41 #define MAX_SIZET	((size_t)(~(size_t)0))
42 
43 /* maximum size visible for Lua (must be representable in a lua_Integer) */
44 #define MAX_SIZE	(sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
45                           : (size_t)(LUA_MAXINTEGER))
46 
47 
48 #define MAX_LUMEM	((lu_mem)(~(lu_mem)0))
49 
50 #define MAX_LMEM	((l_mem)(MAX_LUMEM >> 1))
51 
52 
53 #define MAX_INT		INT_MAX  /* maximum value of an int */
54 
55 
56 /*
57 ** floor of the log2 of the maximum signed value for integral type 't'.
58 ** (That is, maximum 'n' such that '2^n' fits in the given signed type.)
59 */
60 #define log2maxs(t)	(sizeof(t) * 8 - 2)
61 
62 
63 /*
64 ** test whether an unsigned value is a power of 2 (or zero)
65 */
66 #define ispow2(x)	(((x) & ((x) - 1)) == 0)
67 
68 
69 /* number of chars of a literal string without the ending \0 */
70 #define LL(x)   (sizeof(x)/sizeof(char) - 1)
71 
72 
73 /*
74 ** conversion of pointer to unsigned integer: this is for hashing only;
75 ** there is no problem if the integer cannot hold the whole pointer
76 ** value. (In strict ISO C this may cause undefined behavior, but no
77 ** actual machine seems to bother.)
78 */
79 #if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \
80     __STDC_VERSION__ >= 199901L
81 #include <stdint.h>
82 #if defined(UINTPTR_MAX)  /* even in C99 this type is optional */
83 #define L_P2I	uintptr_t
84 #else  /* no 'intptr'? */
85 #define L_P2I	uintmax_t  /* use the largest available integer */
86 #endif
87 #else  /* C89 option */
88 #define L_P2I	size_t
89 #endif
90 
91 #define point2uint(p)	((unsigned int)((L_P2I)(p) & UINT_MAX))
92 
93 
94 
95 /* types of 'usual argument conversions' for lua_Number and lua_Integer */
96 typedef LUAI_UACNUMBER l_uacNumber;
97 typedef LUAI_UACINT l_uacInt;
98 
99 
100 /*
101 ** Internal assertions for in-house debugging
102 */
103 #if defined LUAI_ASSERT
104 #undef NDEBUG
105 #include <assert.h>
106 #define lua_assert(c)           assert(c)
107 #endif
108 
109 #if defined(lua_assert)
110 #define check_exp(c,e)		(lua_assert(c), (e))
111 /* to avoid problems with conditions too long */
112 #define lua_longassert(c)	((c) ? (void)0 : lua_assert(0))
113 #else
114 #define lua_assert(c)		((void)0)
115 #define check_exp(c,e)		(e)
116 #define lua_longassert(c)	((void)0)
117 #endif
118 
119 /*
120 ** assertion for checking API calls
121 */
122 #if !defined(luai_apicheck)
123 #define luai_apicheck(l,e)	((void)l, lua_assert(e))
124 #endif
125 
126 #define api_check(l,e,msg)	luai_apicheck(l,(e) && msg)
127 
128 
129 /* macro to avoid warnings about unused variables */
130 #if !defined(UNUSED)
131 #define UNUSED(x)	((void)(x))
132 #endif
133 
134 
135 /* type casts (a macro highlights casts in the code) */
136 #define cast(t, exp)	((t)(exp))
137 
138 #define cast_void(i)	cast(void, (i))
139 #define cast_voidp(i)	cast(void *, (i))
140 #define cast_num(i)	cast(lua_Number, (i))
141 #define cast_int(i)	cast(int, (i))
142 #define cast_uint(i)	cast(unsigned int, (i))
143 #define cast_byte(i)	cast(lu_byte, (i))
144 #define cast_uchar(i)	cast(unsigned char, (i))
145 #define cast_char(i)	cast(char, (i))
146 #define cast_charp(i)	cast(char *, (i))
147 #define cast_sizet(i)	cast(size_t, (i))
148 
149 
150 /* cast a signed lua_Integer to lua_Unsigned */
151 #if !defined(l_castS2U)
152 #define l_castS2U(i)	((lua_Unsigned)(i))
153 #endif
154 
155 /*
156 ** cast a lua_Unsigned to a signed lua_Integer; this cast is
157 ** not strict ISO C, but two-complement architectures should
158 ** work fine.
159 */
160 #if !defined(l_castU2S)
161 #define l_castU2S(i)	((lua_Integer)(i))
162 #endif
163 
164 
165 /*
166 ** non-return type
167 */
168 #if !defined(l_noret)
169 
170 #if defined(__GNUC__)
171 #define l_noret		void __attribute__((noreturn))
172 #elif defined(_MSC_VER) && _MSC_VER >= 1200
173 #define l_noret		void __declspec(noreturn)
174 #else
175 #define l_noret		void
176 #endif
177 
178 #endif
179 
180 
181 /*
182 ** Inline functions
183 */
184 #if !defined(LUA_USE_C89)
185 #define l_inline	inline
186 #elif defined(__GNUC__)
187 #define l_inline	__inline__
188 #else
189 #define l_inline	/* empty */
190 #endif
191 
192 #define l_sinline	static l_inline
193 
194 
195 /*
196 ** type for virtual-machine instructions;
197 ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
198 */
199 #if LUAI_IS32INT
200 typedef unsigned int l_uint32;
201 #else
202 typedef unsigned long l_uint32;
203 #endif
204 
205 typedef l_uint32 Instruction;
206 
207 
208 
209 /*
210 ** Maximum length for short strings, that is, strings that are
211 ** internalized. (Cannot be smaller than reserved words or tags for
212 ** metamethods, as these strings must be internalized;
213 ** #("function") = 8, #("__newindex") = 10.)
214 */
215 #if !defined(LUAI_MAXSHORTLEN)
216 #define LUAI_MAXSHORTLEN	40
217 #endif
218 
219 
220 /*
221 ** Initial size for the string table (must be power of 2).
222 ** The Lua core alone registers ~50 strings (reserved words +
223 ** metaevent keys + a few others). Libraries would typically add
224 ** a few dozens more.
225 */
226 #if !defined(MINSTRTABSIZE)
227 #define MINSTRTABSIZE	128
228 #endif
229 
230 
231 /*
232 ** Size of cache for strings in the API. 'N' is the number of
233 ** sets (better be a prime) and "M" is the size of each set (M == 1
234 ** makes a direct cache.)
235 */
236 #if !defined(STRCACHE_N)
237 #define STRCACHE_N		53
238 #define STRCACHE_M		2
239 #endif
240 
241 
242 /* minimum size for string buffer */
243 #if !defined(LUA_MINBUFFER)
244 #define LUA_MINBUFFER	32
245 #endif
246 
247 
248 /*
249 ** Maximum depth for nested C calls, syntactical nested non-terminals,
250 ** and other features implemented through recursion in C. (Value must
251 ** fit in a 16-bit unsigned integer. It must also be compatible with
252 ** the size of the C stack.)
253 */
254 #if !defined(LUAI_MAXCCALLS)
255 #define LUAI_MAXCCALLS		200
256 #endif
257 
258 
259 /*
260 ** macros that are executed whenever program enters the Lua core
261 ** ('lua_lock') and leaves the core ('lua_unlock')
262 */
263 #if !defined(lua_lock)
264 #define lua_lock(L)	((void) 0)
265 #define lua_unlock(L)	((void) 0)
266 #endif
267 
268 /*
269 ** macro executed during Lua functions at points where the
270 ** function can yield.
271 */
272 #if !defined(luai_threadyield)
273 #define luai_threadyield(L)	{lua_unlock(L); lua_lock(L);}
274 #endif
275 
276 
277 /*
278 ** these macros allow user-specific actions when a thread is
279 ** created/deleted/resumed/yielded.
280 */
281 #if !defined(luai_userstateopen)
282 #define luai_userstateopen(L)		((void)L)
283 #endif
284 
285 #if !defined(luai_userstateclose)
286 #define luai_userstateclose(L)		((void)L)
287 #endif
288 
289 #if !defined(luai_userstatethread)
290 #define luai_userstatethread(L,L1)	((void)L)
291 #endif
292 
293 #if !defined(luai_userstatefree)
294 #define luai_userstatefree(L,L1)	((void)L)
295 #endif
296 
297 #if !defined(luai_userstateresume)
298 #define luai_userstateresume(L,n)	((void)L)
299 #endif
300 
301 #if !defined(luai_userstateyield)
302 #define luai_userstateyield(L,n)	((void)L)
303 #endif
304 
305 
306 
307 /*
308 ** The luai_num* macros define the primitive operations over numbers.
309 */
310 
311 /* floor division (defined as 'floor(a/b)') */
312 #if !defined(luai_numidiv)
313 #define luai_numidiv(L,a,b)     ((void)L, l_floor(luai_numdiv(L,a,b)))
314 #endif
315 
316 /* float division */
317 #if !defined(luai_numdiv)
318 #define luai_numdiv(L,a,b)      ((a)/(b))
319 #endif
320 
321 /*
322 ** modulo: defined as 'a - floor(a/b)*b'; the direct computation
323 ** using this definition has several problems with rounding errors,
324 ** so it is better to use 'fmod'. 'fmod' gives the result of
325 ** 'a - trunc(a/b)*b', and therefore must be corrected when
326 ** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a
327 ** non-integer negative result: non-integer result is equivalent to
328 ** a non-zero remainder 'm'; negative result is equivalent to 'a' and
329 ** 'b' with different signs, or 'm' and 'b' with different signs
330 ** (as the result 'm' of 'fmod' has the same sign of 'a').
331 */
332 #if !defined(luai_nummod)
333 #define luai_nummod(L,a,b,m)  \
334   { (void)L; (m) = l_mathop(fmod)(a,b); \
335     if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }
336 #endif
337 
338 /* exponentiation */
339 #if !defined(luai_numpow)
340 #define luai_numpow(L,a,b)  \
341   ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b))
342 #endif
343 
344 /* the others are quite standard operations */
345 #if !defined(luai_numadd)
346 #define luai_numadd(L,a,b)      ((a)+(b))
347 #define luai_numsub(L,a,b)      ((a)-(b))
348 #define luai_nummul(L,a,b)      ((a)*(b))
349 #define luai_numunm(L,a)        (-(a))
350 #define luai_numeq(a,b)         ((a)==(b))
351 #define luai_numlt(a,b)         ((a)<(b))
352 #define luai_numle(a,b)         ((a)<=(b))
353 #define luai_numgt(a,b)         ((a)>(b))
354 #define luai_numge(a,b)         ((a)>=(b))
355 #define luai_numisnan(a)        (!luai_numeq((a), (a)))
356 #endif
357 
358 
359 
360 
361 
362 /*
363 ** macro to control inclusion of some hard tests on stack reallocation
364 */
365 #if !defined(HARDSTACKTESTS)
366 #define condmovestack(L,pre,pos)	((void)0)
367 #else
368 /* realloc stack keeping its size */
369 #define condmovestack(L,pre,pos)  \
370   { int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; }
371 #endif
372 
373 #if !defined(HARDMEMTESTS)
374 #define condchangemem(L,pre,pos)	((void)0)
375 #else
376 #define condchangemem(L,pre,pos)  \
377 	{ if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } }
378 #endif
379 
380 #endif
381