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
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.
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;
34 /* chars used as small naturals (so that 'char' is reserved for characters) */
35 typedef unsigned char lu_byte;
38 /* maximum value for size_t */
39 #define MAX_SIZET ((size_t)(~(size_t)0))
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))
46 #define MAX_LUMEM ((lu_mem)(~(lu_mem)0))
48 #define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1))
51 #define MAX_INT INT_MAX /* maximum value of an int */
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
59 #define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX))
63 /* type to ensure maximum alignment */
64 #if defined(LUAI_USER_ALIGNMENT_T)
65 typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
69 #if LUA_FLOAT_TYPE != LUA_FLOAT_INT64
80 /* types of 'usual argument conversions' for lua_Number and lua_Integer */
81 typedef LUAI_UACNUMBER l_uacNumber;
82 typedef LUAI_UACINT l_uacInt;
85 /* internal assertions for in-house debugging */
86 #if defined(lua_assert)
87 #define check_exp(c,e) (lua_assert(c), (e))
88 /* to avoid problems with conditions too long */
89 #define lua_longassert(c) ((c) ? (void)0 : lua_assert(0))
91 #define lua_assert(c) ((void)0)
92 #define check_exp(c,e) (e)
93 #define lua_longassert(c) ((void)0)
97 ** assertion for checking API calls
99 #if !defined(luai_apicheck)
100 #define luai_apicheck(l,e) lua_assert(e)
103 #define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
106 /* macro to avoid warnings about unused variables */
108 #define UNUSED(x) ((void)(x))
112 /* type casts (a macro highlights casts in the code) */
113 #define cast(t, exp) ((t)(exp))
115 #define cast_void(i) cast(void, (i))
116 #define cast_byte(i) cast(lu_byte, (i))
117 #define cast_num(i) cast(lua_Number, (i))
118 #define cast_int(i) cast(int, (i))
119 #define cast_uchar(i) cast(unsigned char, (i))
122 /* cast a signed lua_Integer to lua_Unsigned */
123 #if !defined(l_castS2U)
124 #define l_castS2U(i) ((lua_Unsigned)(i))
128 ** cast a lua_Unsigned to a signed lua_Integer; this cast is
129 ** not strict ISO C, but two-complement architectures should
132 #if !defined(l_castU2S)
133 #define l_castU2S(i) ((lua_Integer)(i))
140 #if defined(__GNUC__)
141 #define l_noret void __attribute__((noreturn))
142 #elif defined(_MSC_VER) && _MSC_VER >= 1200
143 #define l_noret void __declspec(noreturn)
151 ** maximum depth for nested C calls and syntactical nested non-terminals
152 ** in a program. (Value must fit in an unsigned short int.)
154 #if !defined(LUAI_MAXCCALLS)
155 #define LUAI_MAXCCALLS 200
161 ** type for virtual-machine instructions;
162 ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
164 #if LUAI_BITSINT >= 32
165 typedef unsigned int Instruction;
167 typedef unsigned long Instruction;
173 ** Maximum length for short strings, that is, strings that are
174 ** internalized. (Cannot be smaller than reserved words or tags for
175 ** metamethods, as these strings must be internalized;
176 ** #("function") = 8, #("__newindex") = 10.)
178 #if !defined(LUAI_MAXSHORTLEN)
179 #define LUAI_MAXSHORTLEN 40
184 ** Initial size for the string table (must be power of 2).
185 ** The Lua core alone registers ~50 strings (reserved words +
186 ** metaevent keys + a few others). Libraries would typically add
187 ** a few dozens more.
189 #if !defined(MINSTRTABSIZE)
190 #define MINSTRTABSIZE 128
195 ** Size of cache for strings in the API. 'N' is the number of
196 ** sets (better be a prime) and "M" is the size of each set (M == 1
197 ** makes a direct cache.)
199 #if !defined(STRCACHE_N)
200 #define STRCACHE_N 53
205 /* minimum size for string buffer */
206 #if !defined(LUA_MINBUFFER)
207 #define LUA_MINBUFFER 32
212 ** macros that are executed whenever program enters the Lua core
213 ** ('lua_lock') and leaves the core ('lua_unlock')
215 #if !defined(lua_lock)
216 #define lua_lock(L) ((void) 0)
217 #define lua_unlock(L) ((void) 0)
221 ** macro executed during Lua functions at points where the
222 ** function can yield.
224 #if !defined(luai_threadyield)
225 #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
230 ** these macros allow user-specific actions on threads when you defined
231 ** LUAI_EXTRASPACE and need to do something extra when a thread is
232 ** created/deleted/resumed/yielded.
234 #if !defined(luai_userstateopen)
235 #define luai_userstateopen(L) ((void)L)
238 #if !defined(luai_userstateclose)
239 #define luai_userstateclose(L) ((void)L)
242 #if !defined(luai_userstatethread)
243 #define luai_userstatethread(L,L1) ((void)L)
246 #if !defined(luai_userstatefree)
247 #define luai_userstatefree(L,L1) ((void)L)
250 #if !defined(luai_userstateresume)
251 #define luai_userstateresume(L,n) ((void)L)
254 #if !defined(luai_userstateyield)
255 #define luai_userstateyield(L,n) ((void)L)
261 ** The luai_num* macros define the primitive operations over numbers.
264 /* floor division (defined as 'floor(a/b)') */
265 #if !defined(luai_numidiv)
266 #define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b)))
270 #if !defined(luai_numdiv)
271 #define luai_numdiv(L,a,b) ((a)/(b))
275 ** modulo: defined as 'a - floor(a/b)*b'; this definition gives NaN when
276 ** 'b' is huge, but the result should be 'a'. 'fmod' gives the result of
277 ** 'a - trunc(a/b)*b', and therefore must be corrected when 'trunc(a/b)
278 ** ~= floor(a/b)'. That happens when the division has a non-integer
279 ** negative result, which is equivalent to the test below.
281 #if !defined(luai_nummod)
282 #define luai_nummod(L,a,b,m) \
283 { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); }
287 #if !defined(luai_numpow)
288 #define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b))
291 /* the others are quite standard operations */
292 #if !defined(luai_numadd)
293 #define luai_numadd(L,a,b) ((a)+(b))
294 #define luai_numsub(L,a,b) ((a)-(b))
295 #define luai_nummul(L,a,b) ((a)*(b))
296 #define luai_numunm(L,a) (-(a))
297 #define luai_numeq(a,b) ((a)==(b))
298 #define luai_numlt(a,b) ((a)<(b))
299 #define luai_numle(a,b) ((a)<=(b))
300 #define luai_numisnan(a) (!luai_numeq((a), (a)))
308 ** macro to control inclusion of some hard tests on stack reallocation
310 #if !defined(HARDSTACKTESTS)
311 #define condmovestack(L,pre,pos) ((void)0)
313 /* realloc stack keeping its size */
314 #define condmovestack(L,pre,pos) \
315 { int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_); pos; }
318 #if !defined(HARDMEMTESTS)
319 #define condchangemem(L,pre,pos) ((void)0)
321 #define condchangemem(L,pre,pos) \
322 { if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } }