llimits.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. ** $Id: llimits.h,v 1.124 2014/11/02 19:33:33 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. /* maximum value for size_t */
  29. #define MAX_SIZET ((size_t)(~(size_t)0))
  30. /* maximum size visible for Lua (must be representable in a lua_Integer */
  31. #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
  32. : (size_t)(LUA_MAXINTEGER))
  33. #define MAX_LUMEM ((lu_mem)(~(lu_mem)0))
  34. #define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1))
  35. #define MAX_INT INT_MAX /* maximum value of an int */
  36. /*
  37. ** conversion of pointer to integer:
  38. ** this is for hashing only; there is no problem if the integer
  39. ** cannot hold the whole pointer value
  40. */
  41. #define point2int(p) ((unsigned int)((size_t)(p) & UINT_MAX))
  42. /* type to ensure maximum alignment */
  43. #if defined(LUAI_USER_ALIGNMENT_T)
  44. typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
  45. #else
  46. typedef union { double u; void *s; lua_Integer i; long l; } L_Umaxalign;
  47. #endif
  48. /* types of 'usual argument conversions' for lua_Number and lua_Integer */
  49. typedef LUAI_UACNUMBER l_uacNumber;
  50. typedef LUAI_UACINT l_uacInt;
  51. /* internal assertions for in-house debugging */
  52. #if defined(lua_assert)
  53. #define check_exp(c,e) (lua_assert(c), (e))
  54. /* to avoid problems with conditions too long */
  55. #define lua_longassert(c) { if (!(c)) lua_assert(0); }
  56. #else
  57. #define lua_assert(c) ((void)0)
  58. #define check_exp(c,e) (e)
  59. #define lua_longassert(c) ((void)0)
  60. #endif
  61. /*
  62. ** assertion for checking API calls
  63. */
  64. #if defined(LUA_USE_APICHECK)
  65. #include <assert.h>
  66. #define luai_apicheck(e) assert(e)
  67. #else
  68. #define luai_apicheck(e) lua_assert(e)
  69. #endif
  70. #define api_check(e,msg) luai_apicheck((e) && msg)
  71. #if !defined(UNUSED)
  72. #define UNUSED(x) ((void)(x)) /* to avoid warnings */
  73. #endif
  74. #define cast(t, exp) ((t)(exp))
  75. #define cast_void(i) cast(void, (i))
  76. #define cast_byte(i) cast(lu_byte, (i))
  77. #define cast_num(i) cast(lua_Number, (i))
  78. #define cast_int(i) cast(int, (i))
  79. #define cast_uchar(i) cast(unsigned char, (i))
  80. /* cast a signed lua_Integer to lua_Unsigned */
  81. #if !defined(l_castS2U)
  82. #define l_castS2U(i) ((lua_Unsigned)(i))
  83. #endif
  84. /*
  85. ** cast a lua_Unsigned to a signed lua_Integer; this cast is
  86. ** not strict ISO C, but two-complement architectures should
  87. ** work fine.
  88. */
  89. #if !defined(l_castU2S)
  90. #define l_castU2S(i) ((lua_Integer)(i))
  91. #endif
  92. /*
  93. ** non-return type
  94. */
  95. #if defined(__GNUC__)
  96. #define l_noret void __attribute__((noreturn))
  97. #elif defined(_MSC_VER) && _MSC_VER >= 1200
  98. #define l_noret void __declspec(noreturn)
  99. #else
  100. #define l_noret void
  101. #endif
  102. /*
  103. ** maximum depth for nested C calls and syntactical nested non-terminals
  104. ** in a program. (Value must fit in an unsigned short int.)
  105. */
  106. #if !defined(LUAI_MAXCCALLS)
  107. #define LUAI_MAXCCALLS 200
  108. #endif
  109. /*
  110. ** maximum number of upvalues in a closure (both C and Lua). (Value
  111. ** must fit in an unsigned char.)
  112. */
  113. #define MAXUPVAL UCHAR_MAX
  114. /*
  115. ** type for virtual-machine instructions;
  116. ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
  117. */
  118. #if LUAI_BITSINT >= 32
  119. typedef unsigned int Instruction;
  120. #else
  121. typedef unsigned long Instruction;
  122. #endif
  123. /* minimum size for the string table (must be power of 2) */
  124. #if !defined(MINSTRTABSIZE)
  125. #define MINSTRTABSIZE 64 /* minimum size for "predefined" strings */
  126. #endif
  127. /* minimum size for string buffer */
  128. #if !defined(LUA_MINBUFFER)
  129. #define LUA_MINBUFFER 32
  130. #endif
  131. #if !defined(lua_lock)
  132. #define lua_lock(L) ((void) 0)
  133. #define lua_unlock(L) ((void) 0)
  134. #endif
  135. #if !defined(luai_threadyield)
  136. #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
  137. #endif
  138. /*
  139. ** these macros allow user-specific actions on threads when you defined
  140. ** LUAI_EXTRASPACE and need to do something extra when a thread is
  141. ** created/deleted/resumed/yielded.
  142. */
  143. #if !defined(luai_userstateopen)
  144. #define luai_userstateopen(L) ((void)L)
  145. #endif
  146. #if !defined(luai_userstateclose)
  147. #define luai_userstateclose(L) ((void)L)
  148. #endif
  149. #if !defined(luai_userstatethread)
  150. #define luai_userstatethread(L,L1) ((void)L)
  151. #endif
  152. #if !defined(luai_userstatefree)
  153. #define luai_userstatefree(L,L1) ((void)L)
  154. #endif
  155. #if !defined(luai_userstateresume)
  156. #define luai_userstateresume(L,n) ((void)L)
  157. #endif
  158. #if !defined(luai_userstateyield)
  159. #define luai_userstateyield(L,n) ((void)L)
  160. #endif
  161. /*
  162. ** macro to control inclusion of some hard tests on stack reallocation
  163. */
  164. #if !defined(HARDSTACKTESTS)
  165. #define condmovestack(L) ((void)0)
  166. #else
  167. /* realloc stack keeping its size */
  168. #define condmovestack(L) luaD_reallocstack((L), (L)->stacksize)
  169. #endif
  170. #if !defined(HARDMEMTESTS)
  171. #define condchangemem(L) condmovestack(L)
  172. #else
  173. #define condchangemem(L) \
  174. ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1)))
  175. #endif
  176. #endif