7 #ifndef COMMANDLINEFLAGS
12 struct typemap * allocatetypemap() {
13 #ifdef CHECKTYPE||CHECKMEMORY
14 struct typemap *thisvar=(struct typemap *) malloc(sizeof(struct typemap));
15 thisvar->alloctree=rbinit();
16 thisvar->typetree=rbinit();
17 thisvar->low=GC_linux_stack_base();
24 void freefunction(void *ptr) {
26 freestructuremap((struct structuremap *)ptr);
30 void freetypemap(struct typemap * ptr) {
31 rbdestroy(ptr->typetree,freefunction);
32 rbdestroy(ptr->alloctree,freefunction);
36 void typemapreset(struct typemap *ptr) {
37 #ifdef CHECKTYPE||CHECKMEMORY
38 rbdestroy(ptr->typetree,freefunction);
39 ptr->typetree=rbinit();
40 if (ptr->low<ptr->high)
41 rbdelete(ptr->low,ptr->alloctree);
43 rbdelete(ptr->high,ptr->alloctree);
47 void initializetypemapstack(struct typemap * ptr, void *high) {
48 #ifdef CHECKTYPE||CHECKMEMORY
50 if (ptr->low<ptr->high)
51 rbinsert(ptr->low,ptr->high,NULL,ptr->alloctree);
53 rbinsert(ptr->high,ptr->low,NULL,ptr->alloctree);
57 struct structuremap * allocatestructuremap(int s) {
58 struct structuremap *ptr=(struct structuremap *)malloc(sizeof(struct structuremap));
60 ptr->typetree=rbinit();
64 void freestructuremap(struct structuremap *ptr) {
65 rbdestroy(ptr->typetree,freefunction);
69 bool typemapasserttype(struct typemap *thisvar, void *ptr, int s) {
70 int toadd=sizeBytes(s);
71 return typemapasserttypeB(thisvar, ptr,((char *) ptr)+toadd,s);
74 bool typemapasserttypeB(struct typemap * thisvar, void *ptr, void *high, int s) {
76 bool b=typemapchecktype(thisvar,true,ptr,s);
78 printf("Assertion failure\n");
80 bool testb=typemapchecktype(thisvar,true,ptr,s);
85 return typemapassertvalidmemoryB(thisvar,ptr, high);
88 bool typemapassertvalidmemory(struct typemap * thisvar, void* low, int s) {
89 int toadd=sizeBytes(s);
90 return typemapassertvalidmemoryB(thisvar, low,((char *)low)+toadd);
93 bool typemapassertexactmemory(struct typemap * thisvar, void* low, int s) {
94 int toadd=sizeBytes(s);
97 void * high=((char *)low)+toadd;
98 struct pair allocp=rbfind(low,high,thisvar->alloctree);
99 if (allocp.low == NULL) {
101 } else if ((allocp.low != low) || (allocp.high != high)) {
102 /* make sure this block exactly lines up */
113 bool typemapassertvalidmemoryB(struct typemap * thisvar, void* low, void* high) {
115 return typemapcheckmemory(thisvar, low, high);
120 bool typemapistype(struct typemap *thisvar, void *ptr, void *high, int s) {
122 bool b=typemapchecktype(thisvar, false,ptr,s);
124 printf("Verify failure\n");
126 bool testb=typemapchecktype(thisvar, false,ptr,s);
131 return typemapassertvalidmemoryB(thisvar, ptr, high);
134 void typemapallocate(struct typemap *thisvar,void *ptr, int size) {
136 void *high=((char *)ptr)+size;
137 int val=rbinsert(low,high,NULL,thisvar->alloctree);
142 inline int sizeinbytes(unsigned int bits) {
149 int typemapfindoffsetstructure(struct typemap * thisvar, int s, int offset) {
153 for(i=0;i<getnumfields(s);i++) {
155 int ttype=getfield(s,i);
157 mult=numElements(s,i);
159 increment=size(ttype);
161 int delt=offset-count;
162 int byteincrement=increment/8;
163 if (delt<mult*byteincrement) {
164 if (delt%byteincrement==0) {
170 if ((count+sizeinbytes(mult*increment))>offset)
173 count+=sizeinbytes(mult*increment);
178 void typemapdeallocate(struct typemap * thisvar,void *ptr) {
179 if (rbdelete(ptr,thisvar->alloctree)==NULL)
180 printf("Freeing unallocated memory\n");
183 bool typemapcheckmemory(struct typemap *thisvar, void* low, void* high) {
184 struct pair allocp=rbfind(low,high,thisvar->alloctree);
185 if (allocp.low == NULL) {
187 } else if ((allocp.low > low) || (allocp.high < high)) { /* make sure this block is used */
194 void * typemapgetendofblock(struct typemap *thisvar, void* low) {
195 struct pair allocp=rbfind(low,((char*)low)+1,thisvar->alloctree);
196 if (allocp.low == NULL) {
198 } else if ((allocp.low > low)||(allocp.high <= allocp.low)) { /* make sure this block is used */
201 return (void *)allocp.high;
206 bool typemapchecktype(struct typemap *thisvar, bool doaction,void *ptr, int structure) {
207 int ssize=sizeBytes(structure);
209 void *high=((char *)low)+ssize;
210 struct pair allocp=rbfind(low,high,thisvar->alloctree);
211 if (allocp.low==NULL)
213 if (allocp.low>low||allocp.high<high) /* make sure this block is used */
216 struct pair typep=rbfind(low,high,thisvar->typetree);
217 struct structuremap *smap=(struct structuremap *)rblookup(low,high,thisvar->typetree);
218 if (typep.low==NULL) {
222 struct structuremap *sm=allocatestructuremap(structure);
223 int flag=rbinsert(low, high, sm, thisvar->typetree);
225 printf("Error in asserttype\n");
231 return typemapchecktypeB(thisvar, doaction, low,high, structure, thisvar->typetree);
235 bool typemapchecktypeB(struct typemap *thisvar, bool doaction, void *low, void *high, int structure, struct rbtree *ttree) {
236 struct pair typep=rbfind(low,high,ttree);
237 struct structuremap *smap=(struct structuremap *)rblookup(low,high,ttree);
238 if (typep.low==low&&typep.high==high) {
240 if (issubtype(structure,smap->str)) {
246 } else if (issubtype(smap->str,structure)) {
250 return false; /* incompatible types */
251 } else if (typep.low<=low&&typep.high>=high) {
252 /* See if it matches up with structure inside typep */
253 if (rbsearch(low,high,smap->typetree)) {
255 return typemapchecktypeB(thisvar,doaction,low,high, structure, smap->typetree);
257 /* check to see if data lines up correctly */
258 int offset=((char *)low)-((char *)typep.low);
259 int st=typemapfindoffsetstructure(thisvar, smap->str,offset);
262 if (issubtype(structure,st)) {
266 struct structuremap *newsm=allocatestructuremap(structure);
267 int flag=rbinsert(low, high, newsm, smap->typetree);
270 } else if (issubtype(st,structure)) {
274 struct structuremap *newsm=allocatestructuremap(st);
275 int flag=rbinsert(low, high, newsm, smap->typetree);