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** type for virtual-machine instructions;
170** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
171*/
172#if LUAI_IS32INT
173typedef unsigned int l_uint32;
174#else
175typedef unsigned long l_uint32;
176#endif
177
178typedef l_uint32 Instruction;
179
180
181
182/*
183** Maximum length for short strings, that is, strings that are
184** internalized. (Cannot be smaller than reserved words or tags for
185** metamethods, as these strings must be internalized;
186** #("function") = 8, #("__newindex") = 10.)
187*/
188#if !defined(LUAI_MAXSHORTLEN)
189#define LUAI_MAXSHORTLEN 40
190#endif
191
192
193/*
194** Initial size for the string table (must be power of 2).
195** The Lua core alone registers ~50 strings (reserved words +
196** metaevent keys + a few others). Libraries would typically add
197** a few dozens more.
198*/
199#if !defined(MINSTRTABSIZE)
200#define MINSTRTABSIZE 128
201#endif
202
203
204/*
205** Size of cache for strings in the API. 'N' is the number of
206** sets (better be a prime) and "M" is the size of each set (M == 1
207** makes a direct cache.)
208*/
209#if !defined(STRCACHE_N)
210#define STRCACHE_N 53
211#define STRCACHE_M 2
212#endif
213
214
215/* minimum size for string buffer */
216#if !defined(LUA_MINBUFFER)
217#define LUA_MINBUFFER 32
218#endif
219
220
221/*
222** Maximum depth for nested C calls, syntactical nested non-terminals,
223** and other features implemented through recursion in C. (Value must
224** fit in a 16-bit unsigned integer. It must also be compatible with
225** the size of the C stack.)
226*/
227#if !defined(LUAI_MAXCCALLS)
228#define LUAI_MAXCCALLS 200
229#endif
230
231
232/*
233** macros that are executed whenever program enters the Lua core
234** ('lua_lock') and leaves the core ('lua_unlock')
235*/
236#if !defined(lua_lock)
237#define lua_lock(L) ((void) 0)
238#define lua_unlock(L) ((void) 0)
239#endif
240
241/*
242** macro executed during Lua functions at points where the
243** function can yield.
244*/
245#if !defined(luai_threadyield)
246#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
247#endif
248
249
250/*
251** these macros allow user-specific actions when a thread is
252** created/deleted/resumed/yielded.
253*/
254#if !defined(luai_userstateopen)
255#define luai_userstateopen(L) ((void)L)
256#endif
257
258#if !defined(luai_userstateclose)
259#define luai_userstateclose(L) ((void)L)
260#endif
261
262#if !defined(luai_userstatethread)
263#define luai_userstatethread(L,L1) ((void)L)
264#endif
265
266#if !defined(luai_userstatefree)
267#define luai_userstatefree(L,L1) ((void)L)
268#endif
269
270#if !defined(luai_userstateresume)
271#define luai_userstateresume(L,n) ((void)L)
272#endif
273
274#if !defined(luai_userstateyield)
275#define luai_userstateyield(L,n) ((void)L)
276#endif
277
278
279
280/*
281** The luai_num* macros define the primitive operations over numbers.
282*/
283
284/* floor division (defined as 'floor(a/b)') */
285#if !defined(luai_numidiv)
286#define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b)))
287#endif
288
289/* float division */
290#if !defined(luai_numdiv)
291#define luai_numdiv(L,a,b) ((a)/(b))
292#endif
293
294/*
295** modulo: defined as 'a - floor(a/b)*b'; the direct computation
296** using this definition has several problems with rounding errors,
297** so it is better to use 'fmod'. 'fmod' gives the result of
298** 'a - trunc(a/b)*b', and therefore must be corrected when
299** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a
300** non-integer negative result: non-integer result is equivalent to
301** a non-zero remainder 'm'; negative result is equivalent to 'a' and
302** 'b' with different signs, or 'm' and 'b' with different signs
303** (as the result 'm' of 'fmod' has the same sign of 'a').
304*/
305#if !defined(luai_nummod)
306#define luai_nummod(L,a,b,m) \
307 { (void)L; (m) = l_mathop(fmod)(a,b); \
308 if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }
309#endif
310
311/* exponentiation */
312#if !defined(luai_numpow)
313#define luai_numpow(L,a,b) \
314 ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b))
315#endif
316
317/* the others are quite standard operations */
318#if !defined(luai_numadd)
319#define luai_numadd(L,a,b) ((a)+(b))
320#define luai_numsub(L,a,b) ((a)-(b))
321#define luai_nummul(L,a,b) ((a)*(b))
322#define luai_numunm(L,a) (-(a))
323#define luai_numeq(a,b) ((a)==(b))
324#define luai_numlt(a,b) ((a)<(b))
325#define luai_numle(a,b) ((a)<=(b))
326#define luai_numgt(a,b) ((a)>(b))
327#define luai_numge(a,b) ((a)>=(b))
328#define luai_numisnan(a) (!luai_numeq((a), (a)))
329#endif
330
331
332
333
334
335/*
336** macro to control inclusion of some hard tests on stack reallocation
337*/
338#if !defined(HARDSTACKTESTS)
339#define condmovestack(L,pre,pos) ((void)0)
340#else
341/* realloc stack keeping its size */
342#define condmovestack(L,pre,pos) \
343 { int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; }
344#endif
345
346#if !defined(HARDMEMTESTS)
347#define condchangemem(L,pre,pos) ((void)0)
348#else
349#define condchangemem(L,pre,pos) \
350 { if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } }
351#endif
352
353#endif
354