2000-03-24 20:26:08 +03:00
|
|
|
/*
|
2018-08-23 20:26:12 +03:00
|
|
|
** $Id: llimits.h $
|
2014-10-25 15:50:46 +04:00
|
|
|
** Limits, basic types, and some other 'installation-dependent' definitions
|
2000-03-24 20:26:08 +03:00
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2000-03-24 22:49:23 +03:00
|
|
|
#ifndef llimits_h
|
|
|
|
#define llimits_h
|
2000-03-24 20:26:08 +03:00
|
|
|
|
|
|
|
|
|
|
|
#include <limits.h>
|
2000-05-24 17:54:49 +04:00
|
|
|
#include <stddef.h>
|
2000-03-24 20:26:08 +03:00
|
|
|
|
|
|
|
|
2001-02-23 23:28:56 +03:00
|
|
|
#include "lua.h"
|
|
|
|
|
2019-05-13 20:24:10 +03:00
|
|
|
|
2014-10-29 20:07:45 +03:00
|
|
|
/*
|
2022-11-23 23:29:03 +03:00
|
|
|
** 'lu_mem' is an unsigned integer big enough to count the total memory
|
|
|
|
** used by Lua (in bytes). 'l_obj' is a signed integer big enough to
|
2022-12-13 17:55:14 +03:00
|
|
|
** count the total number of objects used by Lua. (It is signed due
|
2022-11-23 23:29:03 +03:00
|
|
|
** to the use of debt in several computations.) Usually, 'size_t' and
|
2014-10-29 20:07:45 +03:00
|
|
|
** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
|
|
|
|
*/
|
|
|
|
#if defined(LUAI_MEM) /* { external definitions? */
|
2005-03-09 19:28:07 +03:00
|
|
|
typedef LUAI_UMEM lu_mem;
|
2022-11-23 23:29:03 +03:00
|
|
|
typedef LUAI_MEM l_obj;
|
2019-05-13 20:24:10 +03:00
|
|
|
#elif LUAI_IS32INT /* }{ */
|
2014-10-29 20:07:45 +03:00
|
|
|
typedef size_t lu_mem;
|
2022-11-23 23:29:03 +03:00
|
|
|
typedef ptrdiff_t l_obj;
|
2014-10-29 20:07:45 +03:00
|
|
|
#else /* 16-bit ints */ /* }{ */
|
|
|
|
typedef unsigned long lu_mem;
|
2022-11-23 23:29:03 +03:00
|
|
|
typedef long l_obj;
|
2014-10-29 20:07:45 +03:00
|
|
|
#endif /* } */
|
2003-12-01 19:33:30 +03:00
|
|
|
|
2023-12-20 17:06:27 +03:00
|
|
|
#define MAX_LOBJ \
|
|
|
|
cast(l_obj, (cast(lu_mem, 1) << (sizeof(l_obj) * CHAR_BIT - 1)) - 1)
|
2022-12-13 21:45:57 +03:00
|
|
|
|
2003-12-01 19:33:30 +03:00
|
|
|
|
2014-10-25 15:50:46 +04:00
|
|
|
/* chars used as small naturals (so that 'char' is reserved for characters) */
|
2001-02-20 21:15:33 +03:00
|
|
|
typedef unsigned char lu_byte;
|
2017-06-27 14:35:01 +03:00
|
|
|
typedef signed char ls_byte;
|
2000-05-24 17:54:49 +04:00
|
|
|
|
|
|
|
|
2013-06-19 18:27:00 +04:00
|
|
|
/* maximum value for size_t */
|
2014-07-18 22:29:12 +04:00
|
|
|
#define MAX_SIZET ((size_t)(~(size_t)0))
|
2000-05-24 17:54:49 +04:00
|
|
|
|
2023-12-20 17:06:27 +03:00
|
|
|
/*
|
2024-06-20 19:43:33 +03:00
|
|
|
** Maximum size for strings and userdata visible for Lua; should be
|
|
|
|
** representable as a lua_Integer and as a size_t.
|
2023-12-20 17:06:27 +03:00
|
|
|
*/
|
2014-04-11 23:56:04 +04:00
|
|
|
#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
|
2024-06-20 19:43:33 +03:00
|
|
|
: cast_sizet(LUA_MAXINTEGER))
|
2017-06-01 22:16:34 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** floor of the log2 of the maximum signed value for integral type 't'.
|
|
|
|
** (That is, maximum 'n' such that '2^n' fits in the given signed type.)
|
|
|
|
*/
|
2022-11-23 23:17:20 +03:00
|
|
|
#define log2maxs(t) cast_int(sizeof(t) * 8 - 2)
|
2000-03-24 20:26:08 +03:00
|
|
|
|
2013-05-27 16:43:37 +04:00
|
|
|
|
2018-06-15 17:13:45 +03:00
|
|
|
/*
|
|
|
|
** test whether an unsigned value is a power of 2 (or zero)
|
|
|
|
*/
|
|
|
|
#define ispow2(x) (((x) & ((x) - 1)) == 0)
|
|
|
|
|
|
|
|
|
2019-04-04 17:45:26 +03:00
|
|
|
/* number of chars of a literal string without the ending \0 */
|
|
|
|
#define LL(x) (sizeof(x)/sizeof(char) - 1)
|
|
|
|
|
|
|
|
|
2000-03-24 20:26:08 +03:00
|
|
|
/*
|
2023-02-02 19:37:11 +03:00
|
|
|
** conversion of pointer to unsigned integer: this is for hashing only;
|
|
|
|
** there is no problem if the integer cannot hold the whole pointer
|
|
|
|
** value. (In strict ISO C this may cause undefined behavior, but no
|
|
|
|
** actual machine seems to bother.)
|
2000-03-24 20:26:08 +03:00
|
|
|
*/
|
2023-02-02 19:37:11 +03:00
|
|
|
#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \
|
|
|
|
__STDC_VERSION__ >= 199901L
|
|
|
|
#include <stdint.h>
|
|
|
|
#if defined(UINTPTR_MAX) /* even in C99 this type is optional */
|
|
|
|
#define L_P2I uintptr_t
|
|
|
|
#else /* no 'intptr'? */
|
2023-04-18 15:44:10 +03:00
|
|
|
#define L_P2I uintmax_t /* use the largest available integer */
|
2023-02-02 19:37:11 +03:00
|
|
|
#endif
|
|
|
|
#else /* C89 option */
|
|
|
|
#define L_P2I size_t
|
|
|
|
#endif
|
|
|
|
|
2024-03-22 20:06:11 +03:00
|
|
|
#define point2uint(p) cast_uint((L_P2I)(p) & UINT_MAX)
|
2000-03-24 20:26:08 +03:00
|
|
|
|
|
|
|
|
2000-03-31 20:28:45 +04:00
|
|
|
|
2014-04-11 23:56:04 +04:00
|
|
|
/* types of 'usual argument conversions' for lua_Number and lua_Integer */
|
2005-03-09 19:28:07 +03:00
|
|
|
typedef LUAI_UACNUMBER l_uacNumber;
|
2014-04-11 23:56:04 +04:00
|
|
|
typedef LUAI_UACINT l_uacInt;
|
2000-10-26 16:47:05 +04:00
|
|
|
|
|
|
|
|
2020-07-08 21:51:55 +03:00
|
|
|
/*
|
|
|
|
** Internal assertions for in-house debugging
|
|
|
|
*/
|
|
|
|
#if defined LUAI_ASSERT
|
|
|
|
#undef NDEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
#define lua_assert(c) assert(c)
|
|
|
|
#endif
|
|
|
|
|
2009-12-17 15:26:09 +03:00
|
|
|
#if defined(lua_assert)
|
2005-12-27 20:12:00 +03:00
|
|
|
#define check_exp(c,e) (lua_assert(c), (e))
|
2011-05-05 23:43:14 +04:00
|
|
|
/* to avoid problems with conditions too long */
|
2015-09-08 19:53:56 +03:00
|
|
|
#define lua_longassert(c) ((c) ? (void)0 : lua_assert(0))
|
2005-12-27 20:12:00 +03:00
|
|
|
#else
|
2011-09-13 21:39:23 +04:00
|
|
|
#define lua_assert(c) ((void)0)
|
2005-12-27 20:12:00 +03:00
|
|
|
#define check_exp(c,e) (e)
|
2011-09-13 21:39:23 +04:00
|
|
|
#define lua_longassert(c) ((void)0)
|
2009-12-17 15:26:09 +03:00
|
|
|
#endif
|
2005-12-27 20:12:00 +03:00
|
|
|
|
2002-06-13 17:45:31 +04:00
|
|
|
|
2015-01-16 16:26:55 +03:00
|
|
|
/* macro to avoid warnings about unused variables */
|
2009-12-17 15:26:09 +03:00
|
|
|
#if !defined(UNUSED)
|
2015-01-16 16:26:55 +03:00
|
|
|
#define UNUSED(x) ((void)(x))
|
2002-03-18 21:16:16 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2015-01-16 16:26:55 +03:00
|
|
|
/* type casts (a macro highlights casts in the code) */
|
2002-03-18 21:16:16 +03:00
|
|
|
#define cast(t, exp) ((t)(exp))
|
|
|
|
|
2014-03-07 20:19:00 +04:00
|
|
|
#define cast_void(i) cast(void, (i))
|
2018-01-28 18:13:26 +03:00
|
|
|
#define cast_voidp(i) cast(void *, (i))
|
2005-12-22 19:19:56 +03:00
|
|
|
#define cast_num(i) cast(lua_Number, (i))
|
|
|
|
#define cast_int(i) cast(int, (i))
|
2018-01-28 18:13:26 +03:00
|
|
|
#define cast_uint(i) cast(unsigned int, (i))
|
2024-07-27 19:32:59 +03:00
|
|
|
#define cast_ulong(i) cast(unsigned long, (i))
|
2018-01-28 18:13:26 +03:00
|
|
|
#define cast_byte(i) cast(lu_byte, (i))
|
2010-12-10 16:40:22 +03:00
|
|
|
#define cast_uchar(i) cast(unsigned char, (i))
|
2018-01-28 18:13:26 +03:00
|
|
|
#define cast_char(i) cast(char, (i))
|
|
|
|
#define cast_charp(i) cast(char *, (i))
|
|
|
|
#define cast_sizet(i) cast(size_t, (i))
|
2014-04-15 18:29:30 +04:00
|
|
|
|
|
|
|
|
2014-04-15 20:32:49 +04:00
|
|
|
/* cast a signed lua_Integer to lua_Unsigned */
|
|
|
|
#if !defined(l_castS2U)
|
|
|
|
#define l_castS2U(i) ((lua_Unsigned)(i))
|
|
|
|
#endif
|
|
|
|
|
2014-04-15 18:29:30 +04:00
|
|
|
/*
|
|
|
|
** cast a lua_Unsigned to a signed lua_Integer; this cast is
|
2014-11-02 22:33:33 +03:00
|
|
|
** not strict ISO C, but two-complement architectures should
|
2014-04-15 18:29:30 +04:00
|
|
|
** work fine.
|
|
|
|
*/
|
2014-04-15 20:32:49 +04:00
|
|
|
#if !defined(l_castU2S)
|
|
|
|
#define l_castU2S(i) ((lua_Integer)(i))
|
|
|
|
#endif
|
2005-12-22 19:19:56 +03:00
|
|
|
|
2024-07-27 19:32:59 +03:00
|
|
|
/*
|
|
|
|
** cast a size_t to lua_Integer: These casts are always valid for
|
|
|
|
** sizes of Lua objects (see MAX_SIZE)
|
|
|
|
*/
|
|
|
|
#define cast_st2S(sz) ((lua_Integer)(sz))
|
|
|
|
|
|
|
|
/* Cast a ptrdiff_t to size_t, when it is known that the minuend
|
|
|
|
** comes from the subtraend (the base)
|
|
|
|
*/
|
|
|
|
#define ct_diff2sz(df) ((size_t)(df))
|
2002-03-18 21:16:16 +03:00
|
|
|
|
2024-07-02 17:09:46 +03:00
|
|
|
/*
|
|
|
|
** Special type equivalent to '(void*)' for functions (to suppress some
|
|
|
|
** warnings when converting function pointers)
|
|
|
|
*/
|
|
|
|
typedef void (*voidf)(void);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Macro to convert pointer-to-void* to pointer-to-function. This cast
|
|
|
|
** is undefined according to ISO C, but POSIX assumes that it works.
|
|
|
|
** (The '__extension__' in gnu compilers is only to avoid warnings.)
|
|
|
|
*/
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
#define cast_func(p) (__extension__ (voidf)(p))
|
|
|
|
#else
|
|
|
|
#define cast_func(p) ((voidf)(p))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-08 00:45:19 +04:00
|
|
|
/*
|
|
|
|
** non-return type
|
|
|
|
*/
|
2018-05-30 17:25:52 +03:00
|
|
|
#if !defined(l_noret)
|
|
|
|
|
2011-10-08 00:45:19 +04:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
#define l_noret void __attribute__((noreturn))
|
2014-12-19 16:30:23 +03:00
|
|
|
#elif defined(_MSC_VER) && _MSC_VER >= 1200
|
2011-10-08 00:45:19 +04:00
|
|
|
#define l_noret void __declspec(noreturn)
|
|
|
|
#else
|
|
|
|
#define l_noret void
|
|
|
|
#endif
|
|
|
|
|
2018-05-30 17:25:52 +03:00
|
|
|
#endif
|
2011-10-08 00:45:19 +04:00
|
|
|
|
|
|
|
|
2021-09-15 17:18:41 +03:00
|
|
|
/*
|
|
|
|
** Inline functions
|
|
|
|
*/
|
|
|
|
#if !defined(LUA_USE_C89)
|
|
|
|
#define l_inline inline
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
#define l_inline __inline__
|
|
|
|
#else
|
|
|
|
#define l_inline /* empty */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define l_sinline static l_inline
|
|
|
|
|
|
|
|
|
2000-03-24 20:26:08 +03:00
|
|
|
/*
|
2024-06-20 19:43:33 +03:00
|
|
|
** An unsigned with (at least) 4 bytes
|
2000-03-24 20:26:08 +03:00
|
|
|
*/
|
2019-05-13 20:24:10 +03:00
|
|
|
#if LUAI_IS32INT
|
2018-12-27 19:32:29 +03:00
|
|
|
typedef unsigned int l_uint32;
|
2014-10-29 20:07:45 +03:00
|
|
|
#else
|
2018-12-27 19:32:29 +03:00
|
|
|
typedef unsigned long l_uint32;
|
2014-10-29 20:07:45 +03:00
|
|
|
#endif
|
2000-03-24 20:26:08 +03:00
|
|
|
|
2009-12-17 15:50:20 +03:00
|
|
|
|
2015-02-05 20:15:33 +03:00
|
|
|
/*
|
|
|
|
** The luai_num* macros define the primitive operations over numbers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* floor division (defined as 'floor(a/b)') */
|
|
|
|
#if !defined(luai_numidiv)
|
2015-02-09 18:41:56 +03:00
|
|
|
#define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b)))
|
2015-02-05 20:15:33 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* float division */
|
|
|
|
#if !defined(luai_numdiv)
|
|
|
|
#define luai_numdiv(L,a,b) ((a)/(b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2018-08-28 18:36:58 +03:00
|
|
|
** modulo: defined as 'a - floor(a/b)*b'; the direct computation
|
|
|
|
** using this definition has several problems with rounding errors,
|
|
|
|
** so it is better to use 'fmod'. 'fmod' gives the result of
|
|
|
|
** 'a - trunc(a/b)*b', and therefore must be corrected when
|
|
|
|
** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a
|
2018-09-11 14:39:12 +03:00
|
|
|
** non-integer negative result: non-integer result is equivalent to
|
|
|
|
** a non-zero remainder 'm'; negative result is equivalent to 'a' and
|
|
|
|
** 'b' with different signs, or 'm' and 'b' with different signs
|
|
|
|
** (as the result 'm' of 'fmod' has the same sign of 'a').
|
2015-02-05 20:15:33 +03:00
|
|
|
*/
|
|
|
|
#if !defined(luai_nummod)
|
|
|
|
#define luai_nummod(L,a,b,m) \
|
2018-08-28 18:36:58 +03:00
|
|
|
{ (void)L; (m) = l_mathop(fmod)(a,b); \
|
|
|
|
if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }
|
2015-02-05 20:15:33 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* exponentiation */
|
|
|
|
#if !defined(luai_numpow)
|
2020-11-13 15:59:07 +03:00
|
|
|
#define luai_numpow(L,a,b) \
|
|
|
|
((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b))
|
2015-02-05 20:15:33 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* the others are quite standard operations */
|
|
|
|
#if !defined(luai_numadd)
|
|
|
|
#define luai_numadd(L,a,b) ((a)+(b))
|
|
|
|
#define luai_numsub(L,a,b) ((a)-(b))
|
|
|
|
#define luai_nummul(L,a,b) ((a)*(b))
|
|
|
|
#define luai_numunm(L,a) (-(a))
|
|
|
|
#define luai_numeq(a,b) ((a)==(b))
|
|
|
|
#define luai_numlt(a,b) ((a)<(b))
|
|
|
|
#define luai_numle(a,b) ((a)<=(b))
|
2019-03-22 19:37:17 +03:00
|
|
|
#define luai_numgt(a,b) ((a)>(b))
|
|
|
|
#define luai_numge(a,b) ((a)>=(b))
|
2015-02-05 20:15:33 +03:00
|
|
|
#define luai_numisnan(a) (!luai_numeq((a), (a)))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2024-06-21 18:29:08 +03:00
|
|
|
/*
|
|
|
|
** {==================================================================
|
|
|
|
** "Abstraction Layer" for basic report of messages and errors
|
|
|
|
** ===================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* print a string */
|
|
|
|
#if !defined(lua_writestring)
|
|
|
|
#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
|
2000-03-24 20:26:08 +03:00
|
|
|
#endif
|
2024-06-21 18:29:08 +03:00
|
|
|
|
|
|
|
/* print a newline and flush the output */
|
|
|
|
#if !defined(lua_writeline)
|
|
|
|
#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* print an error message */
|
|
|
|
#if !defined(lua_writestringerror)
|
|
|
|
#define lua_writestringerror(s,p) \
|
|
|
|
(fprintf(stderr, (s), (p)), fflush(stderr))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* }================================================================== */
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|