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