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