lua5.4
lmem.h
浏览该文件的文档.
1 /*
2 ** $Id: lmem.h $
3 ** Interface to Memory Manager
4 ** See Copyright Notice in lua.h
5 */
6 
7 #ifndef lmem_h
8 #define lmem_h
9 
10 
11 #include <stddef.h>
12 
13 #include "llimits.h"
14 #include "lua.h"
15 
16 
17 #define luaM_error(L) luaD_throw(L, LUA_ERRMEM)
18 
19 
20 /*
21 ** This macro tests whether it is safe to multiply 'n' by the size of
22 ** type 't' without overflows. Because 'e' is always constant, it avoids
23 ** the runtime division MAX_SIZET/(e).
24 ** (The macro is somewhat complex to avoid warnings: The 'sizeof'
25 ** comparison avoids a runtime comparison when overflow cannot occur.
26 ** The compiler should be able to optimize the real test by itself, but
27 ** when it does it, it may give a warning about "comparison is always
28 ** false due to limited range of data type"; the +1 tricks the compiler,
29 ** avoiding this warning but also this optimization.)
30 */
31 #define luaM_testsize(n,e) \
32  (sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e))
33 
34 #define luaM_checksize(L,n,e) \
35  (luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0))
36 
37 
38 /*
39 ** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that
40 ** the result is not larger than 'n' and cannot overflow a 'size_t'
41 ** when multiplied by the size of type 't'. (Assumes that 'n' is an
42 ** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.)
43 */
44 #define luaM_limitN(n,t) \
45  ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \
46  cast_uint((MAX_SIZET/sizeof(t))))
47 
48 
49 /*
50 ** Arrays of chars do not need any test
51 */
52 #define luaM_reallocvchar(L,b,on,n) \
53  cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char)))
54 
55 #define luaM_freemem(L, b, s) luaM_free_(L, (b), (s))
56 #define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b)))
57 #define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b)))
58 
59 #define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0))
60 #define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0))
61 #define luaM_newvectorchecked(L,n,t) \
62  (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t))
63 
64 #define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag)
65 
66 #define luaM_growvector(L,v,nelems,size,t,limit,e) \
67  ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \
68  luaM_limitN(limit,t),e)))
69 
70 #define luaM_reallocvector(L, v,oldn,n,t) \
71  (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \
72  cast_sizet(n) * sizeof(t))))
73 
74 #define luaM_shrinkvector(L,v,size,fs,t) \
75  ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t))))
76 
78 
79 /* not to be called directly */
80 LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
81  size_t size);
82 LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize,
83  size_t size);
84 LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize);
85 LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems,
86  int *size, int size_elem, int limit,
87  const char *what);
88 LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem,
89  int final_n, int size_elem);
90 LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag);
91 
92 #endif
93 
randfuncs
static const luaL_Reg randfuncs[]
Definition: lmathlib.c:631
lua_assert
#define lua_assert(c)
Definition: lauxlib.h:169
math_max
static int math_max(lua_State *L)
Definition: lmathlib.c:219
lua_Unsigned
LUA_UNSIGNED lua_Unsigned
Definition: lua.h:97
PI
#define PI
Definition: lmathlib.c:26
I2UInt
static lua_Unsigned I2UInt(Rand64 x)
Definition: lmathlib.c:501
LUAI_FUNC
#define LUAI_FUNC
Definition: luaconf.h:307
l_noret
#define l_noret
Definition: llimits.h:178
luaM_shrinkvector_
LUAI_FUNC void * luaM_shrinkvector_(lua_State *L, void *block, int *nelem, int final_n, int size_elem)
Definition: lmem.c:110
math_asin
static int math_asin(lua_State *L)
Definition: lmathlib.c:55
lstate.h
math_ult
static int math_ult(lua_State *L)
Definition: lmathlib.c:162
luaM_malloc_
LUAI_FUNC void * luaM_malloc_(lua_State *L, size_t size, int tag)
Definition: lmem.c:188
math_sqrt
static int math_sqrt(lua_State *L)
Definition: lmathlib.c:156
lua_pushstring
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.c:514
scaleFIG
#define scaleFIG
Definition: lmathlib.c:460
Iadd
static Rand64 Iadd(Rand64 i1, Rand64 i2)
Definition: lmathlib.c:401
math_log
static int math_log(lua_State *L)
Definition: lmathlib.c:169
times9
static Rand64 times9(Rand64 i)
Definition: lmathlib.c:414
project
static lua_Unsigned project(lua_Unsigned ran, lua_Unsigned n, RanState *state)
Definition: lmathlib.c:531
unlikely
#define unlikely(x)
Definition: llimits.h:162
lua_pushinteger
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.c:489
luaM_free_
void luaM_free_(lua_State *L, void *block, size_t osize)
Definition: lmem.c:132
luaG_runerror
l_noret luaG_runerror(lua_State *L, const char *fmt,...)
Definition: ldebug.c:767
Rand64
Definition: lmathlib.c:360
lua_pop
#define lua_pop(L, n)
Definition: lua.h:364
luaM_realloc_
void * luaM_realloc_(lua_State *L, void *block, size_t osize, size_t nsize)
Definition: lmem.c:162
math_sin
static int math_sin(lua_State *L)
Definition: lmathlib.c:40
rotl
static Rand64 rotl(Rand64 i, int n)
Definition: lmathlib.c:419
lua_isnoneornil
#define lua_isnoneornil(L, n)
Definition: lua.h:379
math_randomseed
static int math_randomseed(lua_State *L)
Definition: lmathlib.c:617
luaM_saferealloc_
LUAI_FUNC void * luaM_saferealloc_(lua_State *L, void *block, size_t oldsize, size_t size)
Definition: lmem.c:179
lua_Number
LUA_NUMBER lua_Number
Definition: lua.h:90
llimits.h
luaM_growaux_
void * luaM_growaux_(lua_State *L, void *block, int nelems, int *psize, int size_elems, int limit, const char *what)
Definition: lmem.c:79
luaM_error
#define luaM_error(L)
Definition: lmem.h:17
math_deg
static int math_deg(lua_State *L)
Definition: lmathlib.c:194
math_atan
static int math_atan(lua_State *L)
Definition: lmathlib.c:65
lua_type
LUA_API int lua_type(lua_State *L, int idx)
Definition: lapi.c:260
luaopen_math
LUAMOD_API int luaopen_math(lua_State *L)
Definition: lmathlib.c:750
lua_pushnumber
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
Definition: lapi.c:481
RanState
Definition: lmathlib.c:516
lua_isinteger
LUA_API int lua_isinteger(lua_State *L, int idx)
Definition: lapi.c:279
RanState::s
Rand64 s[4]
Definition: lmathlib.c:517
setseed
static void setseed(lua_State *L, Rand64 *state, lua_Unsigned n1, lua_Unsigned n2)
Definition: lmathlib.c:591
luaM_growaux_
LUAI_FUNC void * luaM_growaux_(lua_State *L, void *block, int nelems, int *size, int size_elem, int limit, const char *what)
Definition: lmem.c:79
luaM_saferealloc_
void * luaM_saferealloc_(lua_State *L, void *block, size_t osize, size_t nsize)
Definition: lmem.c:179
lua.h
mathlib
static const luaL_Reg mathlib[]
Definition: lmathlib.c:704
randseed
static void randseed(lua_State *L, RanState *state)
Definition: lmathlib.c:610
math_toint
static int math_toint(lua_State *L)
Definition: lmathlib.c:73
Ixor
static void Ixor(Rand64 *i1, Rand64 i2)
Definition: lmathlib.c:395
ttisnil
#define ttisnil(v)
Definition: lobject.h:171
global_State::frealloc
lua_Alloc frealloc
Definition: lstate.h:220
lua_tointeger
#define lua_tointeger(L, i)
Definition: lua.h:362
lua_setfield
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
Definition: lapi.c:847
global_State::ud
void * ud
Definition: lstate.h:221
math_tan
static int math_tan(lua_State *L)
Definition: lmathlib.c:50
G
#define G(L)
Definition: lstate.h:298
luaL_Reg
Definition: lauxlib.h:37
lua_numbertointeger
#define lua_numbertointeger(n, p)
Definition: luaconf.h:413
lprefix.h
Rand64
struct Rand64 Rand64
ldebug.h
LUA_TNUMBER
#define LUA_TNUMBER
Definition: lua.h:68
luaM_toobig
LUAI_FUNC l_noret luaM_toobig(lua_State *L)
Definition: lmem.c:124
Ishl
static Rand64 Ishl(Rand64 i, int n)
Definition: lmathlib.c:389
firsttry
#define firsttry(g, block, os, ns)
Definition: lmem.c:38
luaL_checkany
LUALIB_API void luaL_checkany(lua_State *L, int arg)
Definition: lauxlib.c:396
math_abs
static int math_abs(lua_State *L)
Definition: lmathlib.c:29
LUA_OPLT
#define LUA_OPLT
Definition: lua.h:223
I2d
static lua_Number I2d(Rand64 x)
Definition: lmathlib.c:466
lua_touserdata
LUA_API void * lua_touserdata(lua_State *L, int idx)
Definition: lapi.c:432
math_cos
static int math_cos(lua_State *L)
Definition: lmathlib.c:45
lu_int32
unsigned long lu_int32
Definition: lmathlib.c:353
math_exp
static int math_exp(lua_State *L)
Definition: lmathlib.c:189
global_State::GCdebt
l_mem GCdebt
Definition: lstate.h:223
luaL_argcheck
#define luaL_argcheck(L, cond, arg, extramsg)
Definition: lauxlib.h:132
lobject.h
global_State
Definition: lstate.h:219
lua_gettop
LUA_API int lua_gettop(lua_State *L)
Definition: lapi.c:168
lua_State
Definition: lstate.h:273
lua_Integer
LUA_INTEGER lua_Integer
Definition: lua.h:94
math_floor
static int math_floor(lua_State *L)
Definition: lmathlib.c:95
trim32
#define trim32(x)
Definition: lmathlib.c:373
lua_settop
LUA_API void lua_settop(lua_State *L, int idx)
Definition: lapi.c:173
math_rad
static int math_rad(lua_State *L)
Definition: lmathlib.c:199
lauxlib.h
lualib.h
lua_pushboolean
LUA_API void lua_pushboolean(lua_State *L, int b)
Definition: lapi.c:581
lua_pushvalue
LUA_API void lua_pushvalue(lua_State *L, int idx)
Definition: lapi.c:246
FIGS
#define FIGS
Definition: lmathlib.c:252
cast_sizet
#define cast_sizet(i)
Definition: llimits.h:134
luaM_shrinkvector_
void * luaM_shrinkvector_(lua_State *L, void *block, int *size, int final_n, int size_elem)
Definition: lmem.c:110
block
static void block(LexState *ls)
Definition: lparser.c:1293
LUAMOD_API
#define LUAMOD_API
Definition: luaconf.h:286
rotl1
static Rand64 rotl1(Rand64 i, int n)
Definition: lmathlib.c:426
math_ceil
static int math_ceil(lua_State *L)
Definition: lmathlib.c:106
luaL_setfuncs
LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
Definition: lauxlib.c:916
luaL_optinteger
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int arg, lua_Integer def)
Definition: lauxlib.c:452
math_random
static int math_random(lua_State *L)
Definition: lmathlib.c:556
math_type
static int math_type(lua_State *L)
Definition: lmathlib.c:233
setrandfunc
static void setrandfunc(lua_State *L)
Definition: lmathlib.c:641
times5
static Rand64 times5(Rand64 i)
Definition: lmathlib.c:409
packI
static Rand64 packI(lu_int32 h, lu_int32 l)
Definition: lmathlib.c:381
pushnumint
static void pushnumint(lua_State *L, lua_Number d)
Definition: lmathlib.c:86
l_mathop
#define l_mathop(op)
Definition: luaconf.h:463
luaL_newlib
#define luaL_newlib(L, l)
Definition: lauxlib.h:129
luaL_checknumber
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int arg)
Definition: lauxlib.c:420
luaL_error
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
Definition: lauxlib.c:234
math_min
static int math_min(lua_State *L)
Definition: lmathlib.c:205
luaL_optnumber
LUALIB_API lua_Number luaL_optnumber(lua_State *L, int arg, lua_Number def)
Definition: lauxlib.c:429
lua_isnone
#define lua_isnone(L, n)
Definition: lua.h:378
luaM_malloc_
void * luaM_malloc_(lua_State *L, size_t size, int tag)
Definition: lmem.c:188
lua_newuserdatauv
LUA_API void * lua_newuserdatauv(lua_State *L, size_t size, int nuvalue)
Definition: lapi.c:1318
luaL_checkinteger
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int arg)
Definition: lauxlib.c:442
lua_tointegerx
LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *pisnum)
Definition: lapi.c:365
tryagain
static void * tryagain(lua_State *L, void *block, size_t osize, size_t nsize)
Definition: lmem.c:146
MINSIZEARRAY
#define MINSIZEARRAY
Definition: lmem.c:76
global_State::nilvalue
TValue nilvalue
Definition: lstate.h:228
lmem.h
lgc.h
luaC_fullgc
void luaC_fullgc(lua_State *L, int isemergency)
Definition: lgc.c:1703
Int2I
static Rand64 Int2I(lua_Unsigned n)
Definition: lmathlib.c:506
math_fmod
static int math_fmod(lua_State *L)
Definition: lmathlib.c:117
luaM_free_
LUAI_FUNC void luaM_free_(lua_State *L, void *block, size_t osize)
Definition: lmem.c:132
lua_upvalueindex
#define lua_upvalueindex(i)
Definition: lua.h:45
math_acos
static int math_acos(lua_State *L)
Definition: lmathlib.c:60
lua_compare
LUA_API int lua_compare(lua_State *L, int index1, int index2, int op)
Definition: lapi.c:327
nextrand
static Rand64 nextrand(Rand64 *state)
Definition: lmathlib.c:436
Rand64::h
lu_int32 h
Definition: lmathlib.c:361
luaM_realloc_
LUAI_FUNC void * luaM_realloc_(lua_State *L, void *block, size_t oldsize, size_t size)
Definition: lmem.c:162
math_modf
static int math_modf(lua_State *L)
Definition: lmathlib.c:139
luaL_pushfail
#define luaL_pushfail(L)
Definition: lauxlib.h:157
luaM_toobig
l_noret luaM_toobig(lua_State *L)
Definition: lmem.c:124
ldo.h
Rand64::l
lu_int32 l
Definition: lmathlib.c:362
Dyndata::n
int n
Definition: lparser.h:131