1 #include "checkpoint.h"
3 #include "structdefs.h"
9 #define MALLOCSIZE 20*1024
12 struct malloclist *next;
17 struct malloclist * top=NULL;
20 void * cpmalloc(int size) {
21 int endoffset=offset+size;
22 if (top==NULL||endoffset>top->size) {
23 int basesize=MALLOCSIZE;
24 struct malloclist *tmp;
27 tmp=RUNMALLOC(sizeof(struct malloclist)+basesize);
35 return &top->space[tmpoffset];
40 struct malloclist *next=top->next;
47 void ** makecheckpoint(int numparams, void ** srcpointer, struct RuntimeHash * forward, struct RuntimeHash * reverse) {
49 void **newarray=cpmalloc(sizeof(void *)*numparams);
51 void **newarray=RUNMALLOC(sizeof(void *)*numparams);
53 struct RuntimeHash *todo=allocateRuntimeHash(100);
55 for(i=0;i<numparams;i++) {
56 void * objptr=srcpointer[i];
57 if (RuntimeHashcontainskey(forward, (int) objptr))
58 RuntimeHashget(forward,(int) objptr,(int *) &newarray[i]);
60 void * copy=createcopy(objptr);
61 RuntimeHashadd(forward, (int) objptr, (int)copy);
62 RuntimeHashadd(reverse, (int) copy, (int) objptr);
63 RuntimeHashadd(todo, (int) objptr, (int) objptr);
67 while(RuntimeHashcountset(todo)!=0) {
68 void * ptr=(void *) RuntimeHashfirstkey(todo);
69 int type=((int *)ptr)[0];
70 RuntimeHashremove(todo, (int) ptr, (int) ptr);
73 RuntimeHashget(forward, (int) ptr, (int *) &cpy);
74 int * pointer=pointerarray[type];
76 /* Array of primitives */
78 } else if (((int)pointer)==1) {
79 /* Array of pointers */
80 struct ArrayObject *ao=(struct ArrayObject *) ptr;
81 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
82 int length=ao->___length___;
84 for(i=0;i<length;i++) {
85 void *objptr=((void **)(((char *)& ao->___length___)+sizeof(int)))[i];
87 ((void **)(((char *)& ao->___length___)+sizeof(int)))[i]=NULL;
88 } else if (RuntimeHashcontainskey(forward, (int) objptr))
89 RuntimeHashget(forward,(int) objptr,(int *) &((void **)(((char *)& ao_cpy->___length___)+sizeof(int)))[i]);
91 void * copy=createcopy(objptr);
92 RuntimeHashadd(forward, (int) objptr, (int)copy);
93 RuntimeHashadd(reverse, (int) copy, (int) objptr);
94 RuntimeHashadd(todo, (int) objptr, (int) objptr);
95 ((void **)(((char *)& ao_cpy->___length___)+sizeof(int)))[i]=copy;
101 for(i=1;i<=size;i++) {
102 int offset=pointer[i];
103 void * objptr=*((void **)(((int)ptr)+offset));
105 *((void **) (((int)cpy)+offset))=NULL;
106 } else if (RuntimeHashcontainskey(forward, (int) objptr))
107 RuntimeHashget(forward, (int) objptr, (int *) &(((char *)cpy)[offset]));
109 void * copy=createcopy(objptr);
110 RuntimeHashadd(forward, (int) objptr, (int) copy);
111 RuntimeHashadd(reverse, (int) copy, (int) objptr);
112 RuntimeHashadd(todo, (int) objptr, (int) objptr);
113 *((void **) (((int)cpy)+offset))=copy;
119 freeRuntimeHash(todo);
123 void * createcopy(void * orig) {
127 int type=((int *)orig)[0];
128 if (type<NUMCLASSES) {
129 /* We have a normal object */
130 int size=classsize[type];
132 void *newobj=cpmalloc(size);
134 void *newobj=RUNMALLOC(size);
136 memcpy(newobj, orig, size);
139 /* We have an array */
140 struct ArrayObject *ao=(struct ArrayObject *)orig;
141 int elementsize=classsize[type];
142 int length=ao->___length___;
143 int size=sizeof(struct ArrayObject)+length*elementsize;
145 void *newobj=cpmalloc(size);
147 void *newobj=RUNMALLOC(size);
149 memcpy(newobj, orig, size);
155 void restorecheckpoint(int numparams, void ** original, void ** checkpoint, struct RuntimeHash *forward, struct RuntimeHash * reverse) {
156 struct RuntimeHash *todo=allocateRuntimeHash(100);
157 struct RuntimeHash *visited=allocateRuntimeHash(100);
160 for(i=0;i<numparams;i++) {
161 if (checkpoint[i]!=NULL) {
162 RuntimeHashadd(todo, (int) checkpoint[i], (int) checkpoint[i]);
163 RuntimeHashadd(visited, (int) checkpoint[i], (int) checkpoint[i]);
167 while(RuntimeHashcountset(todo)!=0) {
168 void * ptr=(void *) RuntimeHashfirstkey(todo);
169 int type=((int *)ptr)[0];
170 RuntimeHashremove(todo, (int) ptr, (int) ptr);
176 RuntimeHashget(reverse, (int) ptr, (int *) &cpy);
177 pointer=pointerarray[type];
178 size=classsize[type];
181 /* Array of primitives */
182 struct ArrayObject *ao=(struct ArrayObject *) ptr;
183 int length=ao->___length___;
184 int cpysize=sizeof(struct ArrayObject)+length*size;
185 memcpy(cpy, ptr, cpysize);
187 } else if ((int)pointer==1) {
188 /* Array of pointers */
189 struct ArrayObject *ao=(struct ArrayObject *) ptr;
190 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
191 int length=ao->___length___;
193 int cpysize=sizeof(struct ArrayObject)+length*size;
194 memcpy(ao_cpy, ao, cpysize);
196 for(i=0;i<length;i++) {
197 void *objptr=((void **)(((char *)& ao->___length___)+sizeof(int)))[i];
199 ((void **)(((char *)& ao_cpy->___length___)+sizeof(int)))[i]=NULL;
201 if (!RuntimeHashcontainskey(visited, (int) objptr)) {
202 RuntimeHashadd(visited, (int) objptr, (int) objptr);
203 RuntimeHashadd(todo, (int) objptr, (int) objptr);
205 RuntimeHashget(reverse, (int) objptr, (int *) &((void **)(((char *)& ao_cpy->___length___)+sizeof(int)))[i]);
209 int numptr=pointer[0];
212 if (hasflags[type]) {
213 flagptr=(void *) (((int *)cpy)[2]);
215 memcpy(cpy, ptr, size);
216 for(i=1;i<=numptr;i++) {
217 int offset=pointer[i];
218 void * objptr=*((void **)(((int)ptr)+offset));
220 *((void **) (((int)cpy)+offset))=NULL;
222 if (!RuntimeHashcontainskey(visited, (int) objptr)) {
223 RuntimeHashadd(visited, (int) objptr, (int) objptr);
224 RuntimeHashadd(todo, (int) objptr, (int) objptr);
226 RuntimeHashget(reverse, (int) objptr, (int *) &(((char *)cpy)[offset]));
229 if (hasflags[type]) {
230 (((void **)cpy)[2])=flagptr;
231 flagorandinit(cpy, 0, 0xFFFFFFFF);
236 freeRuntimeHash(todo);
237 freeRuntimeHash(visited);