llimits.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. ** $Id: llimits.h,v 1.103 2013/02/20 14:08:56 roberto Exp $
  3. ** Limits, basic types, and some other `installation-dependent' definitions
  4. ** See Copyright Notice in lua.h
  5. */
  6. #ifndef llimits_h
  7. #define llimits_h
  8. #include <limits.h>
  9. #include <stddef.h>
  10. #include "lua.h"
  11. typedef unsigned LUA_INT32 lu_int32;
  12. typedef LUAI_UMEM lu_mem;
  13. typedef LUAI_MEM l_mem;
  14. /* chars used as small naturals (so that `char' is reserved for characters) */
  15. typedef unsigned char lu_byte;
  16. #define MAX_SIZET ((size_t)(~(size_t)0)-2)
  17. #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2)
  18. #define MAX_LMEM ((l_mem) ((MAX_LUMEM >> 1) - 2))
  19. #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
  20. /*
  21. ** conversion of pointer to integer
  22. ** this is for hashing only; there is no problem if the integer
  23. ** cannot hold the whole pointer value
  24. */
  25. #define IntPoint(p) ((unsigned int)(lu_mem)(p))
  26. /* type to ensure maximum alignment */
  27. #if !defined(LUAI_USER_ALIGNMENT_T)
  28. #define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; }
  29. #endif
  30. typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
  31. /* result of a `usual argument conversion' over lua_Number */
  32. typedef LUAI_UACNUMBER l_uacNumber;
  33. /* internal assertions for in-house debugging */
  34. #if defined(lua_assert)
  35. #define check_exp(c,e) (lua_assert(c), (e))
  36. /* to avoid problems with conditions too long */
  37. #define lua_longassert(c) { if (!(c)) lua_assert(0); }
  38. #else
  39. #define lua_assert(c) ((void)0)
  40. #define check_exp(c,e) (e)
  41. #define lua_longassert(c) ((void)0)
  42. #endif
  43. /*
  44. ** assertion for checking API calls
  45. */
  46. #if !defined(luai_apicheck)
  47. #if defined(LUA_USE_APICHECK)
  48. #include <assert.h>
  49. #define luai_apicheck(L,e) assert(e)
  50. #else
  51. #define luai_apicheck(L,e) lua_assert(e)
  52. #endif
  53. #endif
  54. #define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
  55. #if !defined(UNUSED)
  56. #define UNUSED(x) ((void)(x)) /* to avoid warnings */
  57. #endif
  58. #define cast(t, exp) ((t)(exp))
  59. #define cast_byte(i) cast(lu_byte, (i))
  60. #define cast_num(i) cast(lua_Number, (i))
  61. #define cast_int(i) cast(int, (i))
  62. #define cast_uchar(i) cast(unsigned char, (i))
  63. /*
  64. ** non-return type
  65. */
  66. #if defined(__GNUC__)
  67. #define l_noret void __attribute__((noreturn))
  68. #elif defined(_MSC_VER)
  69. #define l_noret void __declspec(noreturn)
  70. #else
  71. #define l_noret void
  72. #endif
  73. /*
  74. ** maximum depth for nested C calls and syntactical nested non-terminals
  75. ** in a program. (Value must fit in an unsigned short int.)
  76. */
  77. #if !defined(LUAI_MAXCCALLS)
  78. #define LUAI_MAXCCALLS 200
  79. #endif
  80. /*
  81. ** maximum number of upvalues in a closure (both C and Lua). (Value
  82. ** must fit in an unsigned char.)
  83. */
  84. #define MAXUPVAL UCHAR_MAX
  85. /*
  86. ** type for virtual-machine instructions
  87. ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
  88. */
  89. typedef lu_int32 Instruction;
  90. /* maximum stack for a Lua function */
  91. #define MAXSTACK 250
  92. /* minimum size for the string table (must be power of 2) */
  93. #if !defined(MINSTRTABSIZE)
  94. #define MINSTRTABSIZE 32
  95. #endif
  96. /* minimum size for string buffer */
  97. #if !defined(LUA_MINBUFFER)
  98. #define LUA_MINBUFFER 32
  99. #endif
  100. #if !defined(lua_lock)
  101. #define lua_lock(L) ((void) 0)
  102. #define lua_unlock(L) ((void) 0)
  103. #endif
  104. #if !defined(luai_threadyield)
  105. #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
  106. #endif
  107. /*
  108. ** these macros allow user-specific actions on threads when you defined
  109. ** LUAI_EXTRASPACE and need to do something extra when a thread is
  110. ** created/deleted/resumed/yielded.
  111. */
  112. #if !defined(luai_userstateopen)
  113. #define luai_userstateopen(L) ((void)L)
  114. #endif
  115. #if !defined(luai_userstateclose)
  116. #define luai_userstateclose(L) ((void)L)
  117. #endif
  118. #if !defined(luai_userstatethread)
  119. #define luai_userstatethread(L,L1) ((void)L)
  120. #endif
  121. #if !defined(luai_userstatefree)
  122. #define luai_userstatefree(L,L1) ((void)L)
  123. #endif
  124. #if !defined(luai_userstateresume)
  125. #define luai_userstateresume(L,n) ((void)L)
  126. #endif
  127. #if !defined(luai_userstateyield)
  128. #define luai_userstateyield(L,n) ((void)L)
  129. #endif
  130. /*
  131. ** lua_number2int is a macro to convert lua_Number to int.
  132. ** lua_number2integer is a macro to convert lua_Number to lua_Integer.
  133. ** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned.
  134. ** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number.
  135. ** luai_hashnum is a macro to hash a lua_Number value into an integer.
  136. ** The hash must be deterministic and give reasonable values for
  137. ** both small and large values (outside the range of integers).
  138. */
  139. #if defined(MS_ASMTRICK) || defined(LUA_MSASMTRICK) /* { */
  140. /* trick with Microsoft assembler for X86 */
  141. #define lua_number2int(i,n) __asm {__asm fld n __asm fistp i}
  142. #define lua_number2integer(i,n) lua_number2int(i, n)
  143. #define lua_number2unsigned(i,n) \
  144. {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;}
  145. #elif defined(LUA_IEEE754TRICK) /* }{ */
  146. /* the next trick should work on any machine using IEEE754 with
  147. a 32-bit int type */
  148. union luai_Cast { double l_d; LUA_INT32 l_p[2]; };
  149. #if !defined(LUA_IEEEENDIAN) /* { */
  150. #define LUAI_EXTRAIEEE \
  151. static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)};
  152. #define LUA_IEEEENDIANLOC (ieeeendian.l_p[1] == 33)
  153. #else
  154. #define LUA_IEEEENDIANLOC LUA_IEEEENDIAN
  155. #define LUAI_EXTRAIEEE /* empty */
  156. #endif /* } */
  157. #define lua_number2int32(i,n,t) \
  158. { LUAI_EXTRAIEEE \
  159. volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \
  160. (i) = (t)u.l_p[LUA_IEEEENDIANLOC]; }
  161. #define luai_hashnum(i,n) \
  162. { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \
  163. (i) = u.l_p[0]; (i) += u.l_p[1]; } /* add double bits for his hash */
  164. #define lua_number2int(i,n) lua_number2int32(i, n, int)
  165. #define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned)
  166. /* the trick can be expanded to lua_Integer when it is a 32-bit value */
  167. #if defined(LUA_IEEELL)
  168. #define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer)
  169. #endif
  170. #endif /* } */
  171. /* the following definitions always work, but may be slow */
  172. #if !defined(lua_number2int)
  173. #define lua_number2int(i,n) ((i)=(int)(n))
  174. #endif
  175. #if !defined(lua_number2integer)
  176. #define lua_number2integer(i,n) ((i)=(lua_Integer)(n))
  177. #endif
  178. #if !defined(lua_number2unsigned) /* { */
  179. /* the following definition assures proper modulo behavior */
  180. #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT)
  181. #include <math.h>
  182. #define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1)
  183. #define lua_number2unsigned(i,n) \
  184. ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED))
  185. #else
  186. #define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n))
  187. #endif
  188. #endif /* } */
  189. #if !defined(lua_unsigned2number)
  190. /* on several machines, coercion from unsigned to double is slow,
  191. so it may be worth to avoid */
  192. #define lua_unsigned2number(u) \
  193. (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u))
  194. #endif
  195. #if defined(ltable_c) && !defined(luai_hashnum)
  196. #include <float.h>
  197. #include <math.h>
  198. #define luai_hashnum(i,n) { int e; \
  199. n = l_mathop(frexp)(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \
  200. lua_number2int(i, n); i += e; }
  201. #endif
  202. /*
  203. ** macro to control inclusion of some hard tests on stack reallocation
  204. */
  205. #if !defined(HARDSTACKTESTS)
  206. #define condmovestack(L) ((void)0)
  207. #else
  208. /* realloc stack keeping its size */
  209. #define condmovestack(L) luaD_reallocstack((L), (L)->stacksize)
  210. #endif
  211. #if !defined(HARDMEMTESTS)
  212. #define condchangemem(L) condmovestack(L)
  213. #else
  214. #define condchangemem(L) \
  215. ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1)))
  216. #endif
  217. #endif