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