llimits.h 6.7 KB

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