llimits.h 7.5 KB

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