12 typemap::typemap(typeobject * size) {
16 this->low=GC_linux_stack_base();
19 void freefunction(void *ptr) {
21 delete((structuremap *)ptr);
26 rbdestroy(typetree,freefunction);
27 rbdestroy(alloctree,freefunction);
30 void typemap::reset() {
31 rbdestroy(typetree,freefunction);
34 rbdelete(low,alloctree);
36 rbdelete(high,alloctree);
39 void typemap::initializestack(void *high) {
42 rbinsert(low,high,NULL,alloctree);
44 rbinsert(high,low,NULL,alloctree);
47 structuremap::structuremap(int s) {
52 structuremap::~structuremap() {
53 rbdestroy(typetree,freefunction);
56 bool typemap::asserttype(void *ptr, int s) {
57 int toadd=size->sizeBytes(s);
58 return asserttype(ptr,((char *) ptr)+toadd,s);
61 bool typemap::asserttype(void *ptr, void *high, int s) {
63 bool b=checktype(true,ptr,s);
65 printf("Assertion failure\n");
66 bool testb=checktype(true,ptr,s);
70 return assertvalidmemory(ptr, high);
73 bool typemap::assertvalidmemory(void* low, int s) {
74 int toadd=size->sizeBytes(s);
75 return assertvalidmemory(low,((char *)low)+toadd);
78 bool typemap::assertvalidmemory(void* low, void* high) {
80 return checkmemory(low, high);
85 bool typemap::istype(void *ptr, void *high, int s) {
87 bool b=checktype(false,ptr,s);
89 printf("Verify failure\n");
90 bool testb=checktype(false,ptr,s);
94 return assertvalidmemory(ptr, high);
97 void typemap::allocate(void *ptr, int size) {
99 void *high=((char *)ptr)+size;
100 int val=rbinsert(low,high,NULL,alloctree);
105 inline int sizeinbytes(unsigned int bits) {
112 int typemap::findoffsetstructure(int s, int offset) {
114 for(int i=0;i<size->getnumfields(s);i++) {
116 int ttype=size->getfield(s,i);
117 if (size->isArray(s,i)) {
118 mult=size->numElements(s,i);
120 int increment=size->size(ttype);
122 int delt=offset-count;
123 int byteincrement=increment/8;
124 if (delt<mult*byteincrement) {
125 if (delt%byteincrement==0) {
131 if ((count+sizeinbytes(mult*increment))>offset)
134 count+=sizeinbytes(mult*increment);
139 void typemap::deallocate(void *ptr) {
140 if (rbdelete(ptr,alloctree)==NULL)
141 printf("Freeing unallocated memory\n");
144 bool typemap::checkmemory(void* low, void* high) {
145 struct pair allocp=rbfind(low,high,alloctree);
146 if (allocp.low == NULL) {
148 } else if ((allocp.low > low) || (allocp.high < high)) { /* make sure this block is used */
156 bool typemap::checktype(bool doaction,void *ptr, int structure) {
157 int ssize=size->sizeBytes(structure);
159 void *high=((char *)low)+ssize;
160 struct pair allocp=rbfind(low,high,alloctree);
161 if (allocp.low==NULL)
163 if (allocp.low>low||allocp.high<high) /* make sure this block is used */
165 struct pair typep=rbfind(low,high,typetree);
166 structuremap *smap=(structuremap *)rblookup(low,high,typetree);
167 if (typep.low==NULL) {
170 structuremap *sm=new structuremap(structure);
171 int flag=rbinsert(low, high, sm, typetree);
173 printf("Error in asserttype\n");
178 return checktype(doaction, low,high, structure, typetree);
181 bool typemap::checktype(bool doaction, void *low, void *high, int structure, struct rbtree *ttree) {
182 struct pair typep=rbfind(low,high,ttree);
183 structuremap *smap=(structuremap *)rblookup(low,high,ttree);
184 if (typep.low==low&&typep.high==high) {
186 if (size->issubtype(structure,smap->str)) {
192 } else if (size->issubtype(smap->str,structure)) {
196 return false; /* incompatible types */
197 } else if (typep.low<=low&&typep.high>=high) {
198 /* See if it matches up with structure inside typep */
199 if (rbsearch(low,high,smap->typetree)) {
201 return checktype(doaction,low,high, structure, smap->typetree);
203 /* check to see if data lines up correctly */
204 int offset=((char *)low)-((char *)typep.low);
205 int st=findoffsetstructure(smap->str,offset);
208 if (size->issubtype(structure,st)) {
211 structuremap *newsm=new structuremap(structure);
212 int flag=rbinsert(low, high, newsm, smap->typetree);
214 } else if (size->issubtype(st,structure)) {
217 structuremap *newsm=new structuremap(st);
218 int flag=rbinsert(low, high, newsm, smap->typetree);