add malloc attribute
[IRC.git] / Robust / src / Runtime / runtime.c
1 #include "runtime.h"
2 #include "structdefs.h"
3 #include <signal.h>
4 #include "mem.h"
5 #include <fcntl.h>
6 #include <errno.h>
7 #include <stdio.h>
8 #include "option.h"
9 #ifdef DSTM
10 #include "dstm.h"
11 #include "prelookup.h"
12 #include "prefetch.h"
13 #endif
14
15 extern int classsize[];
16 jmp_buf error_handler;
17 int instructioncount;
18
19 char *options;
20 int injectfailures=0;
21 float failurechance=0;
22 int errors=0;
23 int debugtask=0;
24 int injectinstructionfailures;
25 int failurecount;
26 float instfailurechance=0;
27 int numfailures;
28 int instaccum=0;
29 #ifdef DMALLOC
30 #include "dmalloc.h"
31 #endif
32
33 void exithandler(int sig, siginfo_t *info, void * uap) {
34   exit(0);
35 }
36
37 void initializeexithandler() {
38   struct sigaction sig;
39   sig.sa_sigaction=&exithandler;
40   sig.sa_flags=SA_SIGINFO;
41   sigemptyset(&sig.sa_mask);
42   sigaction(SIGUSR2, &sig, 0);
43 }
44
45
46 /* This function inject failures */
47
48 void injectinstructionfailure() {
49 #ifdef TASK
50   if (injectinstructionfailures) {
51     if (numfailures==0)
52       return;
53     instructioncount=failurecount;
54     instaccum+=failurecount;
55     if ((((double)random())/RAND_MAX)<instfailurechance) {
56       if (numfailures>0)
57         numfailures--;
58       printf("FAILURE!!! %d\n",numfailures);
59       longjmp(error_handler,11);
60     }
61   }
62 #else
63 #ifdef THREADS
64   if (injectinstructionfailures) {
65     if (numfailures==0)
66       return;
67     instaccum+=failurecount;
68     if ((((double)random())/RAND_MAX)<instfailurechance) {
69       if (numfailures>0)
70         numfailures--;
71       printf("FAILURE!!! %d\n",numfailures);
72       threadexit();
73     }
74   }
75 #endif
76 #endif
77 }
78
79 void CALL11(___System______exit____I,int ___status___, int ___status___) {
80   exit(___status___);
81 }
82
83 void CALL11(___System______printI____I,int ___status___, int ___status___) {
84   printf("%d\n",___status___);
85 }
86
87 long CALL00(___System______currentTimeMillis____) {
88   struct timeval tv; long long retval;
89   gettimeofday(&tv, NULL);
90   retval = tv.tv_sec; /* seconds */
91   retval*=1000; /* milliseconds */
92   retval+= (tv.tv_usec/1000); /* adjust milliseconds & add them in */
93   return retval;
94 }
95
96 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
97   struct ArrayObject * chararray=VAR(___s___)->___value___;
98   int i;
99   int offset=VAR(___s___)->___offset___;
100   for(i=0; i<VAR(___s___)->___count___; i++) {
101     short sc=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
102     putchar(sc);
103   }
104 }
105
106 #ifdef DSTM
107 void CALL00(___System______clearPrefetchCache____) {
108   prehashClear();
109 }
110
111 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
112   /* Manual Prefetches to be inserted */
113   //printf("DEBUG-> %s() ___Object___ * ___o___ = %x\n", __func__, VAR(___o___));
114   //printf("DEBUG-> %s() ArrayObject * = %x\n", __func__, VAR(___offsets___));
115   int numoffset=VAR(___offsets___)->___length___;
116   int i;
117   short offArry[numoffset];
118   for(i = 0; i<numoffset; i++) {
119     offArry[i] = *((short *)(((char *)&VAR(___offsets___)->___length___) + sizeof(int) + i * sizeof(short)));
120     //printf("DEBUG-> offArry[%d] = %d\n", i, offArry[i]);
121   }
122   unsigned int oid;
123   if(((unsigned int)(VAR(___o___)) & 1) != 0) { //odd
124     oid =  (unsigned int) VAR(___o___); //outside transaction therefore just an oid
125   } else { //even
126     oid = (unsigned int) COMPOID(VAR(___o___)); //inside transaction therefore a pointer to oid
127   }
128   rangePrefetch(oid, (short)numoffset, offArry);
129 }
130 #endif
131
132 /* Object allocation function */
133
134 #ifdef DSTM
135 __attribute__((malloc)) void * allocate_newglobal(transrecord_t *trans, int type) {
136   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(trans, classsize[type]);
137   v->type=type;
138 #ifdef THREADS
139   v->tid=0;
140   v->lockentry=0;
141   v->lockcount=0;
142 #endif
143   return v;
144 }
145
146 /* Array allocation function */
147
148 __attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(transrecord_t *trans, int type, int length) {
149   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(trans, sizeof(struct ArrayObject)+length*classsize[type]);
150   if (length<0) {
151     printf("ERROR: negative array\n");
152     return NULL;
153   }
154   v->type=type;
155   v->___length___=length;
156 #ifdef THREADS
157   v->tid=0;
158   v->lockentry=0;
159   v->lockcount=0;
160 #endif
161   return v;
162 }
163 #endif
164
165
166 #ifdef PRECISE_GC
167 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
168   struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
169   v->type=type;
170 #ifdef THREADS
171   v->tid=0;
172   v->lockentry=0;
173   v->lockcount=0;
174 #endif
175 #ifdef OPTIONAL
176   v->fses=0;
177 #endif
178   return v;
179 }
180
181 /* Array allocation function */
182
183 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
184   struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
185   v->type=type;
186   if (length<0) {
187     printf("ERROR: negative array\n");
188     return NULL;
189   }
190   v->___length___=length;
191 #ifdef THREADS
192   v->tid=0;
193   v->lockentry=0;
194   v->lockcount=0;
195 #endif
196 #ifdef OPTIONAL
197   v->fses=0;
198 #endif
199   return v;
200 }
201
202 #else
203 __attribute__((malloc)) void * allocate_new(int type) {
204   struct ___Object___ * v=FREEMALLOC(classsize[type]);
205   v->type=type;
206 #ifdef OPTIONAL
207   v->fses=0;
208 #endif
209   return v;
210 }
211
212 /* Array allocation function */
213
214 __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length) {
215   __attribute__((malloc))  struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
216   v->type=type;
217   v->___length___=length;
218 #ifdef OPTIONAL
219   v->fses=0;
220 #endif
221   return v;
222 }
223 #endif
224
225
226 /* Converts C character arrays into Java strings */
227 #ifdef PRECISE_GC
228 __attribute__((malloc)) struct ___String___ * NewString(void * ptr, const char *str,int length) {
229 #else
230   __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length) {
231 #endif
232   int i;
233 #ifdef PRECISE_GC
234   struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
235   int ptrarray[]={1, (int) ptr, (int) chararray};
236   struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
237   chararray=(struct ArrayObject *) ptrarray[2];
238 #else
239   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
240   struct ___String___ * strobj=allocate_new(STRINGTYPE);
241 #endif
242   strobj->___value___=chararray;
243   strobj->___count___=length;
244   strobj->___offset___=0;
245
246   for(i=0; i<length; i++) {
247     ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
248   }
249   return strobj;
250 }
251
252 /* Generated code calls this if we fail a bounds check */
253
254 void failedboundschk() {
255 #ifndef TASK
256   printf("Array out of bounds\n");
257 #ifdef THREADS
258   threadexit();
259 #else
260   exit(-1);
261 #endif
262 #else
263   longjmp(error_handler,2);
264 #endif
265 }
266
267 /* Abort task call */
268 void abort_task() {
269 #ifdef TASK
270   longjmp(error_handler,4);
271 #else
272   printf("Aborting\n");
273   exit(-1);
274 #endif
275 }