llimits.h 7.6 KB

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