xref: /freebsd/contrib/lua/src/lmem.h (revision 0495ed398c4f64013bab2327eb13a303e1f90c13)
18e3e3a7aSWarner Losh /*
2*0495ed39SKyle Evans ** $Id: lmem.h $
38e3e3a7aSWarner Losh ** Interface to Memory Manager
48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h
58e3e3a7aSWarner Losh */
68e3e3a7aSWarner Losh 
78e3e3a7aSWarner Losh #ifndef lmem_h
88e3e3a7aSWarner Losh #define lmem_h
98e3e3a7aSWarner Losh 
108e3e3a7aSWarner Losh 
118e3e3a7aSWarner Losh #include <stddef.h>
128e3e3a7aSWarner Losh 
138e3e3a7aSWarner Losh #include "llimits.h"
148e3e3a7aSWarner Losh #include "lua.h"
158e3e3a7aSWarner Losh 
168e3e3a7aSWarner Losh 
17*0495ed39SKyle Evans #define luaM_error(L)	luaD_throw(L, LUA_ERRMEM)
18*0495ed39SKyle Evans 
19*0495ed39SKyle Evans 
208e3e3a7aSWarner Losh /*
21*0495ed39SKyle Evans ** This macro tests whether it is safe to multiply 'n' by the size of
22*0495ed39SKyle Evans ** type 't' without overflows. Because 'e' is always constant, it avoids
23*0495ed39SKyle Evans ** the runtime division MAX_SIZET/(e).
248e3e3a7aSWarner Losh ** (The macro is somewhat complex to avoid warnings:  The 'sizeof'
258e3e3a7aSWarner Losh ** comparison avoids a runtime comparison when overflow cannot occur.
268e3e3a7aSWarner Losh ** The compiler should be able to optimize the real test by itself, but
278e3e3a7aSWarner Losh ** when it does it, it may give a warning about "comparison is always
288e3e3a7aSWarner Losh ** false due to limited range of data type"; the +1 tricks the compiler,
298e3e3a7aSWarner Losh ** avoiding this warning but also this optimization.)
308e3e3a7aSWarner Losh */
31*0495ed39SKyle Evans #define luaM_testsize(n,e)  \
32*0495ed39SKyle Evans 	(sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e))
33*0495ed39SKyle Evans 
34*0495ed39SKyle Evans #define luaM_checksize(L,n,e)  \
35*0495ed39SKyle Evans 	(luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0))
36*0495ed39SKyle Evans 
37*0495ed39SKyle Evans 
38*0495ed39SKyle Evans /*
39*0495ed39SKyle Evans ** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that
40*0495ed39SKyle Evans ** the result is not larger than 'n' and cannot overflow a 'size_t'
41*0495ed39SKyle Evans ** when multiplied by the size of type 't'. (Assumes that 'n' is an
42*0495ed39SKyle Evans ** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.)
43*0495ed39SKyle Evans */
44*0495ed39SKyle Evans #define luaM_limitN(n,t)  \
45*0495ed39SKyle Evans   ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) :  \
46*0495ed39SKyle Evans      cast_uint((MAX_SIZET/sizeof(t))))
47*0495ed39SKyle Evans 
488e3e3a7aSWarner Losh 
498e3e3a7aSWarner Losh /*
508e3e3a7aSWarner Losh ** Arrays of chars do not need any test
518e3e3a7aSWarner Losh */
528e3e3a7aSWarner Losh #define luaM_reallocvchar(L,b,on,n)  \
53*0495ed39SKyle Evans   cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char)))
548e3e3a7aSWarner Losh 
55*0495ed39SKyle Evans #define luaM_freemem(L, b, s)	luaM_free_(L, (b), (s))
56*0495ed39SKyle Evans #define luaM_free(L, b)		luaM_free_(L, (b), sizeof(*(b)))
57*0495ed39SKyle Evans #define luaM_freearray(L, b, n)   luaM_free_(L, (b), (n)*sizeof(*(b)))
588e3e3a7aSWarner Losh 
59*0495ed39SKyle Evans #define luaM_new(L,t)		cast(t*, luaM_malloc_(L, sizeof(t), 0))
60*0495ed39SKyle Evans #define luaM_newvector(L,n,t)	cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0))
61*0495ed39SKyle Evans #define luaM_newvectorchecked(L,n,t) \
62*0495ed39SKyle Evans   (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t))
638e3e3a7aSWarner Losh 
64*0495ed39SKyle Evans #define luaM_newobject(L,tag,s)	luaM_malloc_(L, (s), tag)
658e3e3a7aSWarner Losh 
668e3e3a7aSWarner Losh #define luaM_growvector(L,v,nelems,size,t,limit,e) \
67*0495ed39SKyle Evans 	((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \
68*0495ed39SKyle Evans                          luaM_limitN(limit,t),e)))
698e3e3a7aSWarner Losh 
708e3e3a7aSWarner Losh #define luaM_reallocvector(L, v,oldn,n,t) \
71*0495ed39SKyle Evans    (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \
72*0495ed39SKyle Evans                                   cast_sizet(n) * sizeof(t))))
73*0495ed39SKyle Evans 
74*0495ed39SKyle Evans #define luaM_shrinkvector(L,v,size,fs,t) \
75*0495ed39SKyle Evans    ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t))))
768e3e3a7aSWarner Losh 
778e3e3a7aSWarner Losh LUAI_FUNC l_noret luaM_toobig (lua_State *L);
788e3e3a7aSWarner Losh 
798e3e3a7aSWarner Losh /* not to be called directly */
808e3e3a7aSWarner Losh LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
818e3e3a7aSWarner Losh                                                           size_t size);
82*0495ed39SKyle Evans LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize,
83*0495ed39SKyle Evans                                                               size_t size);
84*0495ed39SKyle Evans LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize);
85*0495ed39SKyle Evans LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems,
86*0495ed39SKyle Evans                                int *size, int size_elem, int limit,
878e3e3a7aSWarner Losh                                const char *what);
88*0495ed39SKyle Evans LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem,
89*0495ed39SKyle Evans                                     int final_n, int size_elem);
90*0495ed39SKyle Evans LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag);
918e3e3a7aSWarner Losh 
928e3e3a7aSWarner Losh #endif
938e3e3a7aSWarner Losh 
94