llimits.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. ** 'l_mem' is a signed integer big enough to count the total memory
  13. ** used by Lua. (It is signed due to the use of debt in several
  14. ** computations.) Usually, 'ptrdiff_t' should work, but we use 'long'
  15. ** for 16-bit machines.
  16. */
  17. #if defined(LUAI_MEM) /* { external definitions? */
  18. typedef LUAI_MEM l_mem;
  19. typedef LUAI_UMEM lu_mem;
  20. #elif LUAI_IS32INT /* }{ */
  21. typedef ptrdiff_t l_mem;
  22. typedef size_t lu_mem;
  23. #else /* 16-bit ints */ /* }{ */
  24. typedef long l_mem;
  25. typedef unsigned long lu_mem;
  26. #endif /* } */
  27. #define MAX_LMEM \
  28. cast(l_mem, (cast(lu_mem, 1) << (sizeof(l_mem) * 8 - 1)) - 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. /* Type for thread status/error codes */
  33. typedef lu_byte TStatus;
  34. /* The C API still uses 'int' for status/error codes */
  35. #define APIstatus(st) cast_int(st)
  36. /* maximum value for size_t */
  37. #define MAX_SIZET ((size_t)(~(size_t)0))
  38. /*
  39. ** Maximum size for strings and userdata visible for Lua; should be
  40. ** representable as a lua_Integer and as a size_t.
  41. */
  42. #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
  43. : cast_sizet(LUA_MAXINTEGER))
  44. /*
  45. ** floor of the log2 of the maximum signed value for integral type 't'.
  46. ** (That is, maximum 'n' such that '2^n' fits in the given signed type.)
  47. */
  48. #define log2maxs(t) cast_int(sizeof(t) * 8 - 2)
  49. /*
  50. ** test whether an unsigned value is a power of 2 (or zero)
  51. */
  52. #define ispow2(x) (((x) & ((x) - 1)) == 0)
  53. /* number of chars of a literal string without the ending \0 */
  54. #define LL(x) (sizeof(x)/sizeof(char) - 1)
  55. /*
  56. ** conversion of pointer to unsigned integer: this is for hashing only;
  57. ** there is no problem if the integer cannot hold the whole pointer
  58. ** value. (In strict ISO C this may cause undefined behavior, but no
  59. ** actual machine seems to bother.)
  60. */
  61. #if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \
  62. __STDC_VERSION__ >= 199901L
  63. #include <stdint.h>
  64. #if defined(UINTPTR_MAX) /* even in C99 this type is optional */
  65. #define L_P2I uintptr_t
  66. #else /* no 'intptr'? */
  67. #define L_P2I uintmax_t /* use the largest available integer */
  68. #endif
  69. #else /* C89 option */
  70. #define L_P2I size_t
  71. #endif
  72. #define point2uint(p) cast_uint((L_P2I)(p) & UINT_MAX)
  73. /* types of 'usual argument conversions' for lua_Number and lua_Integer */
  74. typedef LUAI_UACNUMBER l_uacNumber;
  75. typedef LUAI_UACINT l_uacInt;
  76. /*
  77. ** Internal assertions for in-house debugging
  78. */
  79. #if defined LUAI_ASSERT
  80. #undef NDEBUG
  81. #include <assert.h>
  82. #define lua_assert(c) assert(c)
  83. #define assert_code(c) c
  84. #endif
  85. #if defined(lua_assert)
  86. #else
  87. #define lua_assert(c) ((void)0)
  88. #define assert_code(c) ((void)0)
  89. #endif
  90. #define check_exp(c,e) (lua_assert(c), (e))
  91. /* to avoid problems with conditions too long */
  92. #define lua_longassert(c) assert_code((c) ? (void)0 : lua_assert(0))
  93. /* macro to avoid warnings about unused variables */
  94. #if !defined(UNUSED)
  95. #define UNUSED(x) ((void)(x))
  96. #endif
  97. /* type casts (a macro highlights casts in the code) */
  98. #define cast(t, exp) ((t)(exp))
  99. #define cast_void(i) cast(void, (i))
  100. #define cast_voidp(i) cast(void *, (i))
  101. #define cast_num(i) cast(lua_Number, (i))
  102. #define cast_int(i) cast(int, (i))
  103. #define cast_uint(i) cast(unsigned int, (i))
  104. #define cast_ulong(i) cast(unsigned long, (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. ** cast a size_t to lua_Integer: These casts are always valid for
  124. ** sizes of Lua objects (see MAX_SIZE)
  125. */
  126. #define cast_st2S(sz) ((lua_Integer)(sz))
  127. /* Cast a ptrdiff_t to size_t, when it is known that the minuend
  128. ** comes from the subtrahend (the base)
  129. */
  130. #define ct_diff2sz(df) ((size_t)(df))
  131. /* ptrdiff_t to lua_Integer */
  132. #define ct_diff2S(df) cast_st2S(ct_diff2sz(df))
  133. /*
  134. ** Special type equivalent to '(void*)' for functions (to suppress some
  135. ** warnings when converting function pointers)
  136. */
  137. typedef void (*voidf)(void);
  138. /*
  139. ** Macro to convert pointer-to-void* to pointer-to-function. This cast
  140. ** is undefined according to ISO C, but POSIX assumes that it works.
  141. ** (The '__extension__' in gnu compilers is only to avoid warnings.)
  142. */
  143. #if defined(__GNUC__)
  144. #define cast_func(p) (__extension__ (voidf)(p))
  145. #else
  146. #define cast_func(p) ((voidf)(p))
  147. #endif
  148. /*
  149. ** non-return type
  150. */
  151. #if !defined(l_noret)
  152. #if defined(__GNUC__)
  153. #define l_noret void __attribute__((noreturn))
  154. #elif defined(_MSC_VER) && _MSC_VER >= 1200
  155. #define l_noret void __declspec(noreturn)
  156. #else
  157. #define l_noret void
  158. #endif
  159. #endif
  160. /*
  161. ** Inline functions
  162. */
  163. #if !defined(LUA_USE_C89)
  164. #define l_inline inline
  165. #elif defined(__GNUC__)
  166. #define l_inline __inline__
  167. #else
  168. #define l_inline /* empty */
  169. #endif
  170. #define l_sinline static l_inline
  171. /*
  172. ** An unsigned with (at least) 4 bytes
  173. */
  174. #if LUAI_IS32INT
  175. typedef unsigned int l_uint32;
  176. #else
  177. typedef unsigned long l_uint32;
  178. #endif
  179. /*
  180. ** The luai_num* macros define the primitive operations over numbers.
  181. */
  182. /* floor division (defined as 'floor(a/b)') */
  183. #if !defined(luai_numidiv)
  184. #define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b)))
  185. #endif
  186. /* float division */
  187. #if !defined(luai_numdiv)
  188. #define luai_numdiv(L,a,b) ((a)/(b))
  189. #endif
  190. /*
  191. ** modulo: defined as 'a - floor(a/b)*b'; the direct computation
  192. ** using this definition has several problems with rounding errors,
  193. ** so it is better to use 'fmod'. 'fmod' gives the result of
  194. ** 'a - trunc(a/b)*b', and therefore must be corrected when
  195. ** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a
  196. ** non-integer negative result: non-integer result is equivalent to
  197. ** a non-zero remainder 'm'; negative result is equivalent to 'a' and
  198. ** 'b' with different signs, or 'm' and 'b' with different signs
  199. ** (as the result 'm' of 'fmod' has the same sign of 'a').
  200. */
  201. #if !defined(luai_nummod)
  202. #define luai_nummod(L,a,b,m) \
  203. { (void)L; (m) = l_mathop(fmod)(a,b); \
  204. if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }
  205. #endif
  206. /* exponentiation */
  207. #if !defined(luai_numpow)
  208. #define luai_numpow(L,a,b) \
  209. ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b))
  210. #endif
  211. /* the others are quite standard operations */
  212. #if !defined(luai_numadd)
  213. #define luai_numadd(L,a,b) ((a)+(b))
  214. #define luai_numsub(L,a,b) ((a)-(b))
  215. #define luai_nummul(L,a,b) ((a)*(b))
  216. #define luai_numunm(L,a) (-(a))
  217. #define luai_numeq(a,b) ((a)==(b))
  218. #define luai_numlt(a,b) ((a)<(b))
  219. #define luai_numle(a,b) ((a)<=(b))
  220. #define luai_numgt(a,b) ((a)>(b))
  221. #define luai_numge(a,b) ((a)>=(b))
  222. #define luai_numisnan(a) (!luai_numeq((a), (a)))
  223. #endif
  224. /*
  225. ** {==================================================================
  226. ** "Abstraction Layer" for basic report of messages and errors
  227. ** ===================================================================
  228. */
  229. /* print a string */
  230. #if !defined(lua_writestring)
  231. #define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
  232. #endif
  233. /* print a newline and flush the output */
  234. #if !defined(lua_writeline)
  235. #define lua_writeline() (lua_writestring("\n", 1), fflush(stdout))
  236. #endif
  237. /* print an error message */
  238. #if !defined(lua_writestringerror)
  239. #define lua_writestringerror(s,p) \
  240. (fprintf(stderr, (s), (p)), fflush(stderr))
  241. #endif
  242. /* }================================================================== */
  243. #endif