llimits.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. ** $Id: llimits.h,v 1.104 2013/04/25 21:15:37 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 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. #define cast_integer(i) cast(lua_Integer, (i))
  64. #define cast_unsigned(i) cast(lua_Unsigned, (i))
  65. /*
  66. ** non-return type
  67. */
  68. #if defined(__GNUC__)
  69. #define l_noret void __attribute__((noreturn))
  70. #elif defined(_MSC_VER)
  71. #define l_noret void __declspec(noreturn)
  72. #else
  73. #define l_noret void
  74. #endif
  75. /*
  76. ** maximum depth for nested C calls and syntactical nested non-terminals
  77. ** in a program. (Value must fit in an unsigned short int.)
  78. */
  79. #if !defined(LUAI_MAXCCALLS)
  80. #define LUAI_MAXCCALLS 200
  81. #endif
  82. /*
  83. ** maximum number of upvalues in a closure (both C and Lua). (Value
  84. ** must fit in an unsigned char.)
  85. */
  86. #define MAXUPVAL UCHAR_MAX
  87. /*
  88. ** type for virtual-machine instructions
  89. ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
  90. */
  91. typedef lu_int32 Instruction;
  92. /* maximum stack for a Lua function */
  93. #define MAXSTACK 250
  94. /* minimum size for the string table (must be power of 2) */
  95. #if !defined(MINSTRTABSIZE)
  96. #define MINSTRTABSIZE 32
  97. #endif
  98. /* minimum size for string buffer */
  99. #if !defined(LUA_MINBUFFER)
  100. #define LUA_MINBUFFER 32
  101. #endif
  102. #if !defined(lua_lock)
  103. #define lua_lock(L) ((void) 0)
  104. #define lua_unlock(L) ((void) 0)
  105. #endif
  106. #if !defined(luai_threadyield)
  107. #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
  108. #endif
  109. /*
  110. ** these macros allow user-specific actions on threads when you defined
  111. ** LUAI_EXTRASPACE and need to do something extra when a thread is
  112. ** created/deleted/resumed/yielded.
  113. */
  114. #if !defined(luai_userstateopen)
  115. #define luai_userstateopen(L) ((void)L)
  116. #endif
  117. #if !defined(luai_userstateclose)
  118. #define luai_userstateclose(L) ((void)L)
  119. #endif
  120. #if !defined(luai_userstatethread)
  121. #define luai_userstatethread(L,L1) ((void)L)
  122. #endif
  123. #if !defined(luai_userstatefree)
  124. #define luai_userstatefree(L,L1) ((void)L)
  125. #endif
  126. #if !defined(luai_userstateresume)
  127. #define luai_userstateresume(L,n) ((void)L)
  128. #endif
  129. #if !defined(luai_userstateyield)
  130. #define luai_userstateyield(L,n) ((void)L)
  131. #endif
  132. /*
  133. ** lua_number2int is a macro to convert lua_Number to int.
  134. ** lua_number2integer is a macro to convert lua_Number to lua_Integer.
  135. ** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned.
  136. ** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number.
  137. ** luai_hashnum is a macro to hash a lua_Number value into an integer.
  138. ** The hash must be deterministic and give reasonable values for
  139. ** both small and large values (outside the range of integers).
  140. */
  141. #if !defined(lua_number2int)
  142. #define lua_number2int(i,n) ((i)=(int)(n))
  143. #endif
  144. #if !defined(lua_number2integer)
  145. #define lua_number2integer(i,n) ((i)=(lua_Integer)(n))
  146. #endif
  147. #if !defined(lua_number2unsigned) /* { */
  148. /* the following definition assures proper modulo behavior */
  149. #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT)
  150. #include <math.h>
  151. #define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1)
  152. #define lua_number2unsigned(i,n) \
  153. ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED))
  154. #else
  155. #define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n))
  156. #endif
  157. #endif /* } */
  158. #if defined(ltable_c) && !defined(luai_hashnum)
  159. #include <float.h>
  160. #include <math.h>
  161. #define luai_hashnum(i,n) { int e; \
  162. n = l_mathop(frexp)(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \
  163. lua_number2int(i, n); i += e; }
  164. #endif
  165. /*
  166. ** macro to control inclusion of some hard tests on stack reallocation
  167. */
  168. #if !defined(HARDSTACKTESTS)
  169. #define condmovestack(L) ((void)0)
  170. #else
  171. /* realloc stack keeping its size */
  172. #define condmovestack(L) luaD_reallocstack((L), (L)->stacksize)
  173. #endif
  174. #if !defined(HARDMEMTESTS)
  175. #define condchangemem(L) condmovestack(L)
  176. #else
  177. #define condchangemem(L) \
  178. ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1)))
  179. #endif
  180. #endif