llimits.h 7.8 KB

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