// VLISP Virtual Machine - 2006 - by Sylvain Huet // Lowcost IS Powerfull #include"vmem.h" #include"vloader.h" #include"vlog.h" void play_check(int nocb); #ifdef VSIMU #include #include #include int vmem_heap[VMEM_LENGTH]; #endif #define VMEM_DBG int vmem_heapindex; int *vmem_top; int vmem_stack; int vmem_start; int vmem_broken; // initialisation de la mémoire void vmemInit(int start) { vmem_top=&vmem_heap[VMEM_LENGTH]; vmem_stack=0; vmem_start=vmem_heapindex=start; vmem_broken=0; } void vmemSetstart(int start) { int size; if (start>=vmem_start-HEADER_LENGTH) return; size=((vmem_start-HEADER_LENGTH-start)<<2)-1; vmem_heap[start]=(size<<8)+(TYPE_BINARY); vmem_heap[start+HEADER_GC]=0; vmem_heap[start+HEADER_LIST]=0; vmem_start=start; } void vmemGCfirst() { int i,k,j,n; int first; first=-1; for(i=vmem_stack;i<0;i++) { k=vmem_top[i]; if ((ISVALPNT(k))&&(k!=NIL)) { k=VALTOPNT(k); #ifdef VMEM_DBG if ((k<0)||(k>=VMEM_LENGTH)) consolestr("1.k out of space\n"); #endif if (!HEADER_USED(k)) { HEADER_MARK(k); vmem_heap[k+HEADER_LIST]=first; first=k; } } } while(first!=-1) { k=first; #ifdef VMEM_DBG if ((k<0)||(k>=VMEM_LENGTH)) consolestr("1.first out of space\n"); #endif first=vmem_heap[k+HEADER_LIST]; if (HEADER_TYPE(k)) // bloc table { n=VSIZE(k); j=k+HEADER_LENGTH; for(i=0;i=VMEM_LENGTH)) consolestr("1.k2 out of space\n"); #endif if (!HEADER_USED(k)) { HEADER_MARK(k); vmem_heap[k+HEADER_LIST]=first; first=k; } } } } } } #ifdef VMEM_DBG void dumpheap() { int pos,realsize; #ifdef VSIMU FILE *f; f=fopen("mem.bin","wb"); fwrite(&vmem_start,1,4,f); fwrite(&vmem_heapindex,1,4,f); fwrite(&vmem_stack,1,4,f); fwrite(vmem_heap,VMEM_LENGTH,1,f); fclose(f); #endif pos=vmem_start; while(pos < vmem_heapindex) { realsize=VSIZE(pos)+HEADER_LENGTH; consolehx(pos); consolestr(":pos ");consolehx(realsize); consolestr(":realsize\n"); if ((realsize<0)||(realsize>=VMEM_LENGTH)) { consolestr("2.realsize out of range\n"); dump((char*)&vmem_heap[pos-32],128); return; } dump((char*)&vmem_heap[pos],32); pos+=realsize; } } #endif void vmemGCsecond() { int pos,newpos,realsize; pos=newpos=vmem_start; while(pos < vmem_heapindex) { realsize=VSIZE(pos)+HEADER_LENGTH; #ifdef VMEM_DBG if ((realsize<0)||(realsize>=VMEM_LENGTH)) { dumpheap(); #ifdef VSIMU printf("CORE DUMP\n"); getchar(); exit(0); #endif } #endif if (HEADER_USED(pos)) { vmem_heap[pos+HEADER_GC]=newpos<<1; newpos+=realsize; } pos+=realsize; } } void vmemGCthird() { int i,k,j,n; int first; first=-1; for(i=vmem_stack;i<0;i++) { k=vmem_top[i]; if ((ISVALPNT(k))&&(k!=NIL)) { k=VALTOPNT(k); vmem_top[i]=vmem_heap[k+HEADER_GC]|1; // attention, hack if (!HEADER_USED(k)) { HEADER_MARK(k); vmem_heap[k+HEADER_LIST]=first; first=k; } } } while(first!=-1) { k=first; first=vmem_heap[k+HEADER_LIST]; if (HEADER_TYPE(k)) // bloc table { n=VSIZE(k); j=k+HEADER_LENGTH; for(i=0;i>2); if (VMEM_LENGTH+vmem_stack-vmem_heapindex-wsize=0) { int i; for(i=0;i0) q[--n]=VPULL(); VPUSH(PNTTOVAL(p)); } void vmemDumpHeap() { #ifdef VSIMU int i,pos,realsize,n; #endif consolestr("HEAP\n----\n"); #ifdef VSIMU pos=vmem_start; n=0; while(pos < vmem_heapindex) { realsize=VSIZE(pos)+HEADER_LENGTH; printf("%06X : %s %d\n",pos,HEADER_TYPE(pos)?"Tab":"Bin",VSIZE(pos)); for(i=0;i=vmem_stack;i--) { k=vmem_top[i]; printf("%d : %6x -> %6x (%d)\n",i,k,k>>1,k>>1); } #endif } void vmemDump() { vmemDumpHeap(); vmemDumpStack(); } void vmemDumpShort() { logGC(); }