1 /* $NetBSD: llimits.h,v 1.5 2015/10/08 13:21:00 mbalmer Exp $ */ 2 3 /* 4 ** Id: llimits.h,v 1.135 2015/06/09 14:21:00 roberto Exp 5 ** Limits, basic types, and some other 'installation-dependent' definitions 6 ** See Copyright Notice in lua.h 7 */ 8 9 #ifndef llimits_h 10 #define llimits_h 11 12 13 #ifndef _KERNEL 14 #include <limits.h> 15 #include <stddef.h> 16 #endif 17 18 19 #include "lua.h" 20 21 /* 22 ** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count 23 ** the total memory used by Lua (in bytes). Usually, 'size_t' and 24 ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. 25 */ 26 #if defined(LUAI_MEM) /* { external definitions? */ 27 typedef LUAI_UMEM lu_mem; 28 typedef LUAI_MEM l_mem; 29 #elif LUAI_BITSINT >= 32 /* }{ */ 30 typedef size_t lu_mem; 31 typedef ptrdiff_t l_mem; 32 #else /* 16-bit ints */ /* }{ */ 33 typedef unsigned long lu_mem; 34 typedef long l_mem; 35 #endif /* } */ 36 37 38 /* chars used as small naturals (so that 'char' is reserved for characters) */ 39 typedef unsigned char lu_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 ** conversion of pointer to unsigned integer: 60 ** this is for hashing only; there is no problem if the integer 61 ** cannot hold the whole pointer value 62 */ 63 #define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX)) 64 65 66 67 /* type to ensure maximum alignment */ 68 #if defined(LUAI_USER_ALIGNMENT_T) 69 typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 70 #else 71 #ifndef _KERNEL 72 typedef union { double u; void *s; lua_Integer i; long l; } L_Umaxalign; 73 #else /* _KERNEL */ 74 typedef union { void *s; lua_Integer i; long l; } L_Umaxalign; 75 #endif 76 #endif 77 78 79 80 /* types of 'usual argument conversions' for lua_Number and lua_Integer */ 81 #ifndef _KERNEL 82 typedef LUAI_UACNUMBER l_uacNumber; 83 #endif 84 typedef LUAI_UACINT l_uacInt; 85 86 87 /* internal assertions for in-house debugging */ 88 #if defined(lua_assert) 89 #define check_exp(c,e) (lua_assert(c), (e)) 90 /* to avoid problems with conditions too long */ 91 #define lua_longassert(c) { if (!(c)) lua_assert(0); } 92 #else 93 #define lua_assert(c) ((void)0) 94 #define check_exp(c,e) (e) 95 #define lua_longassert(c) ((void)0) 96 #endif 97 98 /* 99 ** assertion for checking API calls 100 */ 101 #if !defined(luai_apicheck) 102 #define luai_apicheck(l,e) lua_assert(e) 103 #endif 104 105 #define api_check(l,e,msg) luai_apicheck(l,(e) && msg) 106 107 108 /* macro to avoid warnings about unused variables */ 109 #if !defined(UNUSED) 110 #define UNUSED(x) ((void)(x)) 111 #endif 112 113 114 /* type casts (a macro highlights casts in the code) */ 115 #define cast(t, exp) ((t)(exp)) 116 117 #define cast_void(i) cast(void, (i)) 118 #define cast_byte(i) cast(lu_byte, (i)) 119 #define cast_num(i) cast(lua_Number, (i)) 120 #define cast_int(i) cast(int, (i)) 121 #define cast_uchar(i) cast(unsigned char, (i)) 122 123 124 /* cast a signed lua_Integer to lua_Unsigned */ 125 #if !defined(l_castS2U) 126 #define l_castS2U(i) ((lua_Unsigned)(i)) 127 #endif 128 129 /* 130 ** cast a lua_Unsigned to a signed lua_Integer; this cast is 131 ** not strict ISO C, but two-complement architectures should 132 ** work fine. 133 */ 134 #if !defined(l_castU2S) 135 #define l_castU2S(i) ((lua_Integer)(i)) 136 #endif 137 138 139 /* 140 ** non-return type 141 */ 142 #if defined(__GNUC__) 143 #define l_noret void __attribute__((noreturn)) 144 #elif defined(_MSC_VER) && _MSC_VER >= 1200 145 #define l_noret void __declspec(noreturn) 146 #else 147 #define l_noret void 148 #endif 149 150 151 152 /* 153 ** maximum depth for nested C calls and syntactical nested non-terminals 154 ** in a program. (Value must fit in an unsigned short int.) 155 */ 156 #if !defined(LUAI_MAXCCALLS) 157 #define LUAI_MAXCCALLS 200 158 #endif 159 160 161 162 /* 163 ** type for virtual-machine instructions; 164 ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 165 */ 166 #if LUAI_BITSINT >= 32 167 typedef unsigned int Instruction; 168 #else 169 typedef unsigned long Instruction; 170 #endif 171 172 173 174 /* 175 ** Maximum length for short strings, that is, strings that are 176 ** internalized. (Cannot be smaller than reserved words or tags for 177 ** metamethods, as these strings must be internalized; 178 ** #("function") = 8, #("__newindex") = 10.) 179 */ 180 #if !defined(LUAI_MAXSHORTLEN) 181 #define LUAI_MAXSHORTLEN 40 182 #endif 183 184 185 /* 186 ** Initial size for the string table (must be power of 2). 187 ** The Lua core alone registers ~50 strings (reserved words + 188 ** metaevent keys + a few others). Libraries would typically add 189 ** a few dozens more. 190 */ 191 #if !defined(MINSTRTABSIZE) 192 #define MINSTRTABSIZE 128 193 #endif 194 195 196 /* 197 ** Size of cache for strings in the API (better be a prime) 198 */ 199 #if !defined(STRCACHE_SIZE) 200 #define STRCACHE_SIZE 127 201 #endif 202 203 204 /* minimum size for string buffer */ 205 #if !defined(LUA_MINBUFFER) 206 #define LUA_MINBUFFER 32 207 #endif 208 209 210 /* 211 ** macros that are executed whenether program enters the Lua core 212 ** ('lua_lock') and leaves the core ('lua_unlock') 213 */ 214 #if !defined(lua_lock) 215 #define lua_lock(L) ((void) 0) 216 #define lua_unlock(L) ((void) 0) 217 #endif 218 219 /* 220 ** macro executed during Lua functions at points where the 221 ** function can yield. 222 */ 223 #if !defined(luai_threadyield) 224 #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 225 #endif 226 227 228 /* 229 ** these macros allow user-specific actions on threads when you defined 230 ** LUAI_EXTRASPACE and need to do something extra when a thread is 231 ** created/deleted/resumed/yielded. 232 */ 233 #if !defined(luai_userstateopen) 234 #define luai_userstateopen(L) ((void)L) 235 #endif 236 237 #if !defined(luai_userstateclose) 238 #define luai_userstateclose(L) ((void)L) 239 #endif 240 241 #if !defined(luai_userstatethread) 242 #define luai_userstatethread(L,L1) ((void)L) 243 #endif 244 245 #if !defined(luai_userstatefree) 246 #define luai_userstatefree(L,L1) ((void)L) 247 #endif 248 249 #if !defined(luai_userstateresume) 250 #define luai_userstateresume(L,n) ((void)L) 251 #endif 252 253 #if !defined(luai_userstateyield) 254 #define luai_userstateyield(L,n) ((void)L) 255 #endif 256 257 258 259 /* 260 ** The luai_num* macros define the primitive operations over numbers. 261 */ 262 263 /* floor division (defined as 'floor(a/b)') */ 264 #if !defined(luai_numidiv) 265 #define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b))) 266 #endif 267 268 /* float division */ 269 #if !defined(luai_numdiv) 270 #define luai_numdiv(L,a,b) ((a)/(b)) 271 #endif 272 273 /* 274 ** modulo: defined as 'a - floor(a/b)*b'; this definition gives NaN when 275 ** 'b' is huge, but the result should be 'a'. 'fmod' gives the result of 276 ** 'a - trunc(a/b)*b', and therefore must be corrected when 'trunc(a/b) 277 ** ~= floor(a/b)'. That happens when the division has a non-integer 278 ** negative result, which is equivalent to the test below. 279 */ 280 #if !defined(luai_nummod) 281 #define luai_nummod(L,a,b,m) \ 282 { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); } 283 #endif 284 285 /* exponentiation */ 286 #if !defined(luai_numpow) 287 #define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b)) 288 #endif 289 290 /* the others are quite standard operations */ 291 #if !defined(luai_numadd) 292 #define luai_numadd(L,a,b) ((a)+(b)) 293 #define luai_numsub(L,a,b) ((a)-(b)) 294 #define luai_nummul(L,a,b) ((a)*(b)) 295 #define luai_numunm(L,a) (-(a)) 296 #define luai_numeq(a,b) ((a)==(b)) 297 #define luai_numlt(a,b) ((a)<(b)) 298 #define luai_numle(a,b) ((a)<=(b)) 299 #define luai_numisnan(a) (!luai_numeq((a), (a))) 300 #endif 301 302 303 304 305 306 /* 307 ** macro to control inclusion of some hard tests on stack reallocation 308 */ 309 #if !defined(HARDSTACKTESTS) 310 #define condmovestack(L) ((void)0) 311 #else 312 /* realloc stack keeping its size */ 313 #define condmovestack(L) luaD_reallocstack((L), (L)->stacksize) 314 #endif 315 316 #if !defined(HARDMEMTESTS) 317 #define condchangemem(L) condmovestack(L) 318 #else 319 #define condchangemem(L) \ 320 ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1))) 321 #endif 322 323 #endif 324