00001 #include "converse.h"
00002 #include "pup_paged.h"
00003
00004
00005
00006 pup_pagetable *getNewPagetable(char *fName){
00007 pup_pagetable *_pagetable = new pup_pagetable;
00008 _pagetable->freelist = NULL;
00009 _pagetable->table = NULL;
00010 _pagetable->tailtable = NULL;
00011 _pagetable->maxblk=0;
00012 _pagetable->fName = new char[strlen(fName)+20];
00013 sprintf(_pagetable->fName,"%s_%d.dat",fName,CmiMyPe());
00014 _pagetable->fp = fopen(_pagetable->fName,"wb");
00015 fclose(_pagetable->fp);
00016 _pagetable->fp = fopen(_pagetable->fName,"r+b");
00017
00018 return _pagetable;
00019 }
00020
00021 void PUP_toPagedDisk::addpageentry(){
00022 entry = new pup_pageentry;
00023 entry->next = NULL;
00024 entry->ptr = handle;
00025 entry->blklist = NULL;
00026 tailblklist = NULL;
00027 if(_pagetable->tailtable == NULL){
00028 _pagetable->table = entry;
00029 }else{
00030 _pagetable->tailtable->next = entry;
00031 }
00032 _pagetable->tailtable = entry;
00033 }
00034
00035 void PUP_toPagedDisk::nextblock(){
00036 pup_list *f;
00037 f = _pagetable->freelist;
00038 if(f != NULL){
00039 current_block = f->n;
00040 _pagetable->freelist=f->next;
00041 delete f;
00042 }else{
00043 current_block = _pagetable->maxblk;
00044 _pagetable->maxblk = current_block+1;
00045 }
00046 pup_list *newblk = new pup_list;
00047 newblk->n = current_block;
00048 newblk->next = NULL;
00049 if(tailblklist == NULL){
00050 entry->blklist = newblk;
00051 }else{
00052 tailblklist->next = newblk;
00053 }
00054 tailblklist = newblk;
00055 bytes_left = PUP_BLOCK;
00056 }
00057
00058
00059
00060 void PUP_toPagedDisk::bytes(void *p,size_t n,size_t itemSize,PUP::dataType) {
00061 long size = itemSize*n;
00062 char *c = (char *)p;
00063 while(size > bytes_left){
00064 long next=current_block*PUP_BLOCK + PUP_BLOCK - bytes_left;
00065 fseek(fp,next,SEEK_SET);
00066 fwrite(c,1,bytes_left,fp);
00067 size -= bytes_left;
00068 c += bytes_left;
00069 bytes_left = 0;
00070 nextblock();
00071 }
00072 long next=current_block*PUP_BLOCK + PUP_BLOCK - bytes_left;
00073 fseek(fp,next,SEEK_SET);
00074 fwrite(c,1,size,fp);
00075 bytes_left -= size;
00076 }
00077
00078
00079
00080 void PUP_fromPagedDisk::findpageentry(){
00081 pup_pageentry *p;
00082 p = NULL;
00083 entry = _pagetable->table;
00084 while(entry != NULL && entry->ptr != handle){
00085 p = entry;
00086 entry = entry->next;
00087 }
00088 if( p == NULL){
00089 _pagetable->table = entry->next;
00090 }else{
00091 p->next = entry->next;
00092 }
00093 if(_pagetable->tailtable == entry){
00094 _pagetable->tailtable = p;
00095 }
00096 }
00097
00098 void PUP_fromPagedDisk::nextblock(){
00099 if(current_block != -1){
00100
00101 pup_list *freenode = new pup_list;
00102 freenode->n = current_block;
00103 freenode->next = NULL;
00104 if(_pagetable->freelist == NULL){
00105 _pagetable->freelist = freenode;
00106 _pagetable->tailfreelist = freenode;
00107 }else{
00108 _pagetable->tailfreelist->next = freenode;
00109 _pagetable->tailfreelist = freenode;
00110 }
00111 }
00112 if(entry->blklist != NULL){
00113 current_block = entry->blklist->n;
00114 entry->blklist = entry->blklist->next;
00115 }
00116 bytes_unread = PUP_BLOCK;
00117 }
00118
00119 void PUP_fromPagedDisk::bytes(void *p,size_t n,size_t itemSize,PUP::dataType ){
00120 long size = n*itemSize;
00121 char *c = (char *)p;
00122 while(size > bytes_unread){
00123 long next = current_block*PUP_BLOCK + PUP_BLOCK - bytes_unread;
00124 fseek(fp,next,SEEK_SET);
00125 if (fread(c,1,bytes_unread,fp) != bytes_unread) {
00126 CmiAbort("PUP> reading bytes from disk failed!");
00127 }
00128 size -= bytes_unread;
00129 c += bytes_unread;
00130 bytes_unread = 0;
00131 nextblock();
00132 }
00133 long next = current_block*PUP_BLOCK + PUP_BLOCK - bytes_unread;
00134 fseek(fp,next,SEEK_SET);
00135 if (fread(c,1,size,fp) != size) {
00136 CmiAbort("PUP> reading bytes from disk failed!");
00137 }
00138 bytes_unread -= size;
00139 }
00140