Home | History | Annotate | Line # | Download | only in src
      1 /*	$NetBSD: lmem.h,v 1.9 2023/04/16 20:46:17 nikita Exp $	*/
      2 
      3 /*
      4 ** Id: lmem.h
      5 ** Interface to Memory Manager
      6 ** See Copyright Notice in lua.h
      7 */
      8 
      9 #ifndef lmem_h
     10 #define lmem_h
     11 
     12 
     13 #ifndef _KERNEL
     14 #include <stddef.h>
     15 #endif /* _KERNEL */
     16 
     17 #include "llimits.h"
     18 #include "lua.h"
     19 
     20 
     21 #define luaM_error(L)	luaD_throw(L, LUA_ERRMEM)
     22 
     23 
     24 /*
     25 ** This macro tests whether it is safe to multiply 'n' by the size of
     26 ** type 't' without overflows. Because 'e' is always constant, it avoids
     27 ** the runtime division MAX_SIZET/(e).
     28 ** (The macro is somewhat complex to avoid warnings:  The 'sizeof'
     29 ** comparison avoids a runtime comparison when overflow cannot occur.
     30 ** The compiler should be able to optimize the real test by itself, but
     31 ** when it does it, it may give a warning about "comparison is always
     32 ** false due to limited range of data type"; the +1 tricks the compiler,
     33 ** avoiding this warning but also this optimization.)
     34 */
     35 #define luaM_testsize(n,e)  \
     36 	(sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e))
     37 
     38 #define luaM_checksize(L,n,e)  \
     39 	(luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0))
     40 
     41 
     42 /*
     43 ** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that
     44 ** the result is not larger than 'n' and cannot overflow a 'size_t'
     45 ** when multiplied by the size of type 't'. (Assumes that 'n' is an
     46 ** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.)
     47 */
     48 #define luaM_limitN(n,t)  \
     49   ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) :  \
     50      cast_uint((MAX_SIZET/sizeof(t))))
     51 
     52 
     53 /*
     54 ** Arrays of chars do not need any test
     55 */
     56 #define luaM_reallocvchar(L,b,on,n)  \
     57   cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char)))
     58 
     59 #define luaM_freemem(L, b, s)	luaM_free_(L, (b), (s))
     60 #define luaM_free(L, b)		luaM_free_(L, (b), sizeof(*(b)))
     61 #define luaM_freearray(L, b, n)   luaM_free_(L, (b), (n)*sizeof(*(b)))
     62 
     63 #define luaM_new(L,t)		cast(t*, luaM_malloc_(L, sizeof(t), 0))
     64 #define luaM_newvector(L,n,t)	cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0))
     65 #define luaM_newvectorchecked(L,n,t) \
     66   (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t))
     67 
     68 #define luaM_newobject(L,tag,s)	luaM_malloc_(L, (s), tag)
     69 
     70 #define luaM_growvector(L,v,nelems,size,t,limit,e) \
     71 	((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \
     72                          luaM_limitN(limit,t),e)))
     73 
     74 #define luaM_reallocvector(L, v,oldn,n,t) \
     75    (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \
     76                                   cast_sizet(n) * sizeof(t))))
     77 
     78 #define luaM_shrinkvector(L,v,size,fs,t) \
     79    ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t))))
     80 
     81 LUAI_FUNC l_noret luaM_toobig (lua_State *L);
     82 
     83 /* not to be called directly */
     84 LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
     85                                                           size_t size);
     86 LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize,
     87                                                               size_t size);
     88 LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize);
     89 LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems,
     90                                int *size, int size_elem, int limit,
     91                                const char *what);
     92 LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem,
     93                                     int final_n, int size_elem);
     94 LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag);
     95 
     96 #endif
     97 
     98