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? */ 24 typedef LUAI_UMEM lu_mem; 25 typedef LUAI_MEM l_mem; 26 #elif LUAI_IS32INT /* }{ */ 27 typedef size_t lu_mem; 28 typedef ptrdiff_t l_mem; 29 #else /* 16-bit ints */ /* }{ */ 30 typedef unsigned long lu_mem; 31 typedef long l_mem; 32 #endif /* } */ 33 34 35 /* chars used as small naturals (so that 'char' is reserved for characters) */ 36 typedef unsigned char lu_byte; 37 typedef 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 */ 83 typedef LUAI_UACNUMBER l_uacNumber; 84 typedef 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 189 typedef unsigned int l_uint32; 190 #else 191 typedef unsigned long l_uint32; 192 #endif 193 194 typedef 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 ** macros that are executed whenever program enters the Lua core 239 ** ('lua_lock') and leaves the core ('lua_unlock') 240 */ 241 #if !defined(lua_lock) 242 #define lua_lock(L) ((void) 0) 243 #define lua_unlock(L) ((void) 0) 244 #endif 245 246 /* 247 ** macro executed during Lua functions at points where the 248 ** function can yield. 249 */ 250 #if !defined(luai_threadyield) 251 #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 252 #endif 253 254 255 /* 256 ** these macros allow user-specific actions when a thread is 257 ** created/deleted/resumed/yielded. 258 */ 259 #if !defined(luai_userstateopen) 260 #define luai_userstateopen(L) ((void)L) 261 #endif 262 263 #if !defined(luai_userstateclose) 264 #define luai_userstateclose(L) ((void)L) 265 #endif 266 267 #if !defined(luai_userstatethread) 268 #define luai_userstatethread(L,L1) ((void)L) 269 #endif 270 271 #if !defined(luai_userstatefree) 272 #define luai_userstatefree(L,L1) ((void)L) 273 #endif 274 275 #if !defined(luai_userstateresume) 276 #define luai_userstateresume(L,n) ((void)L) 277 #endif 278 279 #if !defined(luai_userstateyield) 280 #define luai_userstateyield(L,n) ((void)L) 281 #endif 282 283 284 285 /* 286 ** The luai_num* macros define the primitive operations over numbers. 287 */ 288 289 /* floor division (defined as 'floor(a/b)') */ 290 #if !defined(luai_numidiv) 291 #define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b))) 292 #endif 293 294 /* float division */ 295 #if !defined(luai_numdiv) 296 #define luai_numdiv(L,a,b) ((a)/(b)) 297 #endif 298 299 /* 300 ** modulo: defined as 'a - floor(a/b)*b'; the direct computation 301 ** using this definition has several problems with rounding errors, 302 ** so it is better to use 'fmod'. 'fmod' gives the result of 303 ** 'a - trunc(a/b)*b', and therefore must be corrected when 304 ** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a 305 ** non-integer negative result: non-integer result is equivalent to 306 ** a non-zero remainder 'm'; negative result is equivalent to 'a' and 307 ** 'b' with different signs, or 'm' and 'b' with different signs 308 ** (as the result 'm' of 'fmod' has the same sign of 'a'). 309 */ 310 #if !defined(luai_nummod) 311 #define luai_nummod(L,a,b,m) \ 312 { (void)L; (m) = l_mathop(fmod)(a,b); \ 313 if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); } 314 #endif 315 316 /* exponentiation */ 317 #if !defined(luai_numpow) 318 #define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b)) 319 #endif 320 321 /* the others are quite standard operations */ 322 #if !defined(luai_numadd) 323 #define luai_numadd(L,a,b) ((a)+(b)) 324 #define luai_numsub(L,a,b) ((a)-(b)) 325 #define luai_nummul(L,a,b) ((a)*(b)) 326 #define luai_numunm(L,a) (-(a)) 327 #define luai_numeq(a,b) ((a)==(b)) 328 #define luai_numlt(a,b) ((a)<(b)) 329 #define luai_numle(a,b) ((a)<=(b)) 330 #define luai_numgt(a,b) ((a)>(b)) 331 #define luai_numge(a,b) ((a)>=(b)) 332 #define luai_numisnan(a) (!luai_numeq((a), (a))) 333 #endif 334 335 336 337 338 339 /* 340 ** macro to control inclusion of some hard tests on stack reallocation 341 */ 342 #if !defined(HARDSTACKTESTS) 343 #define condmovestack(L,pre,pos) ((void)0) 344 #else 345 /* realloc stack keeping its size */ 346 #define condmovestack(L,pre,pos) \ 347 { int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_, 0); pos; } 348 #endif 349 350 #if !defined(HARDMEMTESTS) 351 #define condchangemem(L,pre,pos) ((void)0) 352 #else 353 #define condchangemem(L,pre,pos) \ 354 { if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } } 355 #endif 356 357 #endif 358