llimits.h 9.2 KB

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