10 struct typemap * allocatetypemap() {
11 struct typemap *thisvar=(struct typemap *) malloc(sizeof(struct typemap));
12 thisvar->alloctree=rbinit();
13 thisvar->typetree=rbinit();
14 thisvar->low=GC_linux_stack_base();
18 void freefunction(void *ptr) {
20 freestructuremap((struct structuremap *)ptr);
24 void freetypemap(struct typemap * ptr) {
25 rbdestroy(ptr->typetree,freefunction);
26 rbdestroy(ptr->alloctree,freefunction);
30 void typemapreset(struct typemap *ptr) {
31 rbdestroy(ptr->typetree,freefunction);
32 ptr->typetree=rbinit();
33 if (ptr->low<ptr->high)
34 rbdelete(ptr->low,ptr->alloctree);
36 rbdelete(ptr->high,ptr->alloctree);
39 void initializetypemapstack(struct typemap * ptr, void *high) {
41 if (ptr->low<ptr->high)
42 rbinsert(ptr->low,ptr->high,NULL,ptr->alloctree);
44 rbinsert(ptr->high,ptr->low,NULL,ptr->alloctree);
47 struct structuremap * allocatestructuremap(int s) {
48 struct structuremap *ptr=(struct structuremap *)malloc(sizeof(struct structuremap));
50 ptr->typetree=rbinit();
54 void freestructuremap(struct structuremap *ptr) {
55 rbdestroy(ptr->typetree,freefunction);
59 bool typemapasserttype(struct typemap *thisvar, void *ptr, int s) {
60 int toadd=sizeBytes(s);
61 return typemapasserttypeB(thisvar, ptr,((char *) ptr)+toadd,s);
64 bool typemapasserttypeB(struct typemap * thisvar, void *ptr, void *high, int s) {
66 bool b=typemapchecktype(thisvar,true,ptr,s);
68 printf("Assertion failure\n");
70 bool testb=typemapchecktype(thisvar,true,ptr,s);
75 return typemapassertvalidmemoryB(thisvar,ptr, high);
78 bool typemapassertvalidmemory(struct typemap * thisvar, void* low, int s) {
79 int toadd=sizeBytes(s);
80 return typemapassertvalidmemoryB(thisvar, low,((char *)low)+toadd);
83 bool typemapassertexactmemory(struct typemap * thisvar, void* low, int s) {
84 int toadd=sizeBytes(s);
87 void * high=((char *)low)+toadd;
88 struct pair allocp=rbfind(low,high,thisvar->alloctree);
89 if (allocp.low == NULL) {
91 } else if ((allocp.low != low) || (allocp.high != high)) {
92 /* make sure this block exactly lines up */
103 bool typemapassertvalidmemoryB(struct typemap * thisvar, void* low, void* high) {
105 return typemapcheckmemory(thisvar, low, high);
110 bool typemapistype(struct typemap *thisvar, void *ptr, void *high, int s) {
112 bool b=typemapchecktype(thisvar, false,ptr,s);
114 printf("Verify failure\n");
116 bool testb=typemapchecktype(thisvar, false,ptr,s);
121 return typemapassertvalidmemoryB(thisvar, ptr, high);
124 void typemapallocate(struct typemap *thisvar,void *ptr, int size) {
126 void *high=((char *)ptr)+size;
127 int val=rbinsert(low,high,NULL,thisvar->alloctree);
132 inline int sizeinbytes(unsigned int bits) {
139 int typemapfindoffsetstructure(struct typemap * thisvar, int s, int offset) {
143 for(i=0;i<getnumfields(s);i++) {
145 int ttype=getfield(s,i);
147 mult=numElements(s,i);
149 increment=size(ttype);
151 int delt=offset-count;
152 int byteincrement=increment/8;
153 if (delt<mult*byteincrement) {
154 if (delt%byteincrement==0) {
160 if ((count+sizeinbytes(mult*increment))>offset)
163 count+=sizeinbytes(mult*increment);
168 void typemapdeallocate(struct typemap * thisvar,void *ptr) {
169 if (rbdelete(ptr,thisvar->alloctree)==NULL)
170 printf("Freeing unallocated memory\n");
173 bool typemapcheckmemory(struct typemap *thisvar, void* low, void* high) {
174 struct pair allocp=rbfind(low,high,thisvar->alloctree);
175 if (allocp.low == NULL) {
177 } else if ((allocp.low > low) || (allocp.high < high)) { /* make sure this block is used */
184 void * typemapgetendofblock(struct typemap *thisvar, void* low) {
185 struct pair allocp=rbfind(low,((char*)low)+1,thisvar->alloctree);
186 if (allocp.low == NULL) {
188 } else if ((allocp.low > low)||(allocp.high <= allocp.low)) { /* make sure this block is used */
191 return (void *)allocp.high;
196 bool typemapchecktype(struct typemap *thisvar, bool doaction,void *ptr, int structure) {
197 int ssize=sizeBytes(structure);
199 void *high=((char *)low)+ssize;
200 struct pair allocp=rbfind(low,high,thisvar->alloctree);
201 if (allocp.low==NULL)
203 if (allocp.low>low||allocp.high<high) /* make sure this block is used */
206 struct pair typep=rbfind(low,high,thisvar->typetree);
207 struct structuremap *smap=(struct structuremap *)rblookup(low,high,thisvar->typetree);
208 if (typep.low==NULL) {
212 struct structuremap *sm=allocatestructuremap(structure);
213 int flag=rbinsert(low, high, sm, thisvar->typetree);
215 printf("Error in asserttype\n");
221 return typemapchecktypeB(thisvar, doaction, low,high, structure, thisvar->typetree);
225 bool typemapchecktypeB(struct typemap *thisvar, bool doaction, void *low, void *high, int structure, struct rbtree *ttree) {
226 struct pair typep=rbfind(low,high,ttree);
227 struct structuremap *smap=(struct structuremap *)rblookup(low,high,ttree);
228 if (typep.low==low&&typep.high==high) {
230 if (issubtype(structure,smap->str)) {
236 } else if (issubtype(smap->str,structure)) {
240 return false; /* incompatible types */
241 } else if (typep.low<=low&&typep.high>=high) {
242 /* See if it matches up with structure inside typep */
243 if (rbsearch(low,high,smap->typetree)) {
245 return typemapchecktypeB(thisvar,doaction,low,high, structure, smap->typetree);
247 /* check to see if data lines up correctly */
248 int offset=((char *)low)-((char *)typep.low);
249 int st=typemapfindoffsetstructure(thisvar, smap->str,offset);
252 if (issubtype(structure,st)) {
256 struct structuremap *newsm=allocatestructuremap(structure);
257 int flag=rbinsert(low, high, newsm, smap->typetree);
260 } else if (issubtype(st,structure)) {
264 struct structuremap *newsm=allocatestructuremap(st);
265 int flag=rbinsert(low, high, newsm, smap->typetree);