5 #include "libredblack/redblack.h"
11 typemap::typemap(typeobject * size) {
17 void freefunction(void *ptr) {
19 delete((structuremap *)ptr);
24 rbdestroy(typetree,freefunction);
25 rbdestroy(alloctree,freefunction);
28 void typemap::reset() {
29 rbdestroy(typetree,freefunction);
34 structuremap::structuremap(int s) {
39 structuremap::~structuremap() {
40 rbdestroy(typetree,freefunction);
43 bool typemap::asserttype(void *ptr, void *high, int s) {
45 bool b=checktype(true,ptr,s);
47 printf("Assertion failure\n");
48 bool testb=checktype(true,ptr,s);
52 return assertvalidmemory(ptr, high);
55 bool typemap::assertvalidmemory(void* low, void* high) {
57 return checkmemory(low, high);
62 bool typemap::istype(void *ptr, void *high, int s) {
64 bool b=checktype(false,ptr,s);
66 printf("Verify failure\n");
67 bool testb=checktype(false,ptr,s);
71 return assertvalidmemory(ptr, high);
74 void typemap::allocate(void *ptr, int size) {
76 void *high=((char *)ptr)+size;
77 int val=rbinsert(low,high,NULL,alloctree);
82 int typemap::findoffsetstructure(int s, int offset) {
84 for(int i=0;i<size->getnumfields(s);i++) {
86 int ttype=size->getfield(s,i);
87 if (size->isArray(s,i)) {
88 mult=size->numElements(s,i);
90 int increment=size->size(ttype);
91 int delt=offset-count;
92 if (delt<mult*increment) {
93 if (delt%increment==0) {
98 count+=mult*increment;
103 void typemap::deallocate(void *ptr) {
104 if (rbdelete(ptr,alloctree)==NULL)
105 printf("Freeing unallocated memory\n");
108 bool typemap::checkmemory(void* low, void* high) {
109 struct pair allocp=rbfind(low,high,alloctree);
110 if (allocp.low == NULL) {
112 } else if ((allocp.low > low) || (allocp.high < high)) { /* make sure this block is used */
120 bool typemap::checktype(bool doaction,void *ptr, int structure) {
121 int ssize=size->size(structure);
123 void *high=((char *)low)+ssize;
124 struct pair allocp=rbfind(low,high,alloctree);
125 if (allocp.low==NULL)
127 if (allocp.low>low||allocp.high<high) /* make sure this block is used */
129 struct pair typep=rbfind(low,high,typetree);
130 structuremap *smap=(structuremap *)rblookup(low,high,typetree);
131 if (typep.low==NULL) {
134 structuremap *sm=new structuremap(structure);
135 int flag=rbinsert(low, high, sm, typetree);
137 printf("Error in asserttype\n");
142 return checktype(doaction, low,high, structure, typetree);
145 bool typemap::checktype(bool doaction, void *low, void *high, int structure, struct rbtree *ttree) {
146 struct pair typep=rbfind(low,high,ttree);
147 structuremap *smap=(structuremap *)rblookup(low,high,ttree);
148 if (typep.low==low&&typep.high==high) {
150 if (size->issubtype(structure,smap->str)) {
156 } else if (size->issubtype(smap->str,structure)) {
160 return false; /* incompatible types */
161 } else if (typep.low<=low&&typep.high>=high) {
162 /* See if it matches up with structure inside typep */
163 if (rbsearch(low,high,smap->typetree)) {
165 return checktype(doaction,low,high, structure, smap->typetree);
167 /* check to see if data lines up correctly */
168 int offset=((char *)low)-((char *)typep.low);
169 int st=findoffsetstructure(smap->str,offset);
172 if (size->issubtype(structure,st)) {
175 structuremap *newsm=new structuremap(structure);
176 int flag=rbinsert(low, high, newsm, smap->typetree);
178 } else if (size->issubtype(st,structure)) {
181 structuremap *newsm=new structuremap(st);
182 int flag=rbinsert(low, high, newsm, smap->typetree);