8 #include "processabstract.h"
14 typemap::typemap(model *m) {
20 void freefunction(void *ptr) {
22 delete((structuremap *)ptr);
27 rbdestroy(typetree,freefunction);
28 rbdestroy(alloctree,freefunction);
31 void typemap::reset() {
32 rbdestroy(typetree,freefunction);
36 structuremap::structuremap(structure *s) {
41 structuremap::~structuremap() {
42 rbdestroy(typetree,freefunction);
45 bool typemap::asserttype(void *ptr, structure *s) {
48 bool b=checktype(true,ptr,s);
50 printf("Assertion failure\n");
51 bool testb=checktype(true,ptr,s);
56 return assertvalidmemory(ptr, s);
59 bool typemap::assertvalidmemory(void* low, void* high) {
62 return checkmemory(low, high);
68 bool typemap::assertvalidmemory(void* ptr, structure* s) {
71 int size=s->getsize(globalmodel->getbitreader(),globalmodel,globalmodel->gethashtable());
73 void *high=((char *)low)+size;
74 return checkmemory(low, high);
80 bool typemap::istype(void *ptr, structure *s) {
83 bool b=checktype(false,ptr,s);
85 printf("Verify failure\n");
86 bool testb=checktype(false,ptr,s);
91 return assertvalidmemory(ptr, s);
94 void typemap::allocate(void *ptr, int size) {
96 void *high=((char *)ptr)+size;
97 int val=rbinsert(low,high,NULL,alloctree);
102 structure * typemap::findoffsetstructure(structure *s, int offset) {
104 for(int i=0;i<s->getnumfields();i++) {
106 ttype *ttype=s->getfield(i)->gettype();
107 if (ttype->getsize()!=NULL) {
108 Element * number=evaluateexpr(globalmodel,ttype->getsize(),globalmodel->gethashtable(),true,false);
109 mult=number->intvalue();
112 int increment=ttype->basesize(globalmodel->getbitreader(),globalmodel,globalmodel->gethashtable());
114 int delt=offset-count;
115 if (delt<mult*increment) {
116 if (delt%increment==0) {
117 return globalmodel->getstructure(ttype->getname());
122 count+=mult*increment;
128 void typemap::deallocate(void *ptr) {
129 if (rbdelete(ptr,alloctree)==NULL)
130 printf("Freeing unallocated memory\n");
133 bool typemap::checkmemory(void* low, void* high) {
134 struct pair allocp=rbfind(low,high,alloctree);
136 if (allocp.low == NULL) {
138 } else if ((allocp.low > low) || (allocp.high < high)) { /* make sure this block is used */
146 bool typemap::checktype(bool doaction,void *ptr, structure *structure) {
147 int size=structure->getsize(globalmodel->getbitreader(),globalmodel,globalmodel->gethashtable());
149 void *high=((char *)low)+size;
150 struct pair allocp=rbfind(low,high,alloctree);
151 if (allocp.low==NULL)
153 if (allocp.low>low||allocp.high<high) /* make sure this block is used */
155 struct pair typep=rbfind(low,high,typetree);
156 structuremap *smap=(structuremap *)rblookup(low,high,typetree);
158 if (typep.low==NULL) {
161 structuremap *sm=new structuremap(structure);
162 int flag=rbinsert(low, high, sm, typetree);
164 printf("Error in asserttype\n");
170 return checktype(doaction, low,high, structure, typetree);
173 bool typemap::checktype(bool doaction, void *low, void *high,structure *structre, struct rbtree *ttree) {
174 struct pair typep=rbfind(low,high,ttree);
175 structuremap *smap=(structuremap *)rblookup(low,high,ttree);
177 if (typep.low==low&&typep.high==high) {
179 if (globalmodel->subtypeof(structre,smap->str)) {
185 } else if (globalmodel->subtypeof(smap->str,structre)) {
189 return false; /* incompatible types */
190 } else if (typep.low<=low&&typep.high>=high) {
191 /* See if it matches up with structure inside typep */
192 if (rbsearch(low,high,smap->typetree)) {
194 return checktype(doaction,low,high, structre, smap->typetree);
196 /* check to see if data lines up correctly */
197 int offset=((char *)low)-((char *)typep.low);
198 structure * st=findoffsetstructure(smap->str,offset);
201 if (globalmodel->subtypeof(structre,st)) {
204 structuremap *newsm=new structuremap(structre);
205 int flag=rbinsert(low, high, newsm, smap->typetree);
207 } else if (globalmodel->subtypeof(st,structre)) {
210 structuremap *newsm=new structuremap(st);
211 int flag=rbinsert(low, high, newsm, smap->typetree);