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 typemapassertvalidmemoryB(struct typemap * thisvar, void* low, void* high) {
85 return typemapcheckmemory(thisvar, low, high);
90 bool typemapistype(struct typemap *thisvar, void *ptr, void *high, int s) {
92 bool b=typemapchecktype(thisvar, false,ptr,s);
94 printf("Verify failure\n");
96 bool testb=typemapchecktype(thisvar, false,ptr,s);
101 return typemapassertvalidmemoryB(thisvar, ptr, high);
104 void typemapallocate(struct typemap *thisvar,void *ptr, int size) {
106 void *high=((char *)ptr)+size;
107 int val=rbinsert(low,high,NULL,thisvar->alloctree);
112 inline int sizeinbytes(unsigned int bits) {
119 int typemapfindoffsetstructure(struct typemap * thisvar, int s, int offset) {
123 for(i=0;i<getnumfields(s);i++) {
125 int ttype=getfield(s,i);
127 mult=numElements(s,i);
129 increment=size(ttype);
131 int delt=offset-count;
132 int byteincrement=increment/8;
133 if (delt<mult*byteincrement) {
134 if (delt%byteincrement==0) {
140 if ((count+sizeinbytes(mult*increment))>offset)
143 count+=sizeinbytes(mult*increment);
148 void typemapdeallocate(struct typemap * thisvar,void *ptr) {
149 if (rbdelete(ptr,thisvar->alloctree)==NULL)
150 printf("Freeing unallocated memory\n");
153 bool typemapcheckmemory(struct typemap *thisvar, void* low, void* high) {
154 struct pair allocp=rbfind(low,high,thisvar->alloctree);
155 if (allocp.low == NULL) {
157 } else if ((allocp.low > low) || (allocp.high < high)) { /* make sure this block is used */
165 bool typemapchecktype(struct typemap *thisvar, bool doaction,void *ptr, int structure) {
166 int ssize=sizeBytes(structure);
168 void *high=((char *)low)+ssize;
169 struct pair allocp=rbfind(low,high,thisvar->alloctree);
170 if (allocp.low==NULL)
172 if (allocp.low>low||allocp.high<high) /* make sure this block is used */
175 struct pair typep=rbfind(low,high,thisvar->typetree);
176 struct structuremap *smap=(struct structuremap *)rblookup(low,high,thisvar->typetree);
177 if (typep.low==NULL) {
181 struct structuremap *sm=allocatestructuremap(structure);
182 int flag=rbinsert(low, high, sm, thisvar->typetree);
184 printf("Error in asserttype\n");
190 return typemapchecktypeB(thisvar, doaction, low,high, structure, thisvar->typetree);
194 bool typemapchecktypeB(struct typemap *thisvar, bool doaction, void *low, void *high, int structure, struct rbtree *ttree) {
195 struct pair typep=rbfind(low,high,ttree);
196 struct structuremap *smap=(struct structuremap *)rblookup(low,high,ttree);
197 if (typep.low==low&&typep.high==high) {
199 if (issubtype(structure,smap->str)) {
205 } else if (issubtype(smap->str,structure)) {
209 return false; /* incompatible types */
210 } else if (typep.low<=low&&typep.high>=high) {
211 /* See if it matches up with structure inside typep */
212 if (rbsearch(low,high,smap->typetree)) {
214 return typemapchecktypeB(thisvar,doaction,low,high, structure, smap->typetree);
216 /* check to see if data lines up correctly */
217 int offset=((char *)low)-((char *)typep.low);
218 int st=typemapfindoffsetstructure(thisvar, smap->str,offset);
221 if (issubtype(structure,st)) {
225 struct structuremap *newsm=allocatestructuremap(structure);
226 int flag=rbinsert(low, high, newsm, smap->typetree);
229 } else if (issubtype(st,structure)) {
233 struct structuremap *newsm=allocatestructuremap(st);
234 int flag=rbinsert(low, high, newsm, smap->typetree);