aboutsummaryrefslogtreecommitdiff
path: root/src/lua/llimits.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/llimits.h')
-rw-r--r--src/lua/llimits.h349
1 files changed, 349 insertions, 0 deletions
diff --git a/src/lua/llimits.h b/src/lua/llimits.h
new file mode 100644
index 0000000..b86d345
--- /dev/null
+++ b/src/lua/llimits.h
@@ -0,0 +1,349 @@
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/* internal assertions for in-house debugging */
88#if defined(lua_assert)
89#define check_exp(c,e) (lua_assert(c), (e))
90/* to avoid problems with conditions too long */
91#define lua_longassert(c) ((c) ? (void)0 : lua_assert(0))
92#else
93#define lua_assert(c) ((void)0)
94#define check_exp(c,e) (e)
95#define lua_longassert(c) ((void)0)
96#endif
97
98/*
99** assertion for checking API calls
100*/
101#if !defined(luai_apicheck)
102#define luai_apicheck(l,e) ((void)l, lua_assert(e))
103#endif
104
105#define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
106
107
108/* macro to avoid warnings about unused variables */
109#if !defined(UNUSED)
110#define UNUSED(x) ((void)(x))
111#endif
112
113
114/* type casts (a macro highlights casts in the code) */
115#define cast(t, exp) ((t)(exp))
116
117#define cast_void(i) cast(void, (i))
118#define cast_voidp(i) cast(void *, (i))
119#define cast_num(i) cast(lua_Number, (i))
120#define cast_int(i) cast(int, (i))
121#define cast_uint(i) cast(unsigned int, (i))
122#define cast_byte(i) cast(lu_byte, (i))
123#define cast_uchar(i) cast(unsigned char, (i))
124#define cast_char(i) cast(char, (i))
125#define cast_charp(i) cast(char *, (i))
126#define cast_sizet(i) cast(size_t, (i))
127
128
129/* cast a signed lua_Integer to lua_Unsigned */
130#if !defined(l_castS2U)
131#define l_castS2U(i) ((lua_Unsigned)(i))
132#endif
133
134/*
135** cast a lua_Unsigned to a signed lua_Integer; this cast is
136** not strict ISO C, but two-complement architectures should
137** work fine.
138*/
139#if !defined(l_castU2S)
140#define l_castU2S(i) ((lua_Integer)(i))
141#endif
142
143
144/*
145** macros to improve jump prediction (used mainly for error handling)
146*/
147#if !defined(likely)
148
149#if defined(__GNUC__)
150#define likely(x) (__builtin_expect(((x) != 0), 1))
151#define unlikely(x) (__builtin_expect(((x) != 0), 0))
152#else
153#define likely(x) (x)
154#define unlikely(x) (x)
155#endif
156
157#endif
158
159
160/*
161** non-return type
162*/
163#if !defined(l_noret)
164
165#if defined(__GNUC__)
166#define l_noret void __attribute__((noreturn))
167#elif defined(_MSC_VER) && _MSC_VER >= 1200
168#define l_noret void __declspec(noreturn)
169#else
170#define l_noret void
171#endif
172
173#endif
174
175
176/*
177** type for virtual-machine instructions;
178** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
179*/
180#if LUAI_IS32INT
181typedef unsigned int l_uint32;
182#else
183typedef unsigned long l_uint32;
184#endif
185
186typedef l_uint32 Instruction;
187
188
189
190/*
191** Maximum length for short strings, that is, strings that are
192** internalized. (Cannot be smaller than reserved words or tags for
193** metamethods, as these strings must be internalized;
194** #("function") = 8, #("__newindex") = 10.)
195*/
196#if !defined(LUAI_MAXSHORTLEN)
197#define LUAI_MAXSHORTLEN 40
198#endif
199
200
201/*
202** Initial size for the string table (must be power of 2).
203** The Lua core alone registers ~50 strings (reserved words +
204** metaevent keys + a few others). Libraries would typically add
205** a few dozens more.
206*/
207#if !defined(MINSTRTABSIZE)
208#define MINSTRTABSIZE 128
209#endif
210
211
212/*
213** Size of cache for strings in the API. 'N' is the number of
214** sets (better be a prime) and "M" is the size of each set (M == 1
215** makes a direct cache.)
216*/
217#if !defined(STRCACHE_N)
218#define STRCACHE_N 53
219#define STRCACHE_M 2
220#endif
221
222
223/* minimum size for string buffer */
224#if !defined(LUA_MINBUFFER)
225#define LUA_MINBUFFER 32
226#endif
227
228
229/*
230** macros that are executed whenever program enters the Lua core
231** ('lua_lock') and leaves the core ('lua_unlock')
232*/
233#if !defined(lua_lock)
234#define lua_lock(L) ((void) 0)
235#define lua_unlock(L) ((void) 0)
236#endif
237
238/*
239** macro executed during Lua functions at points where the
240** function can yield.
241*/
242#if !defined(luai_threadyield)
243#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
244#endif
245
246
247/*
248** these macros allow user-specific actions when a thread is
249** created/deleted/resumed/yielded.
250*/
251#if !defined(luai_userstateopen)
252#define luai_userstateopen(L) ((void)L)
253#endif
254
255#if !defined(luai_userstateclose)
256#define luai_userstateclose(L) ((void)L)
257#endif
258
259#if !defined(luai_userstatethread)
260#define luai_userstatethread(L,L1) ((void)L)
261#endif
262
263#if !defined(luai_userstatefree)
264#define luai_userstatefree(L,L1) ((void)L)
265#endif
266
267#if !defined(luai_userstateresume)
268#define luai_userstateresume(L,n) ((void)L)
269#endif
270
271#if !defined(luai_userstateyield)
272#define luai_userstateyield(L,n) ((void)L)
273#endif
274
275
276
277/*
278** The luai_num* macros define the primitive operations over numbers.
279*/
280
281/* floor division (defined as 'floor(a/b)') */
282#if !defined(luai_numidiv)
283#define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b)))
284#endif
285
286/* float division */
287#if !defined(luai_numdiv)
288#define luai_numdiv(L,a,b) ((a)/(b))
289#endif
290
291/*
292** modulo: defined as 'a - floor(a/b)*b'; the direct computation
293** using this definition has several problems with rounding errors,
294** so it is better to use 'fmod'. 'fmod' gives the result of
295** 'a - trunc(a/b)*b', and therefore must be corrected when
296** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a
297** non-integer negative result: non-integer result is equivalent to
298** a non-zero remainder 'm'; negative result is equivalent to 'a' and
299** 'b' with different signs, or 'm' and 'b' with different signs
300** (as the result 'm' of 'fmod' has the same sign of 'a').
301*/
302#if !defined(luai_nummod)
303#define luai_nummod(L,a,b,m) \
304 { (void)L; (m) = l_mathop(fmod)(a,b); \
305 if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }
306#endif
307
308/* exponentiation */
309#if !defined(luai_numpow)
310#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b))
311#endif
312
313/* the others are quite standard operations */
314#if !defined(luai_numadd)
315#define luai_numadd(L,a,b) ((a)+(b))
316#define luai_numsub(L,a,b) ((a)-(b))
317#define luai_nummul(L,a,b) ((a)*(b))
318#define luai_numunm(L,a) (-(a))
319#define luai_numeq(a,b) ((a)==(b))
320#define luai_numlt(a,b) ((a)<(b))
321#define luai_numle(a,b) ((a)<=(b))
322#define luai_numgt(a,b) ((a)>(b))
323#define luai_numge(a,b) ((a)>=(b))
324#define luai_numisnan(a) (!luai_numeq((a), (a)))
325#endif
326
327
328
329
330
331/*
332** macro to control inclusion of some hard tests on stack reallocation
333*/
334#if !defined(HARDSTACKTESTS)
335#define condmovestack(L,pre,pos) ((void)0)
336#else
337/* realloc stack keeping its size */
338#define condmovestack(L,pre,pos) \
339 { int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_, 0); pos; }
340#endif
341
342#if !defined(HARDMEMTESTS)
343#define condchangemem(L,pre,pos) ((void)0)
344#else
345#define condchangemem(L,pre,pos) \
346 { if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } }
347#endif
348
349#endif