void __a_serializet(T *t,char *name) { char fn[1000]; H h =_m_new_from_t(t); S *s = _m_serialize(h.m); __a_vm_fn(fn,G_vm->dir,name); writeFile(fn,s,s->total_size); free(s); _m_free(h); }
T *__a_unserializet(char *dir_path,char *name) { char fn[1000]; __a_vm_fn(fn,dir_path,name); S *s = readFile(fn,0); H h = _m_unserialize(s); free(s); T *t = _t_new_from_m(h); _m_free(h); return t; }
/** * clean shutdown of the the ceptr system * * should be called by the thread that called _a_boot() (or _a_start_vmhost()) */ void _a_shut_down() { // cleanly close down any processing in the VM_Host __r_kill(G_vm->r); _v_join_thread(&G_vm->clock_thread); _v_join_thread(&G_vm->vm_thread); char fn[1000]; // serialize the semtable __a_serializet(_t_root(G_vm->sem->stores[0].definitions),SEM_FN); int i; T *paths = _t_new_root(RECEPTOR_PATHS); for (i=0;i<G_vm->sem->contexts;i++) { // we don't need the path of the root so start at 1 int *p = _t_get_path(G_vm->sem->stores[i].definitions); if (p) { _t_new(paths,RECEPTOR_PATH,p,sizeof(int)*(_t_path_depth(p)+1)); free(p); } else _t_newr(paths,STRUCTURE_ANYTHING); // should be something like DELETED_CONTEXT } __a_serializet(paths,PATHS_FN); _t_free(paths); // serialize the receptor part of the vmhost void *surface; size_t length; _r_serialize(G_vm->r,&surface,&length); // _r_unserialize(surface); __a_vmfn(fn,G_vm->dir); writeFile(fn,surface,length); free(surface); // serialize other parts of the vmhost H h = _m_newr(null_H,SYS_STATE); H har = _m_newr(h,ACTIVE_RECEPTORS); for (i=0;i<G_vm->active_receptor_count;i++) { _m_new(har,RECEPTOR_XADDR,&G_vm->active_receptors[i].x,sizeof(Xaddr)); } S *s = _m_serialize(h.m); __a_vm_state_fn(fn,G_vm->dir); writeFile(fn,s,s->total_size); free(s); _m_free(h); // free the memory used by the SYS_RECEPTOR _v_free(G_vm); G_vm = NULL; }
void *_m_realloc (void *v, unsigned s, const char *file, int line) { struct alloc_block *ab; void *v2; if (v==NULL) { return _m_alloc(s,file,line); } if (s==0) { _m_free(v); return NULL; } ab = (struct alloc_block *)((char *)v - (unsigned)&((struct alloc_block*)NULL)->content); if (!check_block("Realloc", ab)) return NULL; v2 = _m_alloc(s, file, line); if (v2) memcpy(v2, v, ab->size); _m_free(v); return v2; }
/** * free the memory used by an mtree * * @param[in] h handle to mtree to free * @param[in] free_surface boolean to indicate whether to free the surface values */ void __m_free(H h,int free_surface) { int i = h.m->levels; while(i--) { L *l = _GET_LEVEL(h,i); Mindex j = l->nodes; if (free_surface) { while(j--) { N *n = _GET_NODE(h,l,j); if (!(n->flags & TFLAG_REFERENCE)) { if (n->flags & TFLAG_SURFACE_IS_RECEPTOR) raise_error("mtree can't free receptor!"); if (n->flags & TFLAG_SURFACE_IS_TREE && !(n->flags & TFLAG_SURFACE_IS_RECEPTOR)) { _m_free(*(H *)n->surface); } if (n->flags & TFLAG_ALLOCATED) { free(n->surface); } } } } free(l->nP); } free(h.m->lP); free(h.m); }
/** * bootstrap the ceptr system * * starts up the vmhost and wakes up receptors that should be running in it. * * @TODO check the compository to verify our version of the vmhost * */ void _a_boot(char *dir_path) { // check if the storage directory exists struct stat st = {0}; if (stat(dir_path, &st) == -1) { // if no directory we are firing up an initial instance, so // create directory mkdir(dir_path,0700); // instantiate a VMHost object G_vm = _v_new(); // create the basic receptors that all VMHosts have _v_instantiate_builtins(G_vm); } else { char fn[1000]; void *buffer; // unserialize the semtable base tree SemTable *sem = _sem_new(); T *t = __a_unserializet(dir_path,SEM_FN); sem->stores[0].definitions = t; // restore definitions to the correct store slots T *paths = __a_unserializet(dir_path,PATHS_FN); int i = 0; int c = _t_children(paths); for(i=1;i<=c;i++) { T *p = _t_child(paths,i); if (semeq(RECEPTOR_PATH,_t_symbol(p))) { T *x = _t_get(t,(int *)_t_surface(p)); sem->stores[i-1].definitions = x; } } _t_free(paths); sem->contexts = c+1; // unserialize all of the vmhost's instantiated receptors and other instances __a_vmfn(fn,dir_path); buffer = readFile(fn,0); Receptor *r = _r_unserialize(sem,buffer); G_vm = __v_init(r,sem); free(buffer); // unserialize other vmhost state data S *s; __a_vm_state_fn(fn,dir_path); s = readFile(fn,0); H h = _m_unserialize(s); free(s); H hars; hars.m=h.m; hars.a = _m_child(h,1); // first child is ACTIVE_RECEPTORS H har; har.m=h.m; int j = _m_children(hars); for (i=1;i<=j;i++) { har.a = _m_child(hars,i); if(!semeq(_m_symbol(har),RECEPTOR_XADDR)) raise_error("expecting RECEPTOR_XADDR!"); _v_activate(G_vm,*(Xaddr *)_m_surface(har)); } _m_free(h); } G_vm->dir = dir_path; // _a_check_vm_host_version_on_the_compository(); _v_start_vmhost(G_vm); }
void operator delete[] (void *v) { _m_free(v); }