lua

A copy of the Lua development repository
Log | Files | Refs | README

llimits.h (8062B)


      1 /*
      2 ** $Id: llimits.h $
      3 ** Limits, basic types, and some other 'installation-dependent' definitions
      4 ** See Copyright Notice in lua.h
      5 */
      6 
      7 #ifndef llimits_h
      8 #define llimits_h
      9 
     10 
     11 #include <limits.h>
     12 #include <stddef.h>
     13 
     14 
     15 #include "lua.h"
     16 
     17 
     18 /*
     19 ** 'l_mem' is a signed integer big enough to count the total memory
     20 ** used by Lua.  (It is signed due to the use of debt in several
     21 ** computations.)  Usually, 'ptrdiff_t' should work, but we use 'long'
     22 ** for 16-bit machines.
     23 */
     24 #if defined(LUAI_MEM)		/* { external definitions? */
     25 typedef LUAI_MEM l_mem;
     26 typedef LUAI_UMEM lu_mem;
     27 #elif LUAI_IS32INT	/* }{ */
     28 typedef ptrdiff_t l_mem;
     29 typedef size_t lu_mem;
     30 #else  /* 16-bit ints */	/* }{ */
     31 typedef long l_mem;
     32 typedef unsigned long lu_mem;
     33 #endif				/* } */
     34 
     35 #define MAX_LMEM  \
     36 	cast(l_mem, (cast(lu_mem, 1) << (sizeof(l_mem) * 8 - 1)) - 1)
     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 /* Type for thread status/error codes */
     45 typedef lu_byte TStatus;
     46 
     47 /* The C API still uses 'int' for status/error codes */
     48 #define APIstatus(st)	cast_int(st)
     49 
     50 /* maximum value for size_t */
     51 #define MAX_SIZET	((size_t)(~(size_t)0))
     52 
     53 /*
     54 ** Maximum size for strings and userdata visible for Lua; should be
     55 ** representable as a lua_Integer and as a size_t.
     56 */
     57 #define MAX_SIZE	(sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
     58 			  : cast_sizet(LUA_MAXINTEGER))
     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)	cast_int(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)	cast_uint((L_P2I)(p) & UINT_MAX)
     96 
     97 
     98 
     99 /* types of 'usual argument conversions' for lua_Number and lua_Integer */
    100 typedef LUAI_UACNUMBER l_uacNumber;
    101 typedef LUAI_UACINT l_uacInt;
    102 
    103 
    104 /*
    105 ** Internal assertions for in-house debugging
    106 */
    107 #if defined LUAI_ASSERT
    108 #undef NDEBUG
    109 #include <assert.h>
    110 #define lua_assert(c)           assert(c)
    111 #define assert_code(c)		c
    112 #endif
    113 
    114 #if defined(lua_assert)
    115 #else
    116 #define lua_assert(c)		((void)0)
    117 #define assert_code(c)		((void)0)
    118 #endif
    119 
    120 #define check_exp(c,e)		(lua_assert(c), (e))
    121 /* to avoid problems with conditions too long */
    122 #define lua_longassert(c)	assert_code((c) ? (void)0 : lua_assert(0))
    123 
    124 
    125 /* macro to avoid warnings about unused variables */
    126 #if !defined(UNUSED)
    127 #define UNUSED(x)	((void)(x))
    128 #endif
    129 
    130 
    131 /* type casts (a macro highlights casts in the code) */
    132 #define cast(t, exp)	((t)(exp))
    133 
    134 #define cast_void(i)	cast(void, (i))
    135 #define cast_voidp(i)	cast(void *, (i))
    136 #define cast_num(i)	cast(lua_Number, (i))
    137 #define cast_int(i)	cast(int, (i))
    138 #define cast_uint(i)	cast(unsigned int, (i))
    139 #define cast_ulong(i)	cast(unsigned long, (i))
    140 #define cast_byte(i)	cast(lu_byte, (i))
    141 #define cast_uchar(i)	cast(unsigned char, (i))
    142 #define cast_char(i)	cast(char, (i))
    143 #define cast_charp(i)	cast(char *, (i))
    144 #define cast_sizet(i)	cast(size_t, (i))
    145 
    146 
    147 /* cast a signed lua_Integer to lua_Unsigned */
    148 #if !defined(l_castS2U)
    149 #define l_castS2U(i)	((lua_Unsigned)(i))
    150 #endif
    151 
    152 /*
    153 ** cast a lua_Unsigned to a signed lua_Integer; this cast is
    154 ** not strict ISO C, but two-complement architectures should
    155 ** work fine.
    156 */
    157 #if !defined(l_castU2S)
    158 #define l_castU2S(i)	((lua_Integer)(i))
    159 #endif
    160 
    161 /*
    162 ** cast a size_t to lua_Integer: These casts are always valid for
    163 ** sizes of Lua objects (see MAX_SIZE)
    164 */
    165 #define cast_st2S(sz)	((lua_Integer)(sz))
    166 
    167 /* Cast a ptrdiff_t to size_t, when it is known that the minuend
    168 ** comes from the subtrahend (the base)
    169 */
    170 #define ct_diff2sz(df)	((size_t)(df))
    171 
    172 /* ptrdiff_t to lua_Integer */
    173 #define ct_diff2S(df)	cast_st2S(ct_diff2sz(df))
    174 
    175 /*
    176 ** Special type equivalent to '(void*)' for functions (to suppress some
    177 ** warnings when converting function pointers)
    178 */
    179 typedef void (*voidf)(void);
    180 
    181 /*
    182 ** Macro to convert pointer-to-void* to pointer-to-function. This cast
    183 ** is undefined according to ISO C, but POSIX assumes that it works.
    184 ** (The '__extension__' in gnu compilers is only to avoid warnings.)
    185 */
    186 #if defined(__GNUC__)
    187 #define cast_func(p) (__extension__ (voidf)(p))
    188 #else
    189 #define cast_func(p) ((voidf)(p))
    190 #endif
    191 
    192 
    193 
    194 /*
    195 ** non-return type
    196 */
    197 #if !defined(l_noret)
    198 
    199 #if defined(__GNUC__)
    200 #define l_noret		void __attribute__((noreturn))
    201 #elif defined(_MSC_VER) && _MSC_VER >= 1200
    202 #define l_noret		void __declspec(noreturn)
    203 #else
    204 #define l_noret		void
    205 #endif
    206 
    207 #endif
    208 
    209 
    210 /*
    211 ** Inline functions
    212 */
    213 #if !defined(LUA_USE_C89)
    214 #define l_inline	inline
    215 #elif defined(__GNUC__)
    216 #define l_inline	__inline__
    217 #else
    218 #define l_inline	/* empty */
    219 #endif
    220 
    221 #define l_sinline	static l_inline
    222 
    223 
    224 /*
    225 ** An unsigned with (at least) 4 bytes
    226 */
    227 #if LUAI_IS32INT
    228 typedef unsigned int l_uint32;
    229 #else
    230 typedef unsigned long l_uint32;
    231 #endif
    232 
    233 
    234 /*
    235 ** The luai_num* macros define the primitive operations over numbers.
    236 */
    237 
    238 /* floor division (defined as 'floor(a/b)') */
    239 #if !defined(luai_numidiv)
    240 #define luai_numidiv(L,a,b)     ((void)L, l_floor(luai_numdiv(L,a,b)))
    241 #endif
    242 
    243 /* float division */
    244 #if !defined(luai_numdiv)
    245 #define luai_numdiv(L,a,b)      ((a)/(b))
    246 #endif
    247 
    248 /*
    249 ** modulo: defined as 'a - floor(a/b)*b'; the direct computation
    250 ** using this definition has several problems with rounding errors,
    251 ** so it is better to use 'fmod'. 'fmod' gives the result of
    252 ** 'a - trunc(a/b)*b', and therefore must be corrected when
    253 ** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a
    254 ** non-integer negative result: non-integer result is equivalent to
    255 ** a non-zero remainder 'm'; negative result is equivalent to 'a' and
    256 ** 'b' with different signs, or 'm' and 'b' with different signs
    257 ** (as the result 'm' of 'fmod' has the same sign of 'a').
    258 */
    259 #if !defined(luai_nummod)
    260 #define luai_nummod(L,a,b,m)  \
    261   { (void)L; (m) = l_mathop(fmod)(a,b); \
    262     if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }
    263 #endif
    264 
    265 /* exponentiation */
    266 #if !defined(luai_numpow)
    267 #define luai_numpow(L,a,b)  \
    268   ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b))
    269 #endif
    270 
    271 /* the others are quite standard operations */
    272 #if !defined(luai_numadd)
    273 #define luai_numadd(L,a,b)      ((a)+(b))
    274 #define luai_numsub(L,a,b)      ((a)-(b))
    275 #define luai_nummul(L,a,b)      ((a)*(b))
    276 #define luai_numunm(L,a)        (-(a))
    277 #define luai_numeq(a,b)         ((a)==(b))
    278 #define luai_numlt(a,b)         ((a)<(b))
    279 #define luai_numle(a,b)         ((a)<=(b))
    280 #define luai_numgt(a,b)         ((a)>(b))
    281 #define luai_numge(a,b)         ((a)>=(b))
    282 #define luai_numisnan(a)        (!luai_numeq((a), (a)))
    283 #endif
    284 
    285 
    286 /*
    287 ** {==================================================================
    288 ** "Abstraction Layer" for basic report of messages and errors
    289 ** ===================================================================
    290 */
    291 
    292 /* print a string */
    293 #if !defined(lua_writestring)
    294 #define lua_writestring(s,l)   fwrite((s), sizeof(char), (l), stdout)
    295 #endif
    296 
    297 /* print a newline and flush the output */
    298 #if !defined(lua_writeline)
    299 #define lua_writeline()        (lua_writestring("\n", 1), fflush(stdout))
    300 #endif
    301 
    302 /* print an error message */
    303 #if !defined(lua_writestringerror)
    304 #define lua_writestringerror(s,p) \
    305         (fprintf(stderr, (s), (p)), fflush(stderr))
    306 #endif
    307 
    308 /* }================================================================== */
    309 
    310 #endif
    311