Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-07 10:37:56

0001 /*
0002 ** $Id: llimits.h $
0003 ** Limits, basic types, and some other 'installation-dependent' definitions
0004 ** See Copyright Notice in lua.h
0005 */
0006 
0007 #ifndef llimits_h
0008 #define llimits_h
0009 
0010 
0011 #include <limits.h>
0012 #include <stddef.h>
0013 
0014 
0015 #include "lua.h"
0016 
0017 
0018 /*
0019 ** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count
0020 ** the total memory used by Lua (in bytes). Usually, 'size_t' and
0021 ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
0022 */
0023 #if defined(LUAI_MEM)       /* { external definitions? */
0024 typedef LUAI_UMEM lu_mem;
0025 typedef LUAI_MEM l_mem;
0026 #elif LUAI_IS32INT  /* }{ */
0027 typedef size_t lu_mem;
0028 typedef ptrdiff_t l_mem;
0029 #else  /* 16-bit ints */    /* }{ */
0030 typedef unsigned long lu_mem;
0031 typedef long l_mem;
0032 #endif              /* } */
0033 
0034 
0035 /* chars used as small naturals (so that 'char' is reserved for characters) */
0036 typedef unsigned char lu_byte;
0037 typedef signed char ls_byte;
0038 
0039 
0040 /* maximum value for size_t */
0041 #define MAX_SIZET   ((size_t)(~(size_t)0))
0042 
0043 /* maximum size visible for Lua (must be representable in a lua_Integer) */
0044 #define MAX_SIZE    (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
0045                           : (size_t)(LUA_MAXINTEGER))
0046 
0047 
0048 #define MAX_LUMEM   ((lu_mem)(~(lu_mem)0))
0049 
0050 #define MAX_LMEM    ((l_mem)(MAX_LUMEM >> 1))
0051 
0052 
0053 #define MAX_INT     INT_MAX  /* maximum value of an int */
0054 
0055 
0056 /*
0057 ** floor of the log2 of the maximum signed value for integral type 't'.
0058 ** (That is, maximum 'n' such that '2^n' fits in the given signed type.)
0059 */
0060 #define log2maxs(t) (sizeof(t) * 8 - 2)
0061 
0062 
0063 /*
0064 ** test whether an unsigned value is a power of 2 (or zero)
0065 */
0066 #define ispow2(x)   (((x) & ((x) - 1)) == 0)
0067 
0068 
0069 /* number of chars of a literal string without the ending \0 */
0070 #define LL(x)   (sizeof(x)/sizeof(char) - 1)
0071 
0072 
0073 /*
0074 ** conversion of pointer to unsigned integer: this is for hashing only;
0075 ** there is no problem if the integer cannot hold the whole pointer
0076 ** value. (In strict ISO C this may cause undefined behavior, but no
0077 ** actual machine seems to bother.)
0078 */
0079 #if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \
0080     __STDC_VERSION__ >= 199901L
0081 #include <stdint.h>
0082 #if defined(UINTPTR_MAX)  /* even in C99 this type is optional */
0083 #define L_P2I   uintptr_t
0084 #else  /* no 'intptr'? */
0085 #define L_P2I   uintmax_t  /* use the largest available integer */
0086 #endif
0087 #else  /* C89 option */
0088 #define L_P2I   size_t
0089 #endif
0090 
0091 #define point2uint(p)   ((unsigned int)((L_P2I)(p) & UINT_MAX))
0092 
0093 
0094 
0095 /* types of 'usual argument conversions' for lua_Number and lua_Integer */
0096 typedef LUAI_UACNUMBER l_uacNumber;
0097 typedef LUAI_UACINT l_uacInt;
0098 
0099 
0100 /*
0101 ** Internal assertions for in-house debugging
0102 */
0103 #if defined LUAI_ASSERT
0104 #undef NDEBUG
0105 #include <assert.h>
0106 #define lua_assert(c)           assert(c)
0107 #endif
0108 
0109 #if defined(lua_assert)
0110 #define check_exp(c,e)      (lua_assert(c), (e))
0111 /* to avoid problems with conditions too long */
0112 #define lua_longassert(c)   ((c) ? (void)0 : lua_assert(0))
0113 #else
0114 #define lua_assert(c)       ((void)0)
0115 #define check_exp(c,e)      (e)
0116 #define lua_longassert(c)   ((void)0)
0117 #endif
0118 
0119 /*
0120 ** assertion for checking API calls
0121 */
0122 #if !defined(luai_apicheck)
0123 #define luai_apicheck(l,e)  ((void)l, lua_assert(e))
0124 #endif
0125 
0126 #define api_check(l,e,msg)  luai_apicheck(l,(e) && msg)
0127 
0128 
0129 /* macro to avoid warnings about unused variables */
0130 #if !defined(UNUSED)
0131 #define UNUSED(x)   ((void)(x))
0132 #endif
0133 
0134 
0135 /* type casts (a macro highlights casts in the code) */
0136 #define cast(t, exp)    ((t)(exp))
0137 
0138 #define cast_void(i)    cast(void, (i))
0139 #define cast_voidp(i)   cast(void *, (i))
0140 #define cast_num(i) cast(lua_Number, (i))
0141 #define cast_int(i) cast(int, (i))
0142 #define cast_uint(i)    cast(unsigned int, (i))
0143 #define cast_byte(i)    cast(lu_byte, (i))
0144 #define cast_uchar(i)   cast(unsigned char, (i))
0145 #define cast_char(i)    cast(char, (i))
0146 #define cast_charp(i)   cast(char *, (i))
0147 #define cast_sizet(i)   cast(size_t, (i))
0148 
0149 
0150 /* cast a signed lua_Integer to lua_Unsigned */
0151 #if !defined(l_castS2U)
0152 #define l_castS2U(i)    ((lua_Unsigned)(i))
0153 #endif
0154 
0155 /*
0156 ** cast a lua_Unsigned to a signed lua_Integer; this cast is
0157 ** not strict ISO C, but two-complement architectures should
0158 ** work fine.
0159 */
0160 #if !defined(l_castU2S)
0161 #define l_castU2S(i)    ((lua_Integer)(i))
0162 #endif
0163 
0164 
0165 /*
0166 ** non-return type
0167 */
0168 #if !defined(l_noret)
0169 
0170 #if defined(__GNUC__)
0171 #define l_noret     void __attribute__((noreturn))
0172 #elif defined(_MSC_VER) && _MSC_VER >= 1200
0173 #define l_noret     void __declspec(noreturn)
0174 #else
0175 #define l_noret     void
0176 #endif
0177 
0178 #endif
0179 
0180 
0181 /*
0182 ** Inline functions
0183 */
0184 #if !defined(LUA_USE_C89)
0185 #define l_inline    inline
0186 #elif defined(__GNUC__)
0187 #define l_inline    __inline__
0188 #else
0189 #define l_inline    /* empty */
0190 #endif
0191 
0192 #define l_sinline   static l_inline
0193 
0194 
0195 /*
0196 ** type for virtual-machine instructions;
0197 ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
0198 */
0199 #if LUAI_IS32INT
0200 typedef unsigned int l_uint32;
0201 #else
0202 typedef unsigned long l_uint32;
0203 #endif
0204 
0205 typedef l_uint32 Instruction;
0206 
0207 
0208 
0209 /*
0210 ** Maximum length for short strings, that is, strings that are
0211 ** internalized. (Cannot be smaller than reserved words or tags for
0212 ** metamethods, as these strings must be internalized;
0213 ** #("function") = 8, #("__newindex") = 10.)
0214 */
0215 #if !defined(LUAI_MAXSHORTLEN)
0216 #define LUAI_MAXSHORTLEN    40
0217 #endif
0218 
0219 
0220 /*
0221 ** Initial size for the string table (must be power of 2).
0222 ** The Lua core alone registers ~50 strings (reserved words +
0223 ** metaevent keys + a few others). Libraries would typically add
0224 ** a few dozens more.
0225 */
0226 #if !defined(MINSTRTABSIZE)
0227 #define MINSTRTABSIZE   128
0228 #endif
0229 
0230 
0231 /*
0232 ** Size of cache for strings in the API. 'N' is the number of
0233 ** sets (better be a prime) and "M" is the size of each set (M == 1
0234 ** makes a direct cache.)
0235 */
0236 #if !defined(STRCACHE_N)
0237 #define STRCACHE_N      53
0238 #define STRCACHE_M      2
0239 #endif
0240 
0241 
0242 /* minimum size for string buffer */
0243 #if !defined(LUA_MINBUFFER)
0244 #define LUA_MINBUFFER   32
0245 #endif
0246 
0247 
0248 /*
0249 ** Maximum depth for nested C calls, syntactical nested non-terminals,
0250 ** and other features implemented through recursion in C. (Value must
0251 ** fit in a 16-bit unsigned integer. It must also be compatible with
0252 ** the size of the C stack.)
0253 */
0254 #if !defined(LUAI_MAXCCALLS)
0255 #define LUAI_MAXCCALLS      200
0256 #endif
0257 
0258 
0259 /*
0260 ** macros that are executed whenever program enters the Lua core
0261 ** ('lua_lock') and leaves the core ('lua_unlock')
0262 */
0263 #if !defined(lua_lock)
0264 #define lua_lock(L) ((void) 0)
0265 #define lua_unlock(L)   ((void) 0)
0266 #endif
0267 
0268 /*
0269 ** macro executed during Lua functions at points where the
0270 ** function can yield.
0271 */
0272 #if !defined(luai_threadyield)
0273 #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
0274 #endif
0275 
0276 
0277 /*
0278 ** these macros allow user-specific actions when a thread is
0279 ** created/deleted/resumed/yielded.
0280 */
0281 #if !defined(luai_userstateopen)
0282 #define luai_userstateopen(L)       ((void)L)
0283 #endif
0284 
0285 #if !defined(luai_userstateclose)
0286 #define luai_userstateclose(L)      ((void)L)
0287 #endif
0288 
0289 #if !defined(luai_userstatethread)
0290 #define luai_userstatethread(L,L1)  ((void)L)
0291 #endif
0292 
0293 #if !defined(luai_userstatefree)
0294 #define luai_userstatefree(L,L1)    ((void)L)
0295 #endif
0296 
0297 #if !defined(luai_userstateresume)
0298 #define luai_userstateresume(L,n)   ((void)L)
0299 #endif
0300 
0301 #if !defined(luai_userstateyield)
0302 #define luai_userstateyield(L,n)    ((void)L)
0303 #endif
0304 
0305 
0306 
0307 /*
0308 ** The luai_num* macros define the primitive operations over numbers.
0309 */
0310 
0311 /* floor division (defined as 'floor(a/b)') */
0312 #if !defined(luai_numidiv)
0313 #define luai_numidiv(L,a,b)     ((void)L, l_floor(luai_numdiv(L,a,b)))
0314 #endif
0315 
0316 /* float division */
0317 #if !defined(luai_numdiv)
0318 #define luai_numdiv(L,a,b)      ((a)/(b))
0319 #endif
0320 
0321 /*
0322 ** modulo: defined as 'a - floor(a/b)*b'; the direct computation
0323 ** using this definition has several problems with rounding errors,
0324 ** so it is better to use 'fmod'. 'fmod' gives the result of
0325 ** 'a - trunc(a/b)*b', and therefore must be corrected when
0326 ** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a
0327 ** non-integer negative result: non-integer result is equivalent to
0328 ** a non-zero remainder 'm'; negative result is equivalent to 'a' and
0329 ** 'b' with different signs, or 'm' and 'b' with different signs
0330 ** (as the result 'm' of 'fmod' has the same sign of 'a').
0331 */
0332 #if !defined(luai_nummod)
0333 #define luai_nummod(L,a,b,m)  \
0334   { (void)L; (m) = l_mathop(fmod)(a,b); \
0335     if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }
0336 #endif
0337 
0338 /* exponentiation */
0339 #if !defined(luai_numpow)
0340 #define luai_numpow(L,a,b)  \
0341   ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b))
0342 #endif
0343 
0344 /* the others are quite standard operations */
0345 #if !defined(luai_numadd)
0346 #define luai_numadd(L,a,b)      ((a)+(b))
0347 #define luai_numsub(L,a,b)      ((a)-(b))
0348 #define luai_nummul(L,a,b)      ((a)*(b))
0349 #define luai_numunm(L,a)        (-(a))
0350 #define luai_numeq(a,b)         ((a)==(b))
0351 #define luai_numlt(a,b)         ((a)<(b))
0352 #define luai_numle(a,b)         ((a)<=(b))
0353 #define luai_numgt(a,b)         ((a)>(b))
0354 #define luai_numge(a,b)         ((a)>=(b))
0355 #define luai_numisnan(a)        (!luai_numeq((a), (a)))
0356 #endif
0357 
0358 
0359 
0360 
0361 
0362 /*
0363 ** macro to control inclusion of some hard tests on stack reallocation
0364 */
0365 #if !defined(HARDSTACKTESTS)
0366 #define condmovestack(L,pre,pos)    ((void)0)
0367 #else
0368 /* realloc stack keeping its size */
0369 #define condmovestack(L,pre,pos)  \
0370   { int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; }
0371 #endif
0372 
0373 #if !defined(HARDMEMTESTS)
0374 #define condchangemem(L,pre,pos)    ((void)0)
0375 #else
0376 #define condchangemem(L,pre,pos)  \
0377     { if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } }
0378 #endif
0379 
0380 #endif