llimits.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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' is an unsigned integer big enough to count the total memory
  13. ** used by Lua (in bytes). 'l_obj' is a signed integer big enough to
  14. ** count the total number of objects used by Lua. (It is signed due
  15. ** to the use of debt in several computations.) Usually, 'size_t' and
  16. ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
  17. */
  18. #if defined(LUAI_MEM) /* { external definitions? */
  19. typedef LUAI_UMEM lu_mem;
  20. typedef LUAI_MEM l_obj;
  21. #elif LUAI_IS32INT /* }{ */
  22. typedef size_t lu_mem;
  23. typedef ptrdiff_t l_obj;
  24. #else /* 16-bit ints */ /* }{ */
  25. typedef unsigned long lu_mem;
  26. typedef long l_obj;
  27. #endif /* } */
  28. #define MAX_LOBJ \
  29. cast(l_obj, (cast(lu_mem, 1) << (sizeof(l_obj) * CHAR_BIT - 1)) - 1)
  30. /* chars used as small naturals (so that 'char' is reserved for characters) */
  31. typedef unsigned char lu_byte;
  32. typedef signed char ls_byte;
  33. /* maximum value for size_t */
  34. #define MAX_SIZET ((size_t)(~(size_t)0))
  35. /*
  36. ** Maximum size for strings and userdata visible for Lua (should be
  37. ** representable in a lua_Integer)
  38. */
  39. #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
  40. : (size_t)(LUA_MAXINTEGER))
  41. #define MAX_INT INT_MAX /* maximum value of an int */
  42. /*
  43. ** floor of the log2 of the maximum signed value for integral type 't'.
  44. ** (That is, maximum 'n' such that '2^n' fits in the given signed type.)
  45. */
  46. #define log2maxs(t) cast_int(sizeof(t) * 8 - 2)
  47. /*
  48. ** test whether an unsigned value is a power of 2 (or zero)
  49. */
  50. #define ispow2(x) (((x) & ((x) - 1)) == 0)
  51. /* number of chars of a literal string without the ending \0 */
  52. #define LL(x) (sizeof(x)/sizeof(char) - 1)
  53. /*
  54. ** conversion of pointer to unsigned integer: this is for hashing only;
  55. ** there is no problem if the integer cannot hold the whole pointer
  56. ** value. (In strict ISO C this may cause undefined behavior, but no
  57. ** actual machine seems to bother.)
  58. */
  59. #if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \
  60. __STDC_VERSION__ >= 199901L
  61. #include <stdint.h>
  62. #if defined(UINTPTR_MAX) /* even in C99 this type is optional */
  63. #define L_P2I uintptr_t
  64. #else /* no 'intptr'? */
  65. #define L_P2I uintmax_t /* use the largest available integer */
  66. #endif
  67. #else /* C89 option */
  68. #define L_P2I size_t
  69. #endif
  70. #define point2uint(p) cast_uint((L_P2I)(p) & UINT_MAX)
  71. /* types of 'usual argument conversions' for lua_Number and lua_Integer */
  72. typedef LUAI_UACNUMBER l_uacNumber;
  73. typedef LUAI_UACINT l_uacInt;
  74. /*
  75. ** Internal assertions for in-house debugging
  76. */
  77. #if defined LUAI_ASSERT
  78. #undef NDEBUG
  79. #include <assert.h>
  80. #define lua_assert(c) assert(c)
  81. #endif
  82. #if defined(lua_assert)
  83. #define check_exp(c,e) (lua_assert(c), (e))
  84. /* to avoid problems with conditions too long */
  85. #define lua_longassert(c) ((c) ? (void)0 : lua_assert(0))
  86. #else
  87. #define lua_assert(c) ((void)0)
  88. #define check_exp(c,e) (e)
  89. #define lua_longassert(c) ((void)0)
  90. #endif
  91. /*
  92. ** assertion for checking API calls
  93. */
  94. #if !defined(luai_apicheck)
  95. #define luai_apicheck(l,e) ((void)l, lua_assert(e))
  96. #endif
  97. #define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
  98. /* macro to avoid warnings about unused variables */
  99. #if !defined(UNUSED)
  100. #define UNUSED(x) ((void)(x))
  101. #endif
  102. /* type casts (a macro highlights casts in the code) */
  103. #define cast(t, exp) ((t)(exp))
  104. #define cast_void(i) cast(void, (i))
  105. #define cast_voidp(i) cast(void *, (i))
  106. #define cast_num(i) cast(lua_Number, (i))
  107. #define cast_int(i) cast(int, (i))
  108. #define cast_uint(i) cast(unsigned int, (i))
  109. #define cast_byte(i) cast(lu_byte, (i))
  110. #define cast_uchar(i) cast(unsigned char, (i))
  111. #define cast_char(i) cast(char, (i))
  112. #define cast_charp(i) cast(char *, (i))
  113. #define cast_sizet(i) cast(size_t, (i))
  114. /* cast a signed lua_Integer to lua_Unsigned */
  115. #if !defined(l_castS2U)
  116. #define l_castS2U(i) ((lua_Unsigned)(i))
  117. #endif
  118. /*
  119. ** cast a lua_Unsigned to a signed lua_Integer; this cast is
  120. ** not strict ISO C, but two-complement architectures should
  121. ** work fine.
  122. */
  123. #if !defined(l_castU2S)
  124. #define l_castU2S(i) ((lua_Integer)(i))
  125. #endif
  126. /*
  127. ** non-return type
  128. */
  129. #if !defined(l_noret)
  130. #if defined(__GNUC__)
  131. #define l_noret void __attribute__((noreturn))
  132. #elif defined(_MSC_VER) && _MSC_VER >= 1200
  133. #define l_noret void __declspec(noreturn)
  134. #else
  135. #define l_noret void
  136. #endif
  137. #endif
  138. /*
  139. ** Inline functions
  140. */
  141. #if !defined(LUA_USE_C89)
  142. #define l_inline inline
  143. #elif defined(__GNUC__)
  144. #define l_inline __inline__
  145. #else
  146. #define l_inline /* empty */
  147. #endif
  148. #define l_sinline static l_inline
  149. /*
  150. ** type for virtual-machine instructions;
  151. ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
  152. */
  153. #if LUAI_IS32INT
  154. typedef unsigned int l_uint32;
  155. #else
  156. typedef unsigned long l_uint32;
  157. #endif
  158. typedef l_uint32 Instruction;
  159. /*
  160. ** Maximum length for short strings, that is, strings that are
  161. ** internalized. (Cannot be smaller than reserved words or tags for
  162. ** metamethods, as these strings must be internalized;
  163. ** #("function") = 8, #("__newindex") = 10.)
  164. */
  165. #if !defined(LUAI_MAXSHORTLEN)
  166. #define LUAI_MAXSHORTLEN 40
  167. #endif
  168. /*
  169. ** Initial size for the string table (must be power of 2).
  170. ** The Lua core alone registers ~50 strings (reserved words +
  171. ** metaevent keys + a few others). Libraries would typically add
  172. ** a few dozens more.
  173. */
  174. #if !defined(MINSTRTABSIZE)
  175. #define MINSTRTABSIZE 128
  176. #endif
  177. /*
  178. ** Size of cache for strings in the API. 'N' is the number of
  179. ** sets (better be a prime) and "M" is the size of each set (M == 1
  180. ** makes a direct cache.)
  181. */
  182. #if !defined(STRCACHE_N)
  183. #define STRCACHE_N 53
  184. #define STRCACHE_M 2
  185. #endif
  186. /* minimum size for string buffer */
  187. #if !defined(LUA_MINBUFFER)
  188. #define LUA_MINBUFFER 32
  189. #endif
  190. /*
  191. ** Maximum depth for nested C calls, syntactical nested non-terminals,
  192. ** and other features implemented through recursion in C. (Value must
  193. ** fit in a 16-bit unsigned integer. It must also be compatible with
  194. ** the size of the C stack.)
  195. */
  196. #if !defined(LUAI_MAXCCALLS)
  197. #define LUAI_MAXCCALLS 200
  198. #endif
  199. /*
  200. ** macros that are executed whenever program enters the Lua core
  201. ** ('lua_lock') and leaves the core ('lua_unlock')
  202. */
  203. #if !defined(lua_lock)
  204. #define lua_lock(L) ((void) 0)
  205. #define lua_unlock(L) ((void) 0)
  206. #endif
  207. /*
  208. ** macro executed during Lua functions at points where the
  209. ** function can yield.
  210. */
  211. #if !defined(luai_threadyield)
  212. #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
  213. #endif
  214. /*
  215. ** these macros allow user-specific actions when a thread is
  216. ** created/deleted/resumed/yielded.
  217. */
  218. #if !defined(luai_userstateopen)
  219. #define luai_userstateopen(L) ((void)L)
  220. #endif
  221. #if !defined(luai_userstateclose)
  222. #define luai_userstateclose(L) ((void)L)
  223. #endif
  224. #if !defined(luai_userstatethread)
  225. #define luai_userstatethread(L,L1) ((void)L)
  226. #endif
  227. #if !defined(luai_userstatefree)
  228. #define luai_userstatefree(L,L1) ((void)L)
  229. #endif
  230. #if !defined(luai_userstateresume)
  231. #define luai_userstateresume(L,n) ((void)L)
  232. #endif
  233. #if !defined(luai_userstateyield)
  234. #define luai_userstateyield(L,n) ((void)L)
  235. #endif
  236. /*
  237. ** The luai_num* macros define the primitive operations over numbers.
  238. */
  239. /* floor division (defined as 'floor(a/b)') */
  240. #if !defined(luai_numidiv)
  241. #define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b)))
  242. #endif
  243. /* float division */
  244. #if !defined(luai_numdiv)
  245. #define luai_numdiv(L,a,b) ((a)/(b))
  246. #endif
  247. /*
  248. ** modulo: defined as 'a - floor(a/b)*b'; the direct computation
  249. ** using this definition has several problems with rounding errors,
  250. ** so it is better to use 'fmod'. 'fmod' gives the result of
  251. ** 'a - trunc(a/b)*b', and therefore must be corrected when
  252. ** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a
  253. ** non-integer negative result: non-integer result is equivalent to
  254. ** a non-zero remainder 'm'; negative result is equivalent to 'a' and
  255. ** 'b' with different signs, or 'm' and 'b' with different signs
  256. ** (as the result 'm' of 'fmod' has the same sign of 'a').
  257. */
  258. #if !defined(luai_nummod)
  259. #define luai_nummod(L,a,b,m) \
  260. { (void)L; (m) = l_mathop(fmod)(a,b); \
  261. if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }
  262. #endif
  263. /* exponentiation */
  264. #if !defined(luai_numpow)
  265. #define luai_numpow(L,a,b) \
  266. ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b))
  267. #endif
  268. /* the others are quite standard operations */
  269. #if !defined(luai_numadd)
  270. #define luai_numadd(L,a,b) ((a)+(b))
  271. #define luai_numsub(L,a,b) ((a)-(b))
  272. #define luai_nummul(L,a,b) ((a)*(b))
  273. #define luai_numunm(L,a) (-(a))
  274. #define luai_numeq(a,b) ((a)==(b))
  275. #define luai_numlt(a,b) ((a)<(b))
  276. #define luai_numle(a,b) ((a)<=(b))
  277. #define luai_numgt(a,b) ((a)>(b))
  278. #define luai_numge(a,b) ((a)>=(b))
  279. #define luai_numisnan(a) (!luai_numeq((a), (a)))
  280. #endif
  281. /*
  282. ** macro to control inclusion of some hard tests on stack reallocation
  283. */
  284. #if !defined(HARDSTACKTESTS)
  285. #define condmovestack(L,pre,pos) ((void)0)
  286. #else
  287. /* realloc stack keeping its size */
  288. #define condmovestack(L,pre,pos) \
  289. { int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; }
  290. #endif
  291. #if !defined(HARDMEMTESTS)
  292. #define condchangemem(L,pre,pos) ((void)0)
  293. #else
  294. #define condchangemem(L,pre,pos) \
  295. { if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } }
  296. #endif
  297. #endif