llimits.h 8.0 KB

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