#ifndef _mem_h #define _mem_h #include #include "val.h" #include "vm.h" #include "state.h" void *M(State *S, void *ptr, size_t old, size_t new); #define NEW(S,ty) (ty*)M(S, NULL, 0, sizeof(ty)) #define NEW_ARR(S,ty,n) (ty*)M(S, NULL, 0, (n)*sizeof(ty)) #define RENEW_ARR(S,p,ty,old,new) (ty*)M(S, (p), (old)*sizeof(ty), (new)*sizeof(ty)) #define NEW_OBJ(S,ty, oty) (ty*)alloc_obj(S, sizeof(ty), oty) // needs len,cap,d fields #define ENSURE_CAP(S, darr, type, needed) \ do { if (darr.cap < needed) { \ size_t __newsz = next_pwrof2(needed); \ if (__newsz < 8) __newsz = 8; \ darr.d = RENEW_ARR(S, darr.d, type, darr.cap, __newsz); \ darr.cap = __newsz; \ } } while (0) size_t next_pwrof2(size_t x); #define FREE(S,p,ty) M(S, p, sizeof(ty), 0) #define FREE_ARR(S,p,ty,old) M(S, p, (old)*sizeof(ty), 0) Obj *alloc_obj(State *S, size_t sz, ObjTy oty); #endif