File indexing completed on 2026-11-07 10:37:56
0001
0002
0003
0004
0005
0006
0007 #ifndef lmem_h
0008 #define lmem_h
0009
0010
0011 #include <stddef.h>
0012
0013 #include "llimits.h"
0014 #include "lua.h"
0015
0016
0017 #define luaM_error(L) luaD_throw(L, LUA_ERRMEM)
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 #define luaM_testsize(n,e) \
0032 (sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e))
0033
0034 #define luaM_checksize(L,n,e) \
0035 (luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0))
0036
0037
0038
0039
0040
0041
0042
0043
0044 #define luaM_limitN(n,t) \
0045 ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \
0046 cast_uint((MAX_SIZET/sizeof(t))))
0047
0048
0049
0050
0051
0052 #define luaM_reallocvchar(L,b,on,n) \
0053 cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char)))
0054
0055 #define luaM_freemem(L, b, s) luaM_free_(L, (b), (s))
0056 #define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b)))
0057 #define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b)))
0058
0059 #define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0))
0060 #define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0))
0061 #define luaM_newvectorchecked(L,n,t) \
0062 (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t))
0063
0064 #define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag)
0065
0066 #define luaM_growvector(L,v,nelems,size,t,limit,e) \
0067 ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \
0068 luaM_limitN(limit,t),e)))
0069
0070 #define luaM_reallocvector(L, v,oldn,n,t) \
0071 (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \
0072 cast_sizet(n) * sizeof(t))))
0073
0074 #define luaM_shrinkvector(L,v,size,fs,t) \
0075 ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t))))
0076
0077 LUAI_FUNC l_noret luaM_toobig (lua_State *L);
0078
0079
0080 LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
0081 size_t size);
0082 LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize,
0083 size_t size);
0084 LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize);
0085 LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems,
0086 int *size, int size_elem, int limit,
0087 const char *what);
0088 LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem,
0089 int final_n, int size_elem);
0090 LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag);
0091
0092 #endif
0093