klisp

an open source interpreter for the Kernel Programming Language.
git clone http://git.hanabi.in/repos/klisp.git
Log | Files | Refs | README

kmem.h (1600B)


      1 /*
      2 ** kmem.h
      3 ** Interface to Memory Manager
      4 ** See Copyright Notice in klisp.h
      5 */
      6 
      7 #ifndef kmem_h
      8 #define kmem_h
      9 
     10 /*
     11 ** SOURCE NOTE: This is from Lua
     12 */
     13 
     14 #include <stddef.h>
     15 
     16 #include "klisp.h"
     17 
     18 #define MEMERRMSG	"not enough memory"
     19 
     20 #define klispM_reallocv(L,b,on,n,e)                                     \
     21     ((cast(size_t, (n)+1) <= SIZE_MAX/(e)) ?  /* +1 to avoid warnings */ \
     22      klispM_realloc_(L, (b), (on)*(e), (n)*(e)) :                       \
     23      klispM_toobig(L))
     24 
     25 #define klispM_freemem(K, b, s)	klispM_realloc_(K, (b), (s), 0)
     26 #define klispM_free(K, b)	klispM_realloc_(K, (b), sizeof(*(b)), 0)
     27 #define klispM_freearray(L, b, n, t)   klispM_reallocv(L, (b), n, 0, sizeof(t))
     28 
     29 #define klispM_malloc(K,t)	klispM_realloc_(K, NULL, 0, (t))
     30 #define klispM_new(K,t)		cast(t *, klispM_malloc(K, sizeof(t)))
     31 #define klispM_newvector(L,n,t)                             \
     32     cast(t *, klispM_reallocv(L, NULL, 0, n, sizeof(t)))
     33 
     34 #define klispM_growvector(L,v,nelems,size,t,limit,e)                    \
     35     if ((nelems)+1 > (size))                                            \
     36         ((v)=cast(t *, klispM_growaux_(L,v,&(size),sizeof(t),limit,e)))
     37 
     38 #define klispM_reallocvector(L, v,oldn,n,t)                     \
     39     ((v)=cast(t *, klispM_reallocv(L, v, oldn, n, sizeof(t))))
     40 
     41 void *klispM_realloc_ (klisp_State *K, void *block, size_t oldsize,
     42                        size_t size);
     43 void *klispM_toobig (klisp_State *K);
     44 void *klispM_growaux_ (klisp_State *K, void *block, int *size,
     45                        size_t size_elem, int limit,
     46                        const char *errormsg);
     47 
     48 #endif