llimits.h 7.7 KB

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