7 #ifndef COMMANDLINEFLAGS
12 struct typemap * allocatetypemap() {
13 struct typemap *thisvar=(struct typemap *) malloc(sizeof(struct typemap));
14 #ifdef CHECKTYPE||CHECKMEMORY
15 thisvar->alloctree=rbinit();
16 thisvar->typetree=rbinit();
17 thisvar->low=GC_linux_stack_base();
22 void freefunction(void *ptr) {
24 freestructuremap((struct structuremap *)ptr);
28 void freetypemap(struct typemap * ptr) {
29 rbdestroy(ptr->typetree,freefunction);
30 rbdestroy(ptr->alloctree,freefunction);
34 void typemapreset(struct typemap *ptr) {
35 rbdestroy(ptr->typetree,freefunction);
36 ptr->typetree=rbinit();
37 if (ptr->low<ptr->high)
38 rbdelete(ptr->low,ptr->alloctree);
40 rbdelete(ptr->high,ptr->alloctree);
43 void initializetypemapstack(struct typemap * ptr, void *high) {
45 if (ptr->low<ptr->high)
46 rbinsert(ptr->low,ptr->high,NULL,ptr->alloctree);
48 rbinsert(ptr->high,ptr->low,NULL,ptr->alloctree);
51 struct structuremap * allocatestructuremap(int s) {
52 struct structuremap *ptr=(struct structuremap *)malloc(sizeof(struct structuremap));
54 ptr->typetree=rbinit();
58 void freestructuremap(struct structuremap *ptr) {
59 rbdestroy(ptr->typetree,freefunction);
63 bool typemapasserttype(struct typemap *thisvar, void *ptr, int s) {
64 int toadd=sizeBytes(s);
65 return typemapasserttypeB(thisvar, ptr,((char *) ptr)+toadd,s);
68 bool typemapasserttypeB(struct typemap * thisvar, void *ptr, void *high, int s) {
70 bool b=typemapchecktype(thisvar,true,ptr,s);
72 printf("Assertion failure\n");
74 bool testb=typemapchecktype(thisvar,true,ptr,s);
79 return typemapassertvalidmemoryB(thisvar,ptr, high);
82 bool typemapassertvalidmemory(struct typemap * thisvar, void* low, int s) {
83 int toadd=sizeBytes(s);
84 return typemapassertvalidmemoryB(thisvar, low,((char *)low)+toadd);
87 bool typemapassertexactmemory(struct typemap * thisvar, void* low, int s) {
88 int toadd=sizeBytes(s);
91 void * high=((char *)low)+toadd;
92 struct pair allocp=rbfind(low,high,thisvar->alloctree);
93 if (allocp.low == NULL) {
95 } else if ((allocp.low != low) || (allocp.high != high)) {
96 /* make sure this block exactly lines up */
107 bool typemapassertvalidmemoryB(struct typemap * thisvar, void* low, void* high) {
109 return typemapcheckmemory(thisvar, low, high);
114 bool typemapistype(struct typemap *thisvar, void *ptr, void *high, int s) {
116 bool b=typemapchecktype(thisvar, false,ptr,s);
118 printf("Verify failure\n");
120 bool testb=typemapchecktype(thisvar, false,ptr,s);
125 return typemapassertvalidmemoryB(thisvar, ptr, high);
128 void typemapallocate(struct typemap *thisvar,void *ptr, int size) {
130 void *high=((char *)ptr)+size;
131 int val=rbinsert(low,high,NULL,thisvar->alloctree);
136 inline int sizeinbytes(unsigned int bits) {
143 int typemapfindoffsetstructure(struct typemap * thisvar, int s, int offset) {
147 for(i=0;i<getnumfields(s);i++) {
149 int ttype=getfield(s,i);
151 mult=numElements(s,i);
153 increment=size(ttype);
155 int delt=offset-count;
156 int byteincrement=increment/8;
157 if (delt<mult*byteincrement) {
158 if (delt%byteincrement==0) {
164 if ((count+sizeinbytes(mult*increment))>offset)
167 count+=sizeinbytes(mult*increment);
172 void typemapdeallocate(struct typemap * thisvar,void *ptr) {
173 if (rbdelete(ptr,thisvar->alloctree)==NULL)
174 printf("Freeing unallocated memory\n");
177 bool typemapcheckmemory(struct typemap *thisvar, void* low, void* high) {
178 struct pair allocp=rbfind(low,high,thisvar->alloctree);
179 if (allocp.low == NULL) {
181 } else if ((allocp.low > low) || (allocp.high < high)) { /* make sure this block is used */
188 void * typemapgetendofblock(struct typemap *thisvar, void* low) {
189 struct pair allocp=rbfind(low,((char*)low)+1,thisvar->alloctree);
190 if (allocp.low == NULL) {
192 } else if ((allocp.low > low)||(allocp.high <= allocp.low)) { /* make sure this block is used */
195 return (void *)allocp.high;
200 bool typemapchecktype(struct typemap *thisvar, bool doaction,void *ptr, int structure) {
201 int ssize=sizeBytes(structure);
203 void *high=((char *)low)+ssize;
204 struct pair allocp=rbfind(low,high,thisvar->alloctree);
205 if (allocp.low==NULL)
207 if (allocp.low>low||allocp.high<high) /* make sure this block is used */
210 struct pair typep=rbfind(low,high,thisvar->typetree);
211 struct structuremap *smap=(struct structuremap *)rblookup(low,high,thisvar->typetree);
212 if (typep.low==NULL) {
216 struct structuremap *sm=allocatestructuremap(structure);
217 int flag=rbinsert(low, high, sm, thisvar->typetree);
219 printf("Error in asserttype\n");
225 return typemapchecktypeB(thisvar, doaction, low,high, structure, thisvar->typetree);
229 bool typemapchecktypeB(struct typemap *thisvar, bool doaction, void *low, void *high, int structure, struct rbtree *ttree) {
230 struct pair typep=rbfind(low,high,ttree);
231 struct structuremap *smap=(struct structuremap *)rblookup(low,high,ttree);
232 if (typep.low==low&&typep.high==high) {
234 if (issubtype(structure,smap->str)) {
240 } else if (issubtype(smap->str,structure)) {
244 return false; /* incompatible types */
245 } else if (typep.low<=low&&typep.high>=high) {
246 /* See if it matches up with structure inside typep */
247 if (rbsearch(low,high,smap->typetree)) {
249 return typemapchecktypeB(thisvar,doaction,low,high, structure, smap->typetree);
251 /* check to see if data lines up correctly */
252 int offset=((char *)low)-((char *)typep.low);
253 int st=typemapfindoffsetstructure(thisvar, smap->str,offset);
256 if (issubtype(structure,st)) {
260 struct structuremap *newsm=allocatestructuremap(structure);
261 int flag=rbinsert(low, high, newsm, smap->typetree);
264 } else if (issubtype(st,structure)) {
268 struct structuremap *newsm=allocatestructuremap(st);
269 int flag=rbinsert(low, high, newsm, smap->typetree);