Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-07 10:37:56

0001 /*
0002 ** $Id: lmem.h $
0003 ** Interface to Memory Manager
0004 ** See Copyright Notice in lua.h
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 ** This macro tests whether it is safe to multiply 'n' by the size of
0022 ** type 't' without overflows. Because 'e' is always constant, it avoids
0023 ** the runtime division MAX_SIZET/(e).
0024 ** (The macro is somewhat complex to avoid warnings:  The 'sizeof'
0025 ** comparison avoids a runtime comparison when overflow cannot occur.
0026 ** The compiler should be able to optimize the real test by itself, but
0027 ** when it does it, it may give a warning about "comparison is always
0028 ** false due to limited range of data type"; the +1 tricks the compiler,
0029 ** avoiding this warning but also this optimization.)
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 ** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that
0040 ** the result is not larger than 'n' and cannot overflow a 'size_t'
0041 ** when multiplied by the size of type 't'. (Assumes that 'n' is an
0042 ** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.)
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 ** Arrays of chars do not need any test
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 /* not to be called directly */
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