llimits.h 8.7 KB

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