1*8e3e3a7aSWarner Losh /* 2*8e3e3a7aSWarner Losh ** $Id: lmem.h,v 1.43 2014/12/19 17:26:14 roberto Exp $ 3*8e3e3a7aSWarner Losh ** Interface to Memory Manager 4*8e3e3a7aSWarner Losh ** See Copyright Notice in lua.h 5*8e3e3a7aSWarner Losh */ 6*8e3e3a7aSWarner Losh 7*8e3e3a7aSWarner Losh #ifndef lmem_h 8*8e3e3a7aSWarner Losh #define lmem_h 9*8e3e3a7aSWarner Losh 10*8e3e3a7aSWarner Losh 11*8e3e3a7aSWarner Losh #include <stddef.h> 12*8e3e3a7aSWarner Losh 13*8e3e3a7aSWarner Losh #include "llimits.h" 14*8e3e3a7aSWarner Losh #include "lua.h" 15*8e3e3a7aSWarner Losh 16*8e3e3a7aSWarner Losh 17*8e3e3a7aSWarner Losh /* 18*8e3e3a7aSWarner Losh ** This macro reallocs a vector 'b' from 'on' to 'n' elements, where 19*8e3e3a7aSWarner Losh ** each element has size 'e'. In case of arithmetic overflow of the 20*8e3e3a7aSWarner Losh ** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because 21*8e3e3a7aSWarner Losh ** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e). 22*8e3e3a7aSWarner Losh ** 23*8e3e3a7aSWarner Losh ** (The macro is somewhat complex to avoid warnings: The 'sizeof' 24*8e3e3a7aSWarner Losh ** comparison avoids a runtime comparison when overflow cannot occur. 25*8e3e3a7aSWarner Losh ** The compiler should be able to optimize the real test by itself, but 26*8e3e3a7aSWarner Losh ** when it does it, it may give a warning about "comparison is always 27*8e3e3a7aSWarner Losh ** false due to limited range of data type"; the +1 tricks the compiler, 28*8e3e3a7aSWarner Losh ** avoiding this warning but also this optimization.) 29*8e3e3a7aSWarner Losh */ 30*8e3e3a7aSWarner Losh #define luaM_reallocv(L,b,on,n,e) \ 31*8e3e3a7aSWarner Losh (((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \ 32*8e3e3a7aSWarner Losh ? luaM_toobig(L) : cast_void(0)) , \ 33*8e3e3a7aSWarner Losh luaM_realloc_(L, (b), (on)*(e), (n)*(e))) 34*8e3e3a7aSWarner Losh 35*8e3e3a7aSWarner Losh /* 36*8e3e3a7aSWarner Losh ** Arrays of chars do not need any test 37*8e3e3a7aSWarner Losh */ 38*8e3e3a7aSWarner Losh #define luaM_reallocvchar(L,b,on,n) \ 39*8e3e3a7aSWarner Losh cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) 40*8e3e3a7aSWarner Losh 41*8e3e3a7aSWarner Losh #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 42*8e3e3a7aSWarner Losh #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 43*8e3e3a7aSWarner Losh #define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0) 44*8e3e3a7aSWarner Losh 45*8e3e3a7aSWarner Losh #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) 46*8e3e3a7aSWarner Losh #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 47*8e3e3a7aSWarner Losh #define luaM_newvector(L,n,t) \ 48*8e3e3a7aSWarner Losh cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 49*8e3e3a7aSWarner Losh 50*8e3e3a7aSWarner Losh #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) 51*8e3e3a7aSWarner Losh 52*8e3e3a7aSWarner Losh #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 53*8e3e3a7aSWarner Losh if ((nelems)+1 > (size)) \ 54*8e3e3a7aSWarner Losh ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 55*8e3e3a7aSWarner Losh 56*8e3e3a7aSWarner Losh #define luaM_reallocvector(L, v,oldn,n,t) \ 57*8e3e3a7aSWarner Losh ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 58*8e3e3a7aSWarner Losh 59*8e3e3a7aSWarner Losh LUAI_FUNC l_noret luaM_toobig (lua_State *L); 60*8e3e3a7aSWarner Losh 61*8e3e3a7aSWarner Losh /* not to be called directly */ 62*8e3e3a7aSWarner Losh LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 63*8e3e3a7aSWarner Losh size_t size); 64*8e3e3a7aSWarner Losh LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 65*8e3e3a7aSWarner Losh size_t size_elem, int limit, 66*8e3e3a7aSWarner Losh const char *what); 67*8e3e3a7aSWarner Losh 68*8e3e3a7aSWarner Losh #endif 69*8e3e3a7aSWarner Losh 70