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
9 #define MALLOCSIZE 20*1024
10
11 struct malloclist {
12   struct malloclist *next;
13   int size;
14   char space[];
15 };
16
17 struct malloclist * top=NULL;
18 int offset=0;
19
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;
25     if (size>basesize)
26       basesize=size;
27     tmp=RUNMALLOC(sizeof(struct malloclist)+basesize);
28     tmp->next=top;
29     top=tmp;
30     top->size=basesize;
31     offset=0;
32   }
33   int tmpoffset=offset;
34   offset+=size;
35   return &top->space[tmpoffset];
36 }
37
38 void freemalloc() {
39   while(top!=NULL) {
40     struct malloclist *next=top->next;
41     RUNFREE(top);
42     top=next;
43   }
44 }
45
46
47 void ** makecheckpoint(int numparams, void ** srcpointer, struct RuntimeHash * forward, struct RuntimeHash * reverse) {
48 #ifdef PRECISE_GC
49   void **newarray=cpmalloc(sizeof(void *)*numparams);
50 #else
51   void **newarray=RUNMALLOC(sizeof(void *)*numparams);
52 #endif
53   struct RuntimeHash *todo=allocateRuntimeHash(100);
54   int i;
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]);
59     else {
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);
64       newarray[i]=copy;
65     }
66   }
67   while(RuntimeHashcountset(todo)!=0) {
68     void * ptr=(void *) RuntimeHashfirstkey(todo);
69     int type=((int *)ptr)[0];
70     RuntimeHashremove(todo, (int) ptr, (int) ptr);
71     {
72       void *cpy;
73       RuntimeHashget(forward, (int) ptr, (int *) &cpy);
74       int * pointer=pointerarray[type];
75       if (pointer==0) {
76         /* Array of primitives */
77         /* Do nothing */
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___;
83         int i;
84         for(i=0;i<length;i++) {
85           void *objptr=((void **)(((char *)& ao->___length___)+sizeof(int)))[i];
86           if (objptr==NULL) {
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]);
90           else {
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;
96           }
97         }
98       } else {
99         int size=pointer[0];
100         int i;
101         for(i=1;i<=size;i++) {
102           int offset=pointer[i];
103           void * objptr=*((void **)(((int)ptr)+offset));
104           if (objptr==NULL) {
105             *((void **) (((int)cpy)+offset))=NULL;
106           } else if (RuntimeHashcontainskey(forward, (int) objptr))
107             RuntimeHashget(forward, (int) objptr, (int *) &(((char *)cpy)[offset]));
108           else {
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;
114           }
115         }
116       }
117     }
118   }
119   freeRuntimeHash(todo);
120   return newarray;
121 }
122
123 void * createcopy(void * orig) {
124   if (orig==0)
125     return 0;
126   else {
127     int type=((int *)orig)[0];
128     if (type<NUMCLASSES) {
129       /* We have a normal object */
130       int size=classsize[type];
131 #ifdef PRECISE_GC
132       void *newobj=cpmalloc(size);
133 #else
134       void *newobj=RUNMALLOC(size);
135 #endif
136       memcpy(newobj, orig, size);
137       return newobj;
138     } else {
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;
144 #ifdef PRECISE_GC
145       void *newobj=cpmalloc(size);
146 #else
147       void *newobj=RUNMALLOC(size);
148 #endif
149       memcpy(newobj, orig, size);
150       return newobj;
151     }
152   }
153 }
154
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);
158   int i;
159
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]);
164     }
165   }
166   
167   while(RuntimeHashcountset(todo)!=0) {
168     void * ptr=(void *) RuntimeHashfirstkey(todo);
169     int type=((int *)ptr)[0];
170     RuntimeHashremove(todo, (int) ptr, (int) ptr);
171
172     {
173       void *cpy;
174       int *pointer;
175       int size;
176       RuntimeHashget(reverse, (int) ptr, (int *) &cpy);
177       pointer=pointerarray[type];
178       size=classsize[type];
179
180       if (pointer==0) {
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);
186
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___;
192         int i;
193         int cpysize=sizeof(struct ArrayObject)+length*size;
194         memcpy(ao_cpy, ao, cpysize);
195
196         for(i=0;i<length;i++) {
197           void *objptr=((void **)(((char *)& ao->___length___)+sizeof(int)))[i];
198           if (objptr==NULL)
199             ((void **)(((char *)& ao_cpy->___length___)+sizeof(int)))[i]=NULL;
200           else {
201             if (!RuntimeHashcontainskey(visited, (int) objptr)) {
202               RuntimeHashadd(visited, (int) objptr, (int) objptr);
203               RuntimeHashadd(todo, (int) objptr, (int) objptr);
204             }
205             RuntimeHashget(reverse, (int) objptr, (int *) &((void **)(((char *)& ao_cpy->___length___)+sizeof(int)))[i]);
206           }
207         }
208       } else {
209         int numptr=pointer[0];
210         int i;
211         void *flagptr;
212         if (hasflags[type]) {
213           flagptr=(void *) (((int *)cpy)[2]);
214         }
215         memcpy(cpy, ptr, size);
216         for(i=1;i<=numptr;i++) {
217           int offset=pointer[i];
218           void * objptr=*((void **)(((int)ptr)+offset));
219           if (objptr==NULL)
220             *((void **) (((int)cpy)+offset))=NULL;
221           else {
222             if (!RuntimeHashcontainskey(visited, (int) objptr)) {
223               RuntimeHashadd(visited, (int) objptr, (int) objptr);
224               RuntimeHashadd(todo, (int) objptr, (int) objptr);
225             }
226             RuntimeHashget(reverse, (int) objptr, (int *) &(((char *)cpy)[offset]));
227           }
228         }
229         if (hasflags[type]) {
230           (((void **)cpy)[2])=flagptr;
231           flagorandinit(cpy, 0, 0xFFFFFFFF);
232         }
233       }
234     }
235   }
236   freeRuntimeHash(todo);
237   freeRuntimeHash(visited);
238 }