llimits.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. ** $Id: llimits.h,v 1.141 2015/11/19 19:16:22 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. /*
  12. ** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count
  13. ** the total memory used by Lua (in bytes). Usually, 'size_t' and
  14. ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
  15. */
  16. #if defined(LUAI_MEM) /* { external definitions? */
  17. typedef LUAI_UMEM lu_mem;
  18. typedef LUAI_MEM l_mem;
  19. #elif LUAI_BITSINT >= 32 /* }{ */
  20. typedef size_t lu_mem;
  21. typedef ptrdiff_t l_mem;
  22. #else /* 16-bit ints */ /* }{ */
  23. typedef unsigned long lu_mem;
  24. typedef long l_mem;
  25. #endif /* } */
  26. /* chars used as small naturals (so that 'char' is reserved for characters) */
  27. typedef unsigned char lu_byte;
  28. /* maximum value for size_t */
  29. #define MAX_SIZET ((size_t)(~(size_t)0))
  30. /* maximum size visible for Lua (must be representable in a lua_Integer */
  31. #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
  32. : (size_t)(LUA_MAXINTEGER))
  33. #define MAX_LUMEM ((lu_mem)(~(lu_mem)0))
  34. #define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1))
  35. #define MAX_INT INT_MAX /* maximum value of an int */
  36. /*
  37. ** conversion of pointer to unsigned integer:
  38. ** this is for hashing only; there is no problem if the integer
  39. ** cannot hold the whole pointer value
  40. */
  41. #define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX))
  42. /* type to ensure maximum alignment */
  43. #if defined(LUAI_USER_ALIGNMENT_T)
  44. typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
  45. #else
  46. typedef union {
  47. lua_Number n;
  48. double u;
  49. void *s;
  50. lua_Integer i;
  51. long l;
  52. } L_Umaxalign;
  53. #endif
  54. /* types of 'usual argument conversions' for lua_Number and lua_Integer */
  55. typedef LUAI_UACNUMBER l_uacNumber;
  56. typedef LUAI_UACINT l_uacInt;
  57. /* internal assertions for in-house debugging */
  58. #if defined(lua_assert)
  59. #define check_exp(c,e) (lua_assert(c), (e))
  60. /* to avoid problems with conditions too long */
  61. #define lua_longassert(c) ((c) ? (void)0 : lua_assert(0))
  62. #else
  63. #define lua_assert(c) ((void)0)
  64. #define check_exp(c,e) (e)
  65. #define lua_longassert(c) ((void)0)
  66. #endif
  67. /*
  68. ** assertion for checking API calls
  69. */
  70. #if !defined(luai_apicheck)
  71. #define luai_apicheck(l,e) lua_assert(e)
  72. #endif
  73. #define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
  74. /* macro to avoid warnings about unused variables */
  75. #if !defined(UNUSED)
  76. #define UNUSED(x) ((void)(x))
  77. #endif
  78. /* type casts (a macro highlights casts in the code) */
  79. #define cast(t, exp) ((t)(exp))
  80. #define cast_void(i) cast(void, (i))
  81. #define cast_byte(i) cast(lu_byte, (i))
  82. #define cast_num(i) cast(lua_Number, (i))
  83. #define cast_int(i) cast(int, (i))
  84. #define cast_uchar(i) cast(unsigned char, (i))
  85. /* cast a signed lua_Integer to lua_Unsigned */
  86. #if !defined(l_castS2U)
  87. #define l_castS2U(i) ((lua_Unsigned)(i))
  88. #endif
  89. /*
  90. ** cast a lua_Unsigned to a signed lua_Integer; this cast is
  91. ** not strict ISO C, but two-complement architectures should
  92. ** work fine.
  93. */
  94. #if !defined(l_castU2S)
  95. #define l_castU2S(i) ((lua_Integer)(i))
  96. #endif
  97. /*
  98. ** non-return type
  99. */
  100. #if defined(__GNUC__)
  101. #define l_noret void __attribute__((noreturn))
  102. #elif defined(_MSC_VER) && _MSC_VER >= 1200
  103. #define l_noret void __declspec(noreturn)
  104. #else
  105. #define l_noret void
  106. #endif
  107. /*
  108. ** maximum depth for nested C calls and syntactical nested non-terminals
  109. ** in a program. (Value must fit in an unsigned short int.)
  110. */
  111. #if !defined(LUAI_MAXCCALLS)
  112. #define LUAI_MAXCCALLS 200
  113. #endif
  114. /*
  115. ** type for virtual-machine instructions;
  116. ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
  117. */
  118. #if LUAI_BITSINT >= 32
  119. typedef unsigned int Instruction;
  120. #else
  121. typedef unsigned long Instruction;
  122. #endif
  123. /*
  124. ** Maximum length for short strings, that is, strings that are
  125. ** internalized. (Cannot be smaller than reserved words or tags for
  126. ** metamethods, as these strings must be internalized;
  127. ** #("function") = 8, #("__newindex") = 10.)
  128. */
  129. #if !defined(LUAI_MAXSHORTLEN)
  130. #define LUAI_MAXSHORTLEN 40
  131. #endif
  132. /*
  133. ** Initial size for the string table (must be power of 2).
  134. ** The Lua core alone registers ~50 strings (reserved words +
  135. ** metaevent keys + a few others). Libraries would typically add
  136. ** a few dozens more.
  137. */
  138. #if !defined(MINSTRTABSIZE)
  139. #define MINSTRTABSIZE 128
  140. #endif
  141. /*
  142. ** Size of cache for strings in the API. 'N' is the number of
  143. ** sets (better be a prime) and "M" is the size of each set (M == 1
  144. ** makes a direct cache.)
  145. */
  146. #if !defined(STRCACHE_N)
  147. #define STRCACHE_N 53
  148. #define STRCACHE_M 2
  149. #endif
  150. /* minimum size for string buffer */
  151. #if !defined(LUA_MINBUFFER)
  152. #define LUA_MINBUFFER 32
  153. #endif
  154. /*
  155. ** macros that are executed whenever program enters the Lua core
  156. ** ('lua_lock') and leaves the core ('lua_unlock')
  157. */
  158. #if !defined(lua_lock)
  159. #define lua_lock(L) ((void) 0)
  160. #define lua_unlock(L) ((void) 0)
  161. #endif
  162. /*
  163. ** macro executed during Lua functions at points where the
  164. ** function can yield.
  165. */
  166. #if !defined(luai_threadyield)
  167. #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
  168. #endif
  169. /*
  170. ** these macros allow user-specific actions on threads when you defined
  171. ** LUAI_EXTRASPACE and need to do something extra when a thread is
  172. ** created/deleted/resumed/yielded.
  173. */
  174. #if !defined(luai_userstateopen)
  175. #define luai_userstateopen(L) ((void)L)
  176. #endif
  177. #if !defined(luai_userstateclose)
  178. #define luai_userstateclose(L) ((void)L)
  179. #endif
  180. #if !defined(luai_userstatethread)
  181. #define luai_userstatethread(L,L1) ((void)L)
  182. #endif
  183. #if !defined(luai_userstatefree)
  184. #define luai_userstatefree(L,L1) ((void)L)
  185. #endif
  186. #if !defined(luai_userstateresume)
  187. #define luai_userstateresume(L,n) ((void)L)
  188. #endif
  189. #if !defined(luai_userstateyield)
  190. #define luai_userstateyield(L,n) ((void)L)
  191. #endif
  192. /*
  193. ** The luai_num* macros define the primitive operations over numbers.
  194. */
  195. /* floor division (defined as 'floor(a/b)') */
  196. #if !defined(luai_numidiv)
  197. #define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b)))
  198. #endif
  199. /* float division */
  200. #if !defined(luai_numdiv)
  201. #define luai_numdiv(L,a,b) ((a)/(b))
  202. #endif
  203. /*
  204. ** modulo: defined as 'a - floor(a/b)*b'; this definition gives NaN when
  205. ** 'b' is huge, but the result should be 'a'. 'fmod' gives the result of
  206. ** 'a - trunc(a/b)*b', and therefore must be corrected when 'trunc(a/b)
  207. ** ~= floor(a/b)'. That happens when the division has a non-integer
  208. ** negative result, which is equivalent to the test below.
  209. */
  210. #if !defined(luai_nummod)
  211. #define luai_nummod(L,a,b,m) \
  212. { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); }
  213. #endif
  214. /* exponentiation */
  215. #if !defined(luai_numpow)
  216. #define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b))
  217. #endif
  218. /* the others are quite standard operations */
  219. #if !defined(luai_numadd)
  220. #define luai_numadd(L,a,b) ((a)+(b))
  221. #define luai_numsub(L,a,b) ((a)-(b))
  222. #define luai_nummul(L,a,b) ((a)*(b))
  223. #define luai_numunm(L,a) (-(a))
  224. #define luai_numeq(a,b) ((a)==(b))
  225. #define luai_numlt(a,b) ((a)<(b))
  226. #define luai_numle(a,b) ((a)<=(b))
  227. #define luai_numisnan(a) (!luai_numeq((a), (a)))
  228. #endif
  229. /*
  230. ** macro to control inclusion of some hard tests on stack reallocation
  231. */
  232. #if !defined(HARDSTACKTESTS)
  233. #define condmovestack(L,pre,pos) ((void)0)
  234. #else
  235. /* realloc stack keeping its size */
  236. #define condmovestack(L,pre,pos) \
  237. { int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_); pos; }
  238. #endif
  239. #if !defined(HARDMEMTESTS)
  240. #define condchangemem(L,pre,pos) ((void)0)
  241. #else
  242. #define condchangemem(L,pre,pos) \
  243. { if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } }
  244. #endif
  245. #endif