00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include<Allocator.h>
00017 #include<Database.h>
00018 #include<os.h>
00019 #include<Debug.h>
00020 #include<Config.h>
00021
00022
00023 ChunkIterator Chunk::getIterator()
00024 {
00025 ChunkIterator iter;
00026 iter.chunkID_ = chunkID_;
00027 iter.allocSize_ = allocSize_;
00028 iter.allocType_ = allocType_;
00029 iter.iterPage_ = firstPage_;
00030 iter.nodeOffset_ = 0;
00031 iter.noOfNodes_ = os::floor((PAGE_SIZE - sizeof(PageInfo)) / allocSize_);
00032 return iter;
00033 }
00034
00035 void* ChunkIterator::nextElement()
00036 {
00037 if(NULL == iterPage_)
00038 {
00039 printError(ErrNotExists,"No iter page exists.");
00040 return NULL;
00041 }
00042
00043 if(0 == allocSize_)
00044 {
00045 printError(ErrNotExists,"Iterators are not for variable size allocators");
00046 return NULL;
00047 }
00048 PageInfo* pageInfo = (PageInfo*)iterPage_;
00049 if (0 == noOfNodes_)
00050 {
00051
00052 iterPage_ = pageInfo->nextPage_;
00053 return (char*)pageInfo + sizeof(PageInfo)+ sizeof(int);
00054 }
00055
00056 char *data = ((char*)iterPage_) + sizeof(PageInfo) + (nodeOffset_ * allocSize_) ;
00057
00058
00059 int i = nodeOffset_;
00060 while(nodeOffset_ < noOfNodes_)
00061 {
00062 if (*((int*)data) == 0)
00063 {
00064
00065 data = data + allocSize_;
00066 nodeOffset_++;
00067 if (data >= (char*)iterPage_ + PAGE_SIZE) break;
00068 }
00069 else
00070 {
00071
00072 nodeOffset_++;
00073 printDebug(DM_Iterator,"ChunkID:%d Returning %x nodeOffset:%d",
00074 chunkID_, data + sizeof(int), nodeOffset_);
00075 return data + sizeof(int);
00076 }
00077 }
00078
00079 while(pageInfo->nextPage_ != NULL)
00080 {
00081 iterPage_ = pageInfo->nextPage_;
00082 pageInfo = ((PageInfo*)iterPage_);
00083 data = (char*)iterPage_ + sizeof(PageInfo);
00084 nodeOffset_ = 0;
00085 while(nodeOffset_ < noOfNodes_)
00086 {
00087 if (*((int*)data) == 0)
00088 {
00089
00090 data = data + allocSize_;
00091 nodeOffset_++;
00092 }
00093 else
00094 {
00095 nodeOffset_++;
00096 printDebug(DM_Iterator,"ChunkID:%d Returning %x Page:%x nodeOffset:%d",
00097 chunkID_, data + sizeof(int), pageInfo, nodeOffset_);
00098 return data +sizeof(int);
00099 }
00100 }
00101 }
00102 return NULL;
00103 }