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();
17 void freefunction(void *ptr) {
19 free((struct structuremap *)ptr);
23 void freetypemap(struct typemap * ptr) {
24 rbdestroy(ptr->typetree,freefunction);
25 rbdestroy(ptr->alloctree,freefunction);
29 void typemapreset(struct typemap *ptr) {
30 rbdestroy(ptr->typetree,freefunction);
31 ptr->typetree=rbinit();
32 if (ptr->low<ptr->high)
33 rbdelete(ptr->low,ptr->alloctree);
35 rbdelete(ptr->high,ptr->alloctree);
38 void initializetypemapstack(struct typemap * ptr, void *high) {
40 if (ptr->low<ptr->high)
41 rbinsert(ptr->low,ptr->high,NULL,ptr->alloctree);
43 rbinsert(ptr->high,ptr->low,NULL,ptr->alloctree);
46 struct structuremap * allocatestructuremap(int s) {
47 struct structuremap *ptr=(struct structuremap *)malloc(sizeof(struct structuremap));
49 ptr->typetree=rbinit();
53 void freestructuremap(struct structuremap *ptr) {
54 rbdestroy(ptr->typetree,freefunction);
58 bool typemapasserttype(struct typemap *thisvar, void *ptr, int s) {
59 int toadd=sizeBytes(s);
60 return typemapasserttypeB(thisvar, ptr,((char *) ptr)+toadd,s);
63 bool typemapasserttypeB(struct typemap * thisvar, void *ptr, void *high, int s) {
65 bool b=typemapchecktype(thisvar,true,ptr,s);
67 printf("Assertion failure\n");
69 bool testb=typemapchecktype(thisvar,true,ptr,s);
74 return typemapassertvalidmemoryB(thisvar,ptr, high);
77 bool typemapassertvalidmemory(struct typemap * thisvar, void* low, int s) {
78 int toadd=sizeBytes(s);
79 return typemapassertvalidmemoryB(thisvar, low,((char *)low)+toadd);
82 bool typemapassertvalidmemoryB(struct typemap * thisvar, void* low, void* high) {
84 return typemapcheckmemory(thisvar, low, high);
89 bool typemapistype(struct typemap *thisvar, void *ptr, void *high, int s) {
91 bool b=typemapchecktype(thisvar, false,ptr,s);
93 printf("Verify failure\n");
95 bool testb=typemapchecktype(thisvar, false,ptr,s);
100 return typemapassertvalidmemoryB(thisvar, ptr, high);
103 void typemapallocate(struct typemap *thisvar,void *ptr, int size) {
105 void *high=((char *)ptr)+size;
106 int val=rbinsert(low,high,NULL,thisvar->alloctree);
111 inline int sizeinbytes(unsigned int bits) {
118 int typemapfindoffsetstructure(struct typemap * thisvar, int s, int offset) {
122 for(i=0;i<getnumfields(s);i++) {
124 int ttype=getfield(s,i);
126 mult=numElements(s,i);
128 increment=size(ttype);
130 int delt=offset-count;
131 int byteincrement=increment/8;
132 if (delt<mult*byteincrement) {
133 if (delt%byteincrement==0) {
139 if ((count+sizeinbytes(mult*increment))>offset)
142 count+=sizeinbytes(mult*increment);
147 void typemapdeallocate(struct typemap * thisvar,void *ptr) {
148 if (rbdelete(ptr,thisvar->alloctree)==NULL)
149 printf("Freeing unallocated memory\n");
152 bool typemapcheckmemory(struct typemap *thisvar, void* low, void* high) {
153 struct pair allocp=rbfind(low,high,thisvar->alloctree);
154 if (allocp.low == NULL) {
156 } else if ((allocp.low > low) || (allocp.high < high)) { /* make sure this block is used */
164 bool typemapchecktype(struct typemap *thisvar, bool doaction,void *ptr, int structure) {
165 int ssize=sizeBytes(structure);
167 void *high=((char *)low)+ssize;
168 struct pair allocp=rbfind(low,high,thisvar->alloctree);
169 if (allocp.low==NULL)
171 if (allocp.low>low||allocp.high<high) /* make sure this block is used */
174 struct pair typep=rbfind(low,high,thisvar->typetree);
175 struct structuremap *smap=(struct structuremap *)rblookup(low,high,thisvar->typetree);
176 if (typep.low==NULL) {
180 struct structuremap *sm=allocatestructuremap(structure);
181 int flag=rbinsert(low, high, sm, thisvar->typetree);
183 printf("Error in asserttype\n");
189 return typemapchecktypeB(thisvar, doaction, low,high, structure, thisvar->typetree);
193 bool typemapchecktypeB(struct typemap *thisvar, bool doaction, void *low, void *high, int structure, struct rbtree *ttree) {
194 struct pair typep=rbfind(low,high,ttree);
195 struct structuremap *smap=(struct structuremap *)rblookup(low,high,ttree);
196 if (typep.low==low&&typep.high==high) {
198 if (issubtype(structure,smap->str)) {
204 } else if (issubtype(smap->str,structure)) {
208 return false; /* incompatible types */
209 } else if (typep.low<=low&&typep.high>=high) {
210 /* See if it matches up with structure inside typep */
211 if (rbsearch(low,high,smap->typetree)) {
213 return typemapchecktypeB(thisvar,doaction,low,high, structure, smap->typetree);
215 /* check to see if data lines up correctly */
216 int offset=((char *)low)-((char *)typep.low);
217 int st=typemapfindoffsetstructure(thisvar, smap->str,offset);
220 if (issubtype(structure,st)) {
224 struct structuremap *newsm=allocatestructuremap(structure);
225 int flag=rbinsert(low, high, newsm, smap->typetree);
228 } else if (issubtype(st,structure)) {
232 struct structuremap *newsm=allocatestructuremap(st);
233 int flag=rbinsert(low, high, newsm, smap->typetree);