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