Home | History | Annotate | Line # | Download | only in src
      1 /*	$NetBSD: llimits.h,v 1.12 2023/06/08 21:12:08 nikita Exp $	*/
      2 
      3 /*
      4 ** Id: llimits.h
      5 ** Limits, basic types, and some other 'installation-dependent' definitions
      6 ** See Copyright Notice in lua.h
      7 */
      8 
      9 #ifndef llimits_h
     10 #define llimits_h
     11 
     12 
     13 #ifndef _KERNEL
     14 #include <limits.h>
     15 #include <stddef.h>
     16 #endif /* _KERNEL */
     17 
     18 
     19 #include "lua.h"
     20 
     21 
     22 /*
     23 ** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count
     24 ** the total memory used by Lua (in bytes). Usually, 'size_t' and
     25 ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
     26 */
     27 #if defined(LUAI_MEM)		/* { external definitions? */
     28 typedef LUAI_UMEM lu_mem;
     29 typedef LUAI_MEM l_mem;
     30 #elif LUAI_IS32INT	/* }{ */
     31 typedef size_t lu_mem;
     32 typedef ptrdiff_t l_mem;
     33 #else  /* 16-bit ints */	/* }{ */
     34 typedef unsigned long lu_mem;
     35 typedef long l_mem;
     36 #endif				/* } */
     37 
     38 
     39 /* chars used as small naturals (so that 'char' is reserved for characters) */
     40 typedef unsigned char lu_byte;
     41 typedef signed char ls_byte;
     42 
     43 
     44 /* maximum value for size_t */
     45 #define MAX_SIZET	((size_t)(~(size_t)0))
     46 
     47 /* maximum size visible for Lua (must be representable in a lua_Integer) */
     48 #define MAX_SIZE	(sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
     49                           : (size_t)(LUA_MAXINTEGER))
     50 
     51 
     52 #define MAX_LUMEM	((lu_mem)(~(lu_mem)0))
     53 
     54 #define MAX_LMEM	((l_mem)(MAX_LUMEM >> 1))
     55 
     56 
     57 #define MAX_INT		INT_MAX  /* maximum value of an int */
     58 
     59 
     60 /*
     61 ** floor of the log2 of the maximum signed value for integral type 't'.
     62 ** (That is, maximum 'n' such that '2^n' fits in the given signed type.)
     63 */
     64 #define log2maxs(t)	(sizeof(t) * 8 - 2)
     65 
     66 
     67 /*
     68 ** test whether an unsigned value is a power of 2 (or zero)
     69 */
     70 #define ispow2(x)	(((x) & ((x) - 1)) == 0)
     71 
     72 
     73 /* number of chars of a literal string without the ending \0 */
     74 #define LL(x)   (sizeof(x)/sizeof(char) - 1)
     75 
     76 
     77 /*
     78 ** conversion of pointer to unsigned integer: this is for hashing only;
     79 ** there is no problem if the integer cannot hold the whole pointer
     80 ** value. (In strict ISO C this may cause undefined behavior, but no
     81 ** actual machine seems to bother.)
     82 */
     83 #if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \
     84     __STDC_VERSION__ >= 199901L
     85 #include <stdint.h>
     86 #if defined(UINTPTR_MAX)  /* even in C99 this type is optional */
     87 #define L_P2I	uintptr_t
     88 #else  /* no 'intptr'? */
     89 #define L_P2I	uintmax_t  /* use the largest available integer */
     90 #endif
     91 #else  /* C89 option */
     92 #define L_P2I	size_t
     93 #endif
     94 
     95 #define point2uint(p)	((unsigned int)((L_P2I)(p) & UINT_MAX))
     96 
     97 
     98 
     99 /* types of 'usual argument conversions' for lua_Number and lua_Integer */
    100 #ifndef _KERNEL
    101 typedef LUAI_UACNUMBER l_uacNumber;
    102 #endif /* _KERNEL */
    103 typedef LUAI_UACINT l_uacInt;
    104 
    105 
    106 /*
    107 ** Internal assertions for in-house debugging
    108 */
    109 #if defined LUAI_ASSERT
    110 #undef NDEBUG
    111 #include <assert.h>
    112 #define lua_assert(c)           assert(c)
    113 #endif
    114 
    115 #if defined(lua_assert)
    116 #define check_exp(c,e)		(lua_assert(c), (e))
    117 /* to avoid problems with conditions too long */
    118 #define lua_longassert(c)	((c) ? (void)0 : lua_assert(0))
    119 #else
    120 #define lua_assert(c)		((void)0)
    121 #define check_exp(c,e)		(e)
    122 #define lua_longassert(c)	((void)0)
    123 #endif
    124 
    125 /*
    126 ** assertion for checking API calls
    127 */
    128 #if !defined(luai_apicheck)
    129 #define luai_apicheck(l,e)	((void)l, lua_assert(e))
    130 #endif
    131 
    132 #define api_check(l,e,msg)	luai_apicheck(l,(e) && msg)
    133 
    134 
    135 /* macro to avoid warnings about unused variables */
    136 #if !defined(UNUSED)
    137 #define UNUSED(x)	((void)(x))
    138 #endif
    139 
    140 
    141 /* type casts (a macro highlights casts in the code) */
    142 #define cast(t, exp)	((t)(exp))
    143 
    144 #define cast_void(i)	cast(void, (i))
    145 #define cast_voidp(i)	cast(void *, (i))
    146 #define cast_num(i)	cast(lua_Number, (i))
    147 #define cast_int(i)	cast(int, (i))
    148 #define cast_uint(i)	cast(unsigned int, (i))
    149 #define cast_byte(i)	cast(lu_byte, (i))
    150 #define cast_uchar(i)	cast(unsigned char, (i))
    151 #define cast_char(i)	cast(char, (i))
    152 #define cast_charp(i)	cast(char *, (i))
    153 #define cast_sizet(i)	cast(size_t, (i))
    154 
    155 
    156 /* cast a signed lua_Integer to lua_Unsigned */
    157 #if !defined(l_castS2U)
    158 #define l_castS2U(i)	((lua_Unsigned)(i))
    159 #endif
    160 
    161 /*
    162 ** cast a lua_Unsigned to a signed lua_Integer; this cast is
    163 ** not strict ISO C, but two-complement architectures should
    164 ** work fine.
    165 */
    166 #if !defined(l_castU2S)
    167 #define l_castU2S(i)	((lua_Integer)(i))
    168 #endif
    169 
    170 
    171 /*
    172 ** non-return type
    173 */
    174 #if !defined(l_noret)
    175 
    176 #if defined(__GNUC__)
    177 #define l_noret		void __attribute__((noreturn))
    178 #elif defined(_MSC_VER) && _MSC_VER >= 1200
    179 #define l_noret		void __declspec(noreturn)
    180 #else
    181 #define l_noret		void
    182 #endif
    183 
    184 #endif
    185 
    186 
    187 /*
    188 ** Inline functions
    189 */
    190 #if !defined(LUA_USE_C89)
    191 #define l_inline	inline
    192 #elif defined(__GNUC__)
    193 #define l_inline	__inline__
    194 #else
    195 #define l_inline	/* empty */
    196 #endif
    197 
    198 #define l_sinline	static l_inline
    199 
    200 
    201 /*
    202 ** type for virtual-machine instructions;
    203 ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
    204 */
    205 #if LUAI_IS32INT
    206 typedef unsigned int l_uint32;
    207 #else
    208 typedef unsigned long l_uint32;
    209 #endif
    210 
    211 typedef l_uint32 Instruction;
    212 
    213 
    214 
    215 /*
    216 ** Maximum length for short strings, that is, strings that are
    217 ** internalized. (Cannot be smaller than reserved words or tags for
    218 ** metamethods, as these strings must be internalized;
    219 ** #("function") = 8, #("__newindex") = 10.)
    220 */
    221 #if !defined(LUAI_MAXSHORTLEN)
    222 #define LUAI_MAXSHORTLEN	40
    223 #endif
    224 
    225 
    226 /*
    227 ** Initial size for the string table (must be power of 2).
    228 ** The Lua core alone registers ~50 strings (reserved words +
    229 ** metaevent keys + a few others). Libraries would typically add
    230 ** a few dozens more.
    231 */
    232 #if !defined(MINSTRTABSIZE)
    233 #define MINSTRTABSIZE	128
    234 #endif
    235 
    236 
    237 /*
    238 ** Size of cache for strings in the API. 'N' is the number of
    239 ** sets (better be a prime) and "M" is the size of each set (M == 1
    240 ** makes a direct cache.)
    241 */
    242 #if !defined(STRCACHE_N)
    243 #define STRCACHE_N		53
    244 #define STRCACHE_M		2
    245 #endif
    246 
    247 
    248 /* minimum size for string buffer */
    249 #if !defined(LUA_MINBUFFER)
    250 #define LUA_MINBUFFER	32
    251 #endif
    252 
    253 
    254 /*
    255 ** Maximum depth for nested C calls, syntactical nested non-terminals,
    256 ** and other features implemented through recursion in C. (Value must
    257 ** fit in a 16-bit unsigned integer. It must also be compatible with
    258 ** the size of the C stack.)
    259 */
    260 #if !defined(LUAI_MAXCCALLS)
    261 #define LUAI_MAXCCALLS		200
    262 #endif
    263 
    264 
    265 /*
    266 ** macros that are executed whenever program enters the Lua core
    267 ** ('lua_lock') and leaves the core ('lua_unlock')
    268 */
    269 #if !defined(lua_lock)
    270 #define lua_lock(L)	((void) 0)
    271 #define lua_unlock(L)	((void) 0)
    272 #endif
    273 
    274 /*
    275 ** macro executed during Lua functions at points where the
    276 ** function can yield.
    277 */
    278 #if !defined(luai_threadyield)
    279 #define luai_threadyield(L)	{lua_unlock(L); lua_lock(L);}
    280 #endif
    281 
    282 
    283 /*
    284 ** these macros allow user-specific actions when a thread is
    285 ** created/deleted/resumed/yielded.
    286 */
    287 #if !defined(luai_userstateopen)
    288 #define luai_userstateopen(L)		((void)L)
    289 #endif
    290 
    291 #if !defined(luai_userstateclose)
    292 #define luai_userstateclose(L)		((void)L)
    293 #endif
    294 
    295 #if !defined(luai_userstatethread)
    296 #define luai_userstatethread(L,L1)	((void)L)
    297 #endif
    298 
    299 #if !defined(luai_userstatefree)
    300 #define luai_userstatefree(L,L1)	((void)L)
    301 #endif
    302 
    303 #if !defined(luai_userstateresume)
    304 #define luai_userstateresume(L,n)	((void)L)
    305 #endif
    306 
    307 #if !defined(luai_userstateyield)
    308 #define luai_userstateyield(L,n)	((void)L)
    309 #endif
    310 
    311 
    312 
    313 /*
    314 ** The luai_num* macros define the primitive operations over numbers.
    315 */
    316 
    317 /* floor division (defined as 'floor(a/b)') */
    318 #if !defined(luai_numidiv)
    319 #define luai_numidiv(L,a,b)     ((void)L, l_floor(luai_numdiv(L,a,b)))
    320 #endif
    321 
    322 /* float division */
    323 #if !defined(luai_numdiv)
    324 #define luai_numdiv(L,a,b)      ((a)/(b))
    325 #endif
    326 
    327 /*
    328 ** modulo: defined as 'a - floor(a/b)*b'; the direct computation
    329 ** using this definition has several problems with rounding errors,
    330 ** so it is better to use 'fmod'. 'fmod' gives the result of
    331 ** 'a - trunc(a/b)*b', and therefore must be corrected when
    332 ** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a
    333 ** non-integer negative result: non-integer result is equivalent to
    334 ** a non-zero remainder 'm'; negative result is equivalent to 'a' and
    335 ** 'b' with different signs, or 'm' and 'b' with different signs
    336 ** (as the result 'm' of 'fmod' has the same sign of 'a').
    337 */
    338 #if !defined(luai_nummod)
    339 #define luai_nummod(L,a,b,m)  \
    340   { (void)L; (m) = l_mathop(fmod)(a,b); \
    341     if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }
    342 #endif
    343 
    344 /* exponentiation */
    345 #if !defined(luai_numpow)
    346 #define luai_numpow(L,a,b)  \
    347   ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b))
    348 #endif
    349 
    350 /* the others are quite standard operations */
    351 #if !defined(luai_numadd)
    352 #define luai_numadd(L,a,b)      ((a)+(b))
    353 #define luai_numsub(L,a,b)      ((a)-(b))
    354 #define luai_nummul(L,a,b)      ((a)*(b))
    355 #define luai_numunm(L,a)        (-(a))
    356 #define luai_numeq(a,b)         ((a)==(b))
    357 #define luai_numlt(a,b)         ((a)<(b))
    358 #define luai_numle(a,b)         ((a)<=(b))
    359 #define luai_numgt(a,b)         ((a)>(b))
    360 #define luai_numge(a,b)         ((a)>=(b))
    361 #define luai_numisnan(a)        (!luai_numeq((a), (a)))
    362 #endif
    363 
    364 
    365 
    366 
    367 
    368 /*
    369 ** macro to control inclusion of some hard tests on stack reallocation
    370 */
    371 #if !defined(HARDSTACKTESTS)
    372 #define condmovestack(L,pre,pos)	((void)0)
    373 #else
    374 /* realloc stack keeping its size */
    375 #define condmovestack(L,pre,pos)  \
    376   { int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; }
    377 #endif
    378 
    379 #if !defined(HARDMEMTESTS)
    380 #define condchangemem(L,pre,pos)	((void)0)
    381 #else
    382 #define condchangemem(L,pre,pos)  \
    383 	{ if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } }
    384 #endif
    385 
    386 #endif
    387