/*1** $Id: llimits.h $2** Limits, basic types, and some other 'installation-dependent' definitions3** See Copyright Notice in lua.h4*/56#ifndef llimits_h7#define llimits_h8910#include <limits.h>11#include <stddef.h>121314#include "lua.h"151617/*18** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count19** the total memory used by Lua (in bytes). Usually, 'size_t' and20** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.21*/22#if defined(LUAI_MEM) /* { external definitions? */23typedef LUAI_UMEM lu_mem;24typedef LUAI_MEM l_mem;25#elif LUAI_IS32INT /* }{ */26typedef size_t lu_mem;27typedef ptrdiff_t l_mem;28#else /* 16-bit ints */ /* }{ */29typedef unsigned long lu_mem;30typedef long l_mem;31#endif /* } */323334/* chars used as small naturals (so that 'char' is reserved for characters) */35typedef unsigned char lu_byte;36typedef signed char ls_byte;373839/* maximum value for size_t */40#define MAX_SIZET ((size_t)(~(size_t)0))4142/* maximum size visible for Lua (must be representable in a lua_Integer) */43#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \44: (size_t)(LUA_MAXINTEGER))454647#define MAX_LUMEM ((lu_mem)(~(lu_mem)0))4849#define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1))505152#define MAX_INT INT_MAX /* maximum value of an int */535455/*56** floor of the log2 of the maximum signed value for integral type 't'.57** (That is, maximum 'n' such that '2^n' fits in the given signed type.)58*/59#define log2maxs(t) (sizeof(t) * 8 - 2)606162/*63** test whether an unsigned value is a power of 2 (or zero)64*/65#define ispow2(x) (((x) & ((x) - 1)) == 0)666768/* number of chars of a literal string without the ending \0 */69#define LL(x) (sizeof(x)/sizeof(char) - 1)707172/*73** conversion of pointer to unsigned integer: this is for hashing only;74** there is no problem if the integer cannot hold the whole pointer75** value. (In strict ISO C this may cause undefined behavior, but no76** actual machine seems to bother.)77*/78#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \79__STDC_VERSION__ >= 199901L80#include <stdint.h>81#if defined(UINTPTR_MAX) /* even in C99 this type is optional */82#define L_P2I uintptr_t83#else /* no 'intptr'? */84#define L_P2I uintmax_t /* use the largest available integer */85#endif86#else /* C89 option */87#define L_P2I size_t88#endif8990#define point2uint(p) ((unsigned int)((L_P2I)(p) & UINT_MAX))91929394/* types of 'usual argument conversions' for lua_Number and lua_Integer */95typedef LUAI_UACNUMBER l_uacNumber;96typedef LUAI_UACINT l_uacInt;979899/*100** Internal assertions for in-house debugging101*/102#if defined LUAI_ASSERT103#undef NDEBUG104#include <assert.h>105#define lua_assert(c) assert(c)106#endif107108#if defined(lua_assert)109#define check_exp(c,e) (lua_assert(c), (e))110/* to avoid problems with conditions too long */111#define lua_longassert(c) ((c) ? (void)0 : lua_assert(0))112#else113#define lua_assert(c) ((void)0)114#define check_exp(c,e) (e)115#define lua_longassert(c) ((void)0)116#endif117118/*119** assertion for checking API calls120*/121#if !defined(luai_apicheck)122#define luai_apicheck(l,e) ((void)l, lua_assert(e))123#endif124125#define api_check(l,e,msg) luai_apicheck(l,(e) && msg)126127128/* macro to avoid warnings about unused variables */129#if !defined(UNUSED)130#define UNUSED(x) ((void)(x))131#endif132133134/* type casts (a macro highlights casts in the code) */135#define cast(t, exp) ((t)(exp))136137#define cast_void(i) cast(void, (i))138#define cast_voidp(i) cast(void *, (i))139#define cast_num(i) cast(lua_Number, (i))140#define cast_int(i) cast(int, (i))141#define cast_uint(i) cast(unsigned int, (i))142#define cast_byte(i) cast(lu_byte, (i))143#define cast_uchar(i) cast(unsigned char, (i))144#define cast_char(i) cast(char, (i))145#define cast_charp(i) cast(char *, (i))146#define cast_sizet(i) cast(size_t, (i))147148149/* cast a signed lua_Integer to lua_Unsigned */150#if !defined(l_castS2U)151#define l_castS2U(i) ((lua_Unsigned)(i))152#endif153154/*155** cast a lua_Unsigned to a signed lua_Integer; this cast is156** not strict ISO C, but two-complement architectures should157** work fine.158*/159#if !defined(l_castU2S)160#define l_castU2S(i) ((lua_Integer)(i))161#endif162163164/*165** non-return type166*/167#if !defined(l_noret)168169#if defined(__GNUC__)170#define l_noret void __attribute__((noreturn))171#elif defined(_MSC_VER) && _MSC_VER >= 1200172#define l_noret void __declspec(noreturn)173#else174#define l_noret void175#endif176177#endif178179180/*181** Inline functions182*/183#if !defined(LUA_USE_C89)184#define l_inline inline185#elif defined(__GNUC__)186#define l_inline __inline__187#else188#define l_inline /* empty */189#endif190191#define l_sinline static l_inline192193194/*195** type for virtual-machine instructions;196** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)197*/198#if LUAI_IS32INT199typedef unsigned int l_uint32;200#else201typedef unsigned long l_uint32;202#endif203204typedef l_uint32 Instruction;205206207208/*209** Maximum length for short strings, that is, strings that are210** internalized. (Cannot be smaller than reserved words or tags for211** metamethods, as these strings must be internalized;212** #("function") = 8, #("__newindex") = 10.)213*/214#if !defined(LUAI_MAXSHORTLEN)215#define LUAI_MAXSHORTLEN 40216#endif217218219/*220** Initial size for the string table (must be power of 2).221** The Lua core alone registers ~50 strings (reserved words +222** metaevent keys + a few others). Libraries would typically add223** a few dozens more.224*/225#if !defined(MINSTRTABSIZE)226#define MINSTRTABSIZE 128227#endif228229230/*231** Size of cache for strings in the API. 'N' is the number of232** sets (better be a prime) and "M" is the size of each set (M == 1233** makes a direct cache.)234*/235#if !defined(STRCACHE_N)236#define STRCACHE_N 53237#define STRCACHE_M 2238#endif239240241/* minimum size for string buffer */242#if !defined(LUA_MINBUFFER)243#define LUA_MINBUFFER 32244#endif245246247/*248** Maximum depth for nested C calls, syntactical nested non-terminals,249** and other features implemented through recursion in C. (Value must250** fit in a 16-bit unsigned integer. It must also be compatible with251** the size of the C stack.)252*/253#if !defined(LUAI_MAXCCALLS)254#define LUAI_MAXCCALLS 200255#endif256257258/*259** macros that are executed whenever program enters the Lua core260** ('lua_lock') and leaves the core ('lua_unlock')261*/262#if !defined(lua_lock)263#define lua_lock(L) ((void) 0)264#define lua_unlock(L) ((void) 0)265#endif266267/*268** macro executed during Lua functions at points where the269** function can yield.270*/271#if !defined(luai_threadyield)272#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}273#endif274275276/*277** these macros allow user-specific actions when a thread is278** created/deleted/resumed/yielded.279*/280#if !defined(luai_userstateopen)281#define luai_userstateopen(L) ((void)L)282#endif283284#if !defined(luai_userstateclose)285#define luai_userstateclose(L) ((void)L)286#endif287288#if !defined(luai_userstatethread)289#define luai_userstatethread(L,L1) ((void)L)290#endif291292#if !defined(luai_userstatefree)293#define luai_userstatefree(L,L1) ((void)L)294#endif295296#if !defined(luai_userstateresume)297#define luai_userstateresume(L,n) ((void)L)298#endif299300#if !defined(luai_userstateyield)301#define luai_userstateyield(L,n) ((void)L)302#endif303304305306/*307** The luai_num* macros define the primitive operations over numbers.308*/309310/* floor division (defined as 'floor(a/b)') */311#if !defined(luai_numidiv)312#define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b)))313#endif314315/* float division */316#if !defined(luai_numdiv)317#define luai_numdiv(L,a,b) ((a)/(b))318#endif319320/*321** modulo: defined as 'a - floor(a/b)*b'; the direct computation322** using this definition has several problems with rounding errors,323** so it is better to use 'fmod'. 'fmod' gives the result of324** 'a - trunc(a/b)*b', and therefore must be corrected when325** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a326** non-integer negative result: non-integer result is equivalent to327** a non-zero remainder 'm'; negative result is equivalent to 'a' and328** 'b' with different signs, or 'm' and 'b' with different signs329** (as the result 'm' of 'fmod' has the same sign of 'a').330*/331#if !defined(luai_nummod)332#define luai_nummod(L,a,b,m) \333{ (void)L; (m) = l_mathop(fmod)(a,b); \334if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }335#endif336337/* exponentiation */338#if !defined(luai_numpow)339#define luai_numpow(L,a,b) \340((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b))341#endif342343/* the others are quite standard operations */344#if !defined(luai_numadd)345#define luai_numadd(L,a,b) ((a)+(b))346#define luai_numsub(L,a,b) ((a)-(b))347#define luai_nummul(L,a,b) ((a)*(b))348#define luai_numunm(L,a) (-(a))349#define luai_numeq(a,b) ((a)==(b))350#define luai_numlt(a,b) ((a)<(b))351#define luai_numle(a,b) ((a)<=(b))352#define luai_numgt(a,b) ((a)>(b))353#define luai_numge(a,b) ((a)>=(b))354#define luai_numisnan(a) (!luai_numeq((a), (a)))355#endif356357358359360361/*362** macro to control inclusion of some hard tests on stack reallocation363*/364#if !defined(HARDSTACKTESTS)365#define condmovestack(L,pre,pos) ((void)0)366#else367/* realloc stack keeping its size */368#define condmovestack(L,pre,pos) \369{ int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; }370#endif371372#if !defined(HARDMEMTESTS)373#define condchangemem(L,pre,pos) ((void)0)374#else375#define condchangemem(L,pre,pos) \376{ if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } }377#endif378379#endif380381382