1 #include "checkpoint.h"
3 #include "structdefs.h"
6 #define MALLOCSIZE 20*1024
9 struct malloclist *next;
14 struct malloclist * top=NULL;
17 void * cpmalloc(int size) {
18 int endoffset=offset+size;
19 if (top==NULL||endoffset>top->size) {
20 int basesize=MALLOCSIZE;
21 struct malloclist *tmp;
24 tmp=RUNMALLOC(sizeof(struct malloclist)+basesize);
32 return &top->space[tmpoffset];
37 struct malloclist *next=top->next;
44 void ** makecheckpoint(int numparams, void ** srcpointer, struct RuntimeHash * forward, struct RuntimeHash * reverse) {
46 void **newarray=cpmalloc(sizeof(void *)*numparams);
48 void **newarray=RUNMALLOC(sizeof(void *)*numparams);
50 struct RuntimeHash *todo=allocateRuntimeHash(100);
52 for(i=0;i<numparams;i++) {
53 void * objptr=srcpointer[i];
54 if (RuntimeHashcontainskey(forward, (int) objptr))
55 RuntimeHashget(forward,(int) objptr,(int *) &newarray[i]);
57 void * copy=createcopy(objptr);
58 RuntimeHashadd(forward, (int) objptr, (int)copy);
59 RuntimeHashadd(reverse, (int) copy, (int) objptr);
60 RuntimeHashadd(todo, (int) objptr, (int) objptr);
64 while(RuntimeHashcountset(todo)!=0) {
65 void * ptr=(void *) RuntimeHashfirstkey(todo);
66 int type=((int *)ptr)[0];
67 RuntimeHashremove(todo, (int) ptr, (int) ptr);
70 RuntimeHashget(forward, (int) ptr, (int *) &cpy);
71 int * pointer=pointerarray[type];
73 /* Array of primitives */
75 } else if (((int)pointer)==1) {
76 /* Array of pointers */
77 struct ArrayObject *ao=(struct ArrayObject *) ptr;
78 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
79 int length=ao->___length___;
81 for(i=0;i<length;i++) {
82 void *objptr=((void **)(((char *)& ao->___length___)+sizeof(int)))[i];
84 ((void **)(((char *)& ao->___length___)+sizeof(int)))[i]=NULL;
85 } else if (RuntimeHashcontainskey(forward, (int) objptr))
86 RuntimeHashget(forward,(int) objptr,(int *) &((void **)(((char *)& ao_cpy->___length___)+sizeof(int)))[i]);
88 void * copy=createcopy(objptr);
89 RuntimeHashadd(forward, (int) objptr, (int)copy);
90 RuntimeHashadd(reverse, (int) copy, (int) objptr);
91 RuntimeHashadd(todo, (int) objptr, (int) objptr);
92 ((void **)(((char *)& ao_cpy->___length___)+sizeof(int)))[i]=copy;
98 for(i=1;i<=size;i++) {
99 int offset=pointer[i];
100 void * objptr=*((void **)(((int)ptr)+offset));
102 *((void **) (((int)cpy)+offset))=NULL;
103 } else if (RuntimeHashcontainskey(forward, (int) objptr))
104 RuntimeHashget(forward, (int) objptr, (int *) &(((char *)cpy)[offset]));
106 void * copy=createcopy(objptr);
107 RuntimeHashadd(forward, (int) objptr, (int) copy);
108 RuntimeHashadd(reverse, (int) copy, (int) objptr);
109 RuntimeHashadd(todo, (int) objptr, (int) objptr);
110 *((void **) (((int)cpy)+offset))=copy;
116 freeRuntimeHash(todo);
120 void * createcopy(void * orig) {
124 int type=((int *)orig)[0];
125 if (type<NUMCLASSES) {
126 /* We have a normal object */
127 int size=classsize[type];
129 void *newobj=cpmalloc(size);
131 void *newobj=RUNMALLOC(size);
133 memcpy(newobj, orig, size);
136 /* We have an array */
137 struct ArrayObject *ao=(struct ArrayObject *)orig;
138 int elementsize=classsize[type];
139 int length=ao->___length___;
140 int size=sizeof(struct ArrayObject)+length*elementsize;
142 void *newobj=cpmalloc(size);
144 void *newobj=RUNMALLOC(size);
146 memcpy(newobj, orig, size);
152 void restorecheckpoint(int numparams, void ** original, void ** checkpoint, struct RuntimeHash *forward, struct RuntimeHash * reverse) {
153 struct RuntimeHash *todo=allocateRuntimeHash(100);
156 for(i=0;i<numparams;i++) {
157 RuntimeHashadd(todo, (int) checkpoint[i], (int) checkpoint[i]);
160 while(RuntimeHashcountset(todo)!=0) {
161 void * ptr=(void *) RuntimeHashfirstkey(todo);
162 int type=((int *)ptr)[0];
163 RuntimeHashremove(todo, (int) ptr, (int) ptr);
168 RuntimeHashget(reverse, (int) ptr, (int *) &cpy);
169 pointer=pointerarray[type];
170 size=classsize[type];
173 /* Array of primitives */
174 struct ArrayObject *ao=(struct ArrayObject *) ptr;
175 int length=ao->___length___;
176 int cpysize=sizeof(struct ArrayObject)+length*size;
177 memcpy(cpy, ptr, cpysize);
179 } else if ((int)pointer==1) {
180 /* Array of pointers */
181 struct ArrayObject *ao=(struct ArrayObject *) ptr;
182 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
183 int length=ao->___length___;
185 int cpysize=sizeof(struct ArrayObject)+length*size;
186 memcpy(ao_cpy, ao, cpysize);
188 for(i=0;i<length;i++) {
189 void *objptr=((void **)(((char *)& ao->___length___)+sizeof(int)))[i];
191 ((void **)(((char *)& ao_cpy->___length___)+sizeof(int)))[i]=NULL;
193 RuntimeHashget(reverse, (int) objptr, (int *) &((void **)(((char *)& ao_cpy->___length___)+sizeof(int)))[i]);
196 int numptr=pointer[0];
199 if (hasflags[type]) {
200 flagptr=(void *) (((int *)cpy)[2]);
202 memcpy(cpy, ptr, size);
203 for(i=1;i<=numptr;i++) {
204 int offset=pointer[i];
205 void * objptr=*((void **)(((int)ptr)+offset));
207 *((void **) (((int)cpy)+offset))=NULL;
209 RuntimeHashget(reverse, (int) objptr, (int *) &(((char *)cpy)[offset]));
211 if (hasflags[type]) {
212 (((void **)cpy)[2])=flagptr;
213 flagorand(cpy, 1, 0xFFFFFFFF);
218 freeRuntimeHash(todo);