1 /* 2 ** $Id: llimits.h,v 1.103.1.1 2013/04/12 18:48:47 roberto Exp $ 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 <sys/zfs_context.h> 12 13 #include "lua.h" 14 15 16 typedef unsigned LUA_INT32 lu_int32; 17 18 typedef LUAI_UMEM lu_mem; 19 20 typedef LUAI_MEM l_mem; 21 22 23 24 /* chars used as small naturals (so that `char' is reserved for characters) */ 25 typedef unsigned char lu_byte; 26 27 28 #define MAX_SIZET ((size_t)(~(size_t)0)-2) 29 30 #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) 31 32 #define MAX_LMEM ((l_mem) ((MAX_LUMEM >> 1) - 2)) 33 34 35 #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 36 37 /* 38 ** conversion of pointer to integer 39 ** this is for hashing only; there is no problem if the integer 40 ** cannot hold the whole pointer value 41 */ 42 #define IntPoint(p) ((unsigned int)(lu_mem)(p)) 43 44 45 46 /* type to ensure maximum alignment */ 47 #if !defined(LUAI_USER_ALIGNMENT_T) 48 #define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; } 49 #endif 50 51 typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 52 53 54 /* result of a `usual argument conversion' over lua_Number */ 55 typedef LUAI_UACNUMBER l_uacNumber; 56 57 58 /* internal assertions for in-house debugging */ 59 #if defined(lua_assert) 60 #define check_exp(c,e) (lua_assert(c), (e)) 61 /* to avoid problems with conditions too long */ 62 #define lua_longassert(c) { if (!(c)) lua_assert(0); } 63 #else 64 #define lua_assert(c) ((void)0) 65 #define check_exp(c,e) (e) 66 #define lua_longassert(c) ((void)0) 67 #endif 68 69 /* 70 ** assertion for checking API calls 71 */ 72 #if !defined(luai_apicheck) 73 74 #if defined(LUA_USE_APICHECK) 75 #include <assert.h> 76 #define luai_apicheck(L,e) assert(e) 77 #else 78 #define luai_apicheck(L,e) lua_assert(e) 79 #endif 80 81 #endif 82 83 #define api_check(l,e,msg) luai_apicheck(l,(e) && msg) 84 85 86 #if !defined(UNUSED) 87 #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 88 #endif 89 90 91 #define cast(t, exp) ((t)(exp)) 92 93 #define cast_byte(i) cast(lu_byte, (i)) 94 #define cast_num(i) cast(lua_Number, (i)) 95 #define cast_int(i) cast(int, (i)) 96 #define cast_uchar(i) cast(unsigned char, (i)) 97 98 99 /* 100 ** non-return type 101 */ 102 #if defined(__GNUC__) 103 #define l_noret void __attribute__((noreturn)) 104 #elif defined(_MSC_VER) 105 #define l_noret void __declspec(noreturn) 106 #else 107 #define l_noret void 108 #endif 109 110 111 112 /* 113 ** maximum depth for nested C calls and syntactical nested non-terminals 114 ** in a program. (Value must fit in an unsigned short int.) 115 ** 116 ** Note: On amd64 platform, the limit has been measured to be 45. We set 117 ** the maximum lower to give a margin for changing the amount of stack 118 ** used by various functions involved in parsing and executing code. 119 */ 120 #if !defined(LUAI_MAXCCALLS) 121 #define LUAI_MAXCCALLS 20 122 #endif 123 124 /* 125 ** maximum number of upvalues in a closure (both C and Lua). (Value 126 ** must fit in an unsigned char.) 127 */ 128 #define MAXUPVAL UCHAR_MAX 129 130 131 /* 132 ** type for virtual-machine instructions 133 ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 134 */ 135 typedef lu_int32 Instruction; 136 137 138 139 /* maximum stack for a Lua function */ 140 #define MAXSTACK 250 141 142 143 144 /* minimum size for the string table (must be power of 2) */ 145 #if !defined(MINSTRTABSIZE) 146 #define MINSTRTABSIZE 32 147 #endif 148 149 150 /* minimum size for string buffer */ 151 #if !defined(LUA_MINBUFFER) 152 #define LUA_MINBUFFER 32 153 #endif 154 155 156 #if !defined(lua_lock) 157 #define lua_lock(L) ((void) 0) 158 #define lua_unlock(L) ((void) 0) 159 #endif 160 161 #if !defined(luai_threadyield) 162 #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 163 #endif 164 165 166 /* 167 ** these macros allow user-specific actions on threads when you defined 168 ** LUAI_EXTRASPACE and need to do something extra when a thread is 169 ** created/deleted/resumed/yielded. 170 */ 171 #if !defined(luai_userstateopen) 172 #define luai_userstateopen(L) ((void)L) 173 #endif 174 175 #if !defined(luai_userstateclose) 176 #define luai_userstateclose(L) ((void)L) 177 #endif 178 179 #if !defined(luai_userstatethread) 180 #define luai_userstatethread(L,L1) ((void)L) 181 #endif 182 183 #if !defined(luai_userstatefree) 184 #define luai_userstatefree(L,L1) ((void)L) 185 #endif 186 187 #if !defined(luai_userstateresume) 188 #define luai_userstateresume(L,n) ((void)L) 189 #endif 190 191 #if !defined(luai_userstateyield) 192 #define luai_userstateyield(L,n) ((void)L) 193 #endif 194 195 /* 196 ** lua_number2int is a macro to convert lua_Number to int. 197 ** lua_number2integer is a macro to convert lua_Number to lua_Integer. 198 ** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned. 199 ** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number. 200 ** luai_hashnum is a macro to hash a lua_Number value into an integer. 201 ** The hash must be deterministic and give reasonable values for 202 ** both small and large values (outside the range of integers). 203 */ 204 205 #if defined(MS_ASMTRICK) || defined(LUA_MSASMTRICK) /* { */ 206 /* trick with Microsoft assembler for X86 */ 207 208 #define lua_number2int(i,n) __asm {__asm fld n __asm fistp i} 209 #define lua_number2integer(i,n) lua_number2int(i, n) 210 #define lua_number2unsigned(i,n) \ 211 {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;} 212 213 214 #elif defined(LUA_IEEE754TRICK) /* }{ */ 215 /* the next trick should work on any machine using IEEE754 with 216 a 32-bit int type */ 217 218 union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; 219 220 #if !defined(LUA_IEEEENDIAN) /* { */ 221 #define LUAI_EXTRAIEEE \ 222 static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)}; 223 #define LUA_IEEEENDIANLOC (ieeeendian.l_p[1] == 33) 224 #else 225 #define LUA_IEEEENDIANLOC LUA_IEEEENDIAN 226 #define LUAI_EXTRAIEEE /* empty */ 227 #endif /* } */ 228 229 #define lua_number2int32(i,n,t) \ 230 { LUAI_EXTRAIEEE \ 231 volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \ 232 (i) = (t)u.l_p[LUA_IEEEENDIANLOC]; } 233 234 #define luai_hashnum(i,n) \ 235 { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \ 236 (i) = u.l_p[0]; (i) += u.l_p[1]; } /* add double bits for his hash */ 237 238 #define lua_number2int(i,n) lua_number2int32(i, n, int) 239 #define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned) 240 241 /* the trick can be expanded to lua_Integer when it is a 32-bit value */ 242 #if defined(LUA_IEEELL) 243 #define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer) 244 #endif 245 246 #endif /* } */ 247 248 249 /* the following definitions always work, but may be slow */ 250 251 #if !defined(lua_number2int) 252 #define lua_number2int(i,n) ((i)=(int)(n)) 253 #endif 254 255 #if !defined(lua_number2integer) 256 #define lua_number2integer(i,n) ((i)=(lua_Integer)(n)) 257 #endif 258 259 #if !defined(lua_number2unsigned) /* { */ 260 /* the following definition assures proper modulo behavior */ 261 #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT) 262 #include <math.h> 263 #define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1) 264 #define lua_number2unsigned(i,n) \ 265 ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED)) 266 #else 267 #define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n)) 268 #endif 269 #endif /* } */ 270 271 272 #if !defined(lua_unsigned2number) 273 /* on several machines, coercion from unsigned to double is slow, 274 so it may be worth to avoid */ 275 #define lua_unsigned2number(u) \ 276 (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u)) 277 #endif 278 279 280 281 #if defined(ltable_c) && !defined(luai_hashnum) 282 283 extern int lcompat_hashnum(int64_t); 284 285 #define luai_hashnum(i,n) (i = lcompat_hashnum(n)) 286 287 #endif 288 289 290 291 /* 292 ** macro to control inclusion of some hard tests on stack reallocation 293 */ 294 #if !defined(HARDSTACKTESTS) 295 #define condmovestack(L) ((void)0) 296 #else 297 /* realloc stack keeping its size */ 298 #define condmovestack(L) luaD_reallocstack((L), (L)->stacksize) 299 #endif 300 301 #if !defined(HARDMEMTESTS) 302 #define condchangemem(L) condmovestack(L) 303 #else 304 #define condchangemem(L) \ 305 ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1))) 306 #endif 307 308 #endif 309