llimits.h 8.1 KB

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