7 #ifndef COMMANDLINEFLAGS
12 struct typemap * allocatetypemap() {
13 struct typemap *thisvar=(struct typemap *) malloc(sizeof(struct typemap));
14 thisvar->alloctree=rbinit();
15 thisvar->typetree=rbinit();
16 thisvar->low=GC_linux_stack_base();
20 void freefunction(void *ptr) {
22 freestructuremap((struct structuremap *)ptr);
26 void freetypemap(struct typemap * ptr) {
27 rbdestroy(ptr->typetree,freefunction);
28 rbdestroy(ptr->alloctree,freefunction);
32 void typemapreset(struct typemap *ptr) {
33 rbdestroy(ptr->typetree,freefunction);
34 ptr->typetree=rbinit();
35 if (ptr->low<ptr->high)
36 rbdelete(ptr->low,ptr->alloctree);
38 rbdelete(ptr->high,ptr->alloctree);
41 void initializetypemapstack(struct typemap * ptr, void *high) {
43 if (ptr->low<ptr->high)
44 rbinsert(ptr->low,ptr->high,NULL,ptr->alloctree);
46 rbinsert(ptr->high,ptr->low,NULL,ptr->alloctree);
49 struct structuremap * allocatestructuremap(int s) {
50 struct structuremap *ptr=(struct structuremap *)malloc(sizeof(struct structuremap));
52 ptr->typetree=rbinit();
56 void freestructuremap(struct structuremap *ptr) {
57 rbdestroy(ptr->typetree,freefunction);
61 bool typemapasserttype(struct typemap *thisvar, void *ptr, int s) {
62 int toadd=sizeBytes(s);
63 return typemapasserttypeB(thisvar, ptr,((char *) ptr)+toadd,s);
66 bool typemapasserttypeB(struct typemap * thisvar, void *ptr, void *high, int s) {
68 bool b=typemapchecktype(thisvar,true,ptr,s);
70 printf("Assertion failure\n");
72 bool testb=typemapchecktype(thisvar,true,ptr,s);
77 return typemapassertvalidmemoryB(thisvar,ptr, high);
80 bool typemapassertvalidmemory(struct typemap * thisvar, void* low, int s) {
81 int toadd=sizeBytes(s);
82 return typemapassertvalidmemoryB(thisvar, low,((char *)low)+toadd);
85 bool typemapassertexactmemory(struct typemap * thisvar, void* low, int s) {
86 int toadd=sizeBytes(s);
89 void * high=((char *)low)+toadd;
90 struct pair allocp=rbfind(low,high,thisvar->alloctree);
91 if (allocp.low == NULL) {
93 } else if ((allocp.low != low) || (allocp.high != high)) {
94 /* make sure this block exactly lines up */
105 bool typemapassertvalidmemoryB(struct typemap * thisvar, void* low, void* high) {
107 return typemapcheckmemory(thisvar, low, high);
112 bool typemapistype(struct typemap *thisvar, void *ptr, void *high, int s) {
114 bool b=typemapchecktype(thisvar, false,ptr,s);
116 printf("Verify failure\n");
118 bool testb=typemapchecktype(thisvar, false,ptr,s);
123 return typemapassertvalidmemoryB(thisvar, ptr, high);
126 void typemapallocate(struct typemap *thisvar,void *ptr, int size) {
128 void *high=((char *)ptr)+size;
129 int val=rbinsert(low,high,NULL,thisvar->alloctree);
134 inline int sizeinbytes(unsigned int bits) {
141 int typemapfindoffsetstructure(struct typemap * thisvar, int s, int offset) {
145 for(i=0;i<getnumfields(s);i++) {
147 int ttype=getfield(s,i);
149 mult=numElements(s,i);
151 increment=size(ttype);
153 int delt=offset-count;
154 int byteincrement=increment/8;
155 if (delt<mult*byteincrement) {
156 if (delt%byteincrement==0) {
162 if ((count+sizeinbytes(mult*increment))>offset)
165 count+=sizeinbytes(mult*increment);
170 void typemapdeallocate(struct typemap * thisvar,void *ptr) {
171 if (rbdelete(ptr,thisvar->alloctree)==NULL)
172 printf("Freeing unallocated memory\n");
175 bool typemapcheckmemory(struct typemap *thisvar, void* low, void* high) {
176 struct pair allocp=rbfind(low,high,thisvar->alloctree);
177 if (allocp.low == NULL) {
179 } else if ((allocp.low > low) || (allocp.high < high)) { /* make sure this block is used */
186 void * typemapgetendofblock(struct typemap *thisvar, void* low) {
187 struct pair allocp=rbfind(low,((char*)low)+1,thisvar->alloctree);
188 if (allocp.low == NULL) {
190 } else if ((allocp.low > low)||(allocp.high <= allocp.low)) { /* make sure this block is used */
193 return (void *)allocp.high;
198 bool typemapchecktype(struct typemap *thisvar, bool doaction,void *ptr, int structure) {
199 int ssize=sizeBytes(structure);
201 void *high=((char *)low)+ssize;
202 struct pair allocp=rbfind(low,high,thisvar->alloctree);
203 if (allocp.low==NULL)
205 if (allocp.low>low||allocp.high<high) /* make sure this block is used */
208 struct pair typep=rbfind(low,high,thisvar->typetree);
209 struct structuremap *smap=(struct structuremap *)rblookup(low,high,thisvar->typetree);
210 if (typep.low==NULL) {
214 struct structuremap *sm=allocatestructuremap(structure);
215 int flag=rbinsert(low, high, sm, thisvar->typetree);
217 printf("Error in asserttype\n");
223 return typemapchecktypeB(thisvar, doaction, low,high, structure, thisvar->typetree);
227 bool typemapchecktypeB(struct typemap *thisvar, bool doaction, void *low, void *high, int structure, struct rbtree *ttree) {
228 struct pair typep=rbfind(low,high,ttree);
229 struct structuremap *smap=(struct structuremap *)rblookup(low,high,ttree);
230 if (typep.low==low&&typep.high==high) {
232 if (issubtype(structure,smap->str)) {
238 } else if (issubtype(smap->str,structure)) {
242 return false; /* incompatible types */
243 } else if (typep.low<=low&&typep.high>=high) {
244 /* See if it matches up with structure inside typep */
245 if (rbsearch(low,high,smap->typetree)) {
247 return typemapchecktypeB(thisvar,doaction,low,high, structure, smap->typetree);
249 /* check to see if data lines up correctly */
250 int offset=((char *)low)-((char *)typep.low);
251 int st=typemapfindoffsetstructure(thisvar, smap->str,offset);
254 if (issubtype(structure,st)) {
258 struct structuremap *newsm=allocatestructuremap(structure);
259 int flag=rbinsert(low, high, newsm, smap->typetree);
262 } else if (issubtype(st,structure)) {
266 struct structuremap *newsm=allocatestructuremap(st);
267 int flag=rbinsert(low, high, newsm, smap->typetree);