This commit was manufactured by cvs2svn to create tag 'buildscript'.
[IRC.git] /
1 #include "checkpoint.h"
2 #include "runtime.h"
3 #include "structdefs.h"
4 #include <string.h>
5 #ifdef DMALLOC
6 #include "dmalloc.h"
7 #endif
8 extern void * curr_heapbase;
9 extern void * curr_heapptr;
10 extern void * curr_heapgcpoint;
11 extern void * curr_heaptop;
12
13 extern void * to_heapbase;
14 extern void * to_heapptr;
15 extern void * to_heaptop;
16
17
18 #define MALLOCSIZE 20*1024
19
20 struct malloclist {
21   struct malloclist *next;
22   int size;
23 #ifdef RAW
24   char * space;
25 #else
26   char space[];
27 #endif
28 };
29
30 struct malloclist * top=NULL;
31 int offset=0;
32
33 void * cpmalloc(int size) {
34   int endoffset=offset+size;
35   int tmpoffset=0;
36   if (top==NULL||endoffset>top->size) {
37     int basesize=MALLOCSIZE;
38     struct malloclist *tmp;
39     if (size>basesize)
40       basesize=size;
41     tmp=RUNMALLOC(sizeof(struct malloclist)+basesize);
42     tmp->next=top;
43     top=tmp;
44     top->size=basesize;
45     offset=0;
46   }
47   tmpoffset=offset;
48   offset+=size;
49   return &top->space[tmpoffset];
50 }
51
52 void freemalloc() {
53   while(top!=NULL) {
54     struct malloclist *next=top->next;
55     RUNFREE(top);
56     top=next;
57   }
58 }
59
60 void checkvalid(void * ptr) {
61   if (ptr>=curr_heapbase&&ptr<=curr_heaptop) {
62 #ifndef RAW
63     printf("Valid\n");
64 #endif
65   }
66 }
67
68 void validitycheck(struct RuntimeHash *forward, struct RuntimeHash *reverse) {
69   struct RuntimeIterator rit;
70   RuntimeHashiterator(forward, &rit);
71   while(RunhasNext(&rit)) {
72     struct ___Object___ * data=(struct ___Object___*) Runnext(&rit);
73     int type=data->type;
74     unsigned int * pointer=pointerarray[type];
75     int size;
76     int i;
77     if (pointer!=0&&((int)pointer)!=1) {
78       size=pointer[0];
79       for(i=1; i<=size; i++) {
80         int offset=pointer[i];
81         void * ptr=*(void **)(((int) data) + offset);
82         if (ptr!=NULL&&!RuntimeHashcontainskey(reverse, (int) ptr)) {
83 #ifndef RAW
84           printf("Bad\n");
85 #endif
86         }
87         checkvalid(ptr);
88       }
89     }
90   }
91
92   RuntimeHashiterator(reverse, &rit);
93   while(RunhasNext(&rit)) {
94     struct ___Object___ * data=(struct ___Object___*) Runkey(&rit);
95     int type=0;
96     unsigned int * pointer=NULL;
97     int size;
98     int i;
99     Runnext(&rit);
100     type=data->type;
101     pointer=pointerarray[type];
102     if (pointer!=0&&((int)pointer)!=1) {
103       size=pointer[0];
104       for(i=1; i<=size; i++) {
105         int offset=pointer[i];
106         void * ptr=*(void **)(((int) data) + offset);
107         if (ptr!=NULL&&!RuntimeHashcontainskey(reverse, (int) ptr)) {
108 #ifndef RAW
109           printf("Bad2\n");
110 #endif
111         }
112         checkvalid(ptr);
113       }
114     }
115   }
116 }
117
118
119
120 void ** makecheckpoint(int numparams, void ** srcpointer, struct RuntimeHash * forward, struct RuntimeHash * reverse) {
121 #ifdef PRECISE_GC
122   void **newarray=cpmalloc(sizeof(void *)*numparams);
123 #else
124   void **newarray=RUNMALLOC(sizeof(void *)*numparams);
125 #endif
126   struct RuntimeHash *todo=allocateRuntimeHash(100);
127   int i;
128
129   for(i=0; i<numparams; i++) {
130     void * objptr=srcpointer[i];
131     if (RuntimeHashcontainskey(forward, (int) objptr))
132       RuntimeHashget(forward,(int) objptr,(int *) &newarray[i]);
133     else {
134       void * copy=createcopy(objptr);
135       RuntimeHashadd(forward, (int) objptr, (int)copy);
136       RuntimeHashadd(reverse, (int) copy, (int) objptr);
137       RuntimeHashadd(todo, (int) objptr, (int) objptr);
138       newarray[i]=copy;
139     }
140   }
141   while(RuntimeHashcountset(todo)!=0) {
142     void * ptr=(void *) RuntimeHashfirstkey(todo);
143     int type=((int *)ptr)[0];
144     RuntimeHashremove(todo, (int) ptr, (int) ptr);
145     {
146       void *cpy;
147       unsigned int * pointer=NULL;
148       RuntimeHashget(forward, (int) ptr, (int *) &cpy);
149
150       pointer=pointerarray[type];
151 #ifdef TASK
152       if (type==TAGTYPE) {
153         void *objptr=((struct ___TagDescriptor___*)ptr)->flagptr;
154         if (objptr!=NULL) {
155           if (!RuntimeHashcontainskey(forward, (int) objptr)) {
156             void *copy=createcopy(objptr);
157             RuntimeHashadd(forward, (int) objptr, (int) copy);
158             RuntimeHashadd(reverse, (int) copy, (int) objptr);
159             RuntimeHashadd(todo, (int) objptr, (int) objptr);
160             ((struct ___TagDescriptor___*)cpy)->flagptr=copy;
161           } else {
162             RuntimeHashget(forward, (int) objptr, (int *) &(((struct ___TagDescriptor___*) cpy)->flagptr));
163           }
164         }
165       } else
166 #endif
167       if (pointer==0) {
168         /* Array of primitives */
169         /* Do nothing */
170       } else if (((int)pointer)==1) {
171         /* Array of pointers */
172         struct ArrayObject *ao=(struct ArrayObject *) ptr;
173         struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
174         int length=ao->___length___;
175         int i;
176         for(i=0; i<length; i++) {
177           void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
178           if (objptr==NULL) {
179             ((void **)(((char *)&ao_cpy->___length___)+sizeof(int)))[i]=NULL;
180           } else if (RuntimeHashcontainskey(forward, (int) objptr))
181             RuntimeHashget(forward,(int) objptr,(int *) &((void **)(((char *)&ao_cpy->___length___)+sizeof(int)))[i]);
182           else {
183             void * copy=createcopy(objptr);
184             RuntimeHashadd(forward, (int) objptr, (int)copy);
185             RuntimeHashadd(reverse, (int) copy, (int) objptr);
186             RuntimeHashadd(todo, (int) objptr, (int) objptr);
187             ((void **)(((char *)&ao_cpy->___length___)+sizeof(int)))[i]=copy;
188           }
189         }
190       } else {
191         int size=pointer[0];
192         int i;
193         for(i=1; i<=size; i++) {
194           int offset=pointer[i];
195           void * objptr=*((void **)(((int)ptr)+offset));
196           if (objptr==NULL) {
197             *((void **)(((int)cpy)+offset))=NULL;
198           } else if (RuntimeHashcontainskey(forward, (int) objptr))
199             RuntimeHashget(forward, (int) objptr, (int *) &(((char *)cpy)[offset]));
200           else {
201             void * copy=createcopy(objptr);
202             RuntimeHashadd(forward, (int) objptr, (int) copy);
203             RuntimeHashadd(reverse, (int) copy, (int) objptr);
204             RuntimeHashadd(todo, (int) objptr, (int) objptr);
205             *((void **)(((int)cpy)+offset))=copy;
206           }
207         }
208       }
209     }
210   }
211   freeRuntimeHash(todo);
212   return newarray;
213 }
214
215 void * createcopy(void * orig) {
216   if (orig==0)
217     return 0;
218   else {
219     int type=((int *)orig)[0];
220     if (type<NUMCLASSES) {
221       /* We have a normal object */
222       int size=classsize[type];
223 #ifdef PRECISE_GC
224       void *newobj=cpmalloc(size);
225 #else
226       void *newobj=RUNMALLOC(size);
227 #endif
228       memcpy(newobj, orig, size);
229       return newobj;
230     } else {
231       /* We have an array */
232       struct ArrayObject *ao=(struct ArrayObject *)orig;
233       int elementsize=classsize[type];
234       int length=ao->___length___;
235       int size=sizeof(struct ArrayObject)+length*elementsize;
236 #ifdef PRECISE_GC
237       void *newobj=cpmalloc(size);
238 #else
239       void *newobj=RUNMALLOC(size);
240 #endif
241       memcpy(newobj, orig, size);
242       return newobj;
243     }
244   }
245 }
246
247 void restorecheckpoint(int numparams, void ** original, void ** checkpoint, struct RuntimeHash *forward, struct RuntimeHash * reverse) {
248   struct RuntimeHash *todo=allocateRuntimeHash(100);
249   struct RuntimeHash *visited=allocateRuntimeHash(100);
250   int i;
251
252   for(i=0; i<numparams; i++) {
253     if (checkpoint[i]!=NULL) {
254       RuntimeHashadd(todo, (int) checkpoint[i], (int) checkpoint[i]);
255       RuntimeHashadd(visited, (int) checkpoint[i], (int) checkpoint[i]);
256     }
257   }
258
259   while(RuntimeHashcountset(todo)!=0) {
260     void * ptr=(void *) RuntimeHashfirstkey(todo);
261     int type=((int *)ptr)[0];
262     RuntimeHashremove(todo, (int) ptr, (int) ptr);
263
264     {
265       void *cpy;
266       unsigned int *pointer;
267       int size;
268       RuntimeHashget(reverse, (int) ptr, (int *) &cpy);
269       pointer=pointerarray[type];
270       size=classsize[type];
271 #ifdef TASK
272       if (type==TAGTYPE) {
273         void *objptr=((struct ___TagDescriptor___*)ptr)->flagptr;
274         memcpy(cpy, ptr, size);
275         if (objptr!=NULL) {
276           if (!RuntimeHashcontainskey(visited, (int) objptr)) {
277             RuntimeHashadd(visited, (int) objptr, (int) objptr);
278             RuntimeHashadd(todo, (int) objptr, (int) objptr);
279           }
280           RuntimeHashget(reverse, (int) objptr, (int *) &(((struct ___TagDescriptor___ *)cpy)->flagptr));
281         }
282       } else
283 #endif
284       if (pointer==0) {
285         /* Array of primitives */
286         struct ArrayObject *ao=(struct ArrayObject *) ptr;
287         int length=ao->___length___;
288         int cpysize=sizeof(struct ArrayObject)+length*size;
289         memcpy(cpy, ptr, cpysize);
290       } else if ((int)pointer==1) {
291         /* Array of pointers */
292         struct ArrayObject *ao=(struct ArrayObject *) ptr;
293         struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
294         int length=ao->___length___;
295         int i;
296         int cpysize=sizeof(struct ArrayObject)+length*size;
297         memcpy(ao_cpy, ao, cpysize);
298
299         for(i=0; i<length; i++) {
300           void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
301           if (objptr==NULL)
302             ((void **)(((char *)&ao_cpy->___length___)+sizeof(int)))[i]=NULL;
303           else {
304             if (!RuntimeHashcontainskey(visited, (int) objptr)) {
305               RuntimeHashadd(visited, (int) objptr, (int) objptr);
306               RuntimeHashadd(todo, (int) objptr, (int) objptr);
307             }
308             RuntimeHashget(reverse, (int) objptr, (int *) &((void **)(((char *)&ao_cpy->___length___)+sizeof(int)))[i]);
309           }
310         }
311       } else {
312         int numptr=pointer[0];
313         int i;
314         void *flagptr;
315         int oldflag;
316         int currflag;
317         if (hasflags[type]) {
318           flagptr=(void *)(((int *)cpy)[2]);
319           oldflag=(((int *)cpy)[1]);
320           currflag=(((int *)ptr)[1]);
321         }
322         memcpy(cpy, ptr, size);
323         for(i=1; i<=numptr; i++) {
324           int offset=pointer[i];
325           void * objptr=*((void **)(((int)ptr)+offset));
326           if (objptr==NULL)
327             *((void **)(((int)cpy)+offset))=NULL;
328           else {
329             if (!RuntimeHashcontainskey(visited, (int) objptr)) {
330               RuntimeHashadd(visited, (int) objptr, (int) objptr);
331               RuntimeHashadd(todo, (int) objptr, (int) objptr);
332             }
333             RuntimeHashget(reverse, (int) objptr, (int *) &(((char *)cpy)[offset]));
334           }
335         }
336         if (hasflags[type]) {
337           (((void **)cpy)[2])=flagptr;
338           if (currflag!=oldflag) {
339             flagorandinit(cpy, 0, 0xFFFFFFFF);
340 #ifdef MULTICORE
341             enqueueObject(cpy, NULL,0); //TODO
342 #else
343             enqueueObject(cpy);
344 #endif
345           }
346         }
347       }
348     }
349   }
350   freeRuntimeHash(todo);
351   freeRuntimeHash(visited);
352 }