avoid charm.h in converse code, use converse.h instead
[charm.git] / src / util / pup_paged.C
1 #include "converse.h"
2 #include "pup_paged.h"
3
4
5
6 pup_pagetable *getNewPagetable(char *fName){
7         pup_pagetable *_pagetable = new pup_pagetable;
8         _pagetable->freelist = NULL;
9         _pagetable->table = NULL;
10         _pagetable->tailtable = NULL;
11         _pagetable->maxblk=0;
12         _pagetable->fName = new char[strlen(fName)+20];
13         sprintf(_pagetable->fName,"%s_%d.dat",fName,CmiMyPe());
14         _pagetable->fp = fopen(_pagetable->fName,"wb");
15         fclose(_pagetable->fp);
16         _pagetable->fp = fopen(_pagetable->fName,"r+b");
17         
18         return _pagetable;
19 }
20
21 void PUP_toPagedDisk::addpageentry(){
22         entry = new pup_pageentry;
23         entry->next = NULL;
24         entry->ptr = handle;
25         entry->blklist = NULL;
26         tailblklist = NULL;
27         if(_pagetable->tailtable == NULL){
28                 _pagetable->table = entry;
29         }else{
30                 _pagetable->tailtable->next = entry;
31         }
32         _pagetable->tailtable = entry;
33 }
34
35 void PUP_toPagedDisk::nextblock(){
36         pup_list *f;
37         f = _pagetable->freelist;
38         if(f != NULL){
39                 current_block =  f->n;
40                 _pagetable->freelist=f->next;
41                 delete f;
42         }else{
43                 current_block = _pagetable->maxblk;
44                 _pagetable->maxblk = current_block+1;
45         }
46         pup_list *newblk = new pup_list;
47         newblk->n = current_block;
48         newblk->next = NULL;
49         if(tailblklist == NULL){
50                 entry->blklist = newblk;
51         }else{
52                 tailblklist->next = newblk;
53         }
54         tailblklist = newblk;
55         bytes_left = PUP_BLOCK;
56 }
57
58
59
60 void PUP_toPagedDisk::bytes(void *p,int n,size_t itemSize,PUP::dataType) {
61         long size = itemSize*n;
62         char *c = (char *)p;
63         while(size > bytes_left){
64                 long next=current_block*PUP_BLOCK + PUP_BLOCK - bytes_left;
65                 fseek(fp,next,SEEK_SET);
66                 fwrite(c,1,bytes_left,fp);
67                 size -= bytes_left;
68                 c += bytes_left;
69                 bytes_left = 0;
70                 nextblock();
71         }
72         long next=current_block*PUP_BLOCK + PUP_BLOCK - bytes_left;
73         fseek(fp,next,SEEK_SET);
74         fwrite(c,1,size,fp);
75         bytes_left -= size;
76 }
77
78
79
80 void PUP_fromPagedDisk::findpageentry(){
81         pup_pageentry *p;
82         p = NULL;
83         entry = _pagetable->table;
84         while(entry != NULL && entry->ptr != handle){
85                 p = entry;
86                 entry = entry->next;
87         }
88         if( p == NULL){
89                 _pagetable->table = entry->next;
90         }else{
91                 p->next = entry->next;
92         }
93         if(_pagetable->tailtable == entry){
94                 _pagetable->tailtable = p;
95         }
96 }
97
98 void PUP_fromPagedDisk::nextblock(){
99         if(current_block != -1){
100         // add blocks to the free list;
101                 pup_list *freenode = new pup_list;
102                 freenode->n = current_block;
103                 freenode->next = NULL;
104                 if(_pagetable->freelist == NULL){
105                         _pagetable->freelist = freenode;
106                         _pagetable->tailfreelist = freenode;
107                 }else{
108                         _pagetable->tailfreelist->next = freenode;
109                         _pagetable->tailfreelist = freenode;
110                 }
111         }
112         if(entry->blklist != NULL){
113                 current_block = entry->blklist->n;
114                 entry->blklist = entry->blklist->next;
115         }
116         bytes_unread = PUP_BLOCK;
117 }
118
119 void PUP_fromPagedDisk::bytes(void *p,int n,size_t itemSize,PUP::dataType ){
120         long size = n*itemSize;
121         char *c = (char *)p;
122         while(size > bytes_unread){
123                 long next = current_block*PUP_BLOCK + PUP_BLOCK - bytes_unread;
124                 fseek(fp,next,SEEK_SET);
125                 fread(c,1,bytes_unread,fp);
126                 size -= bytes_unread;
127                 c += bytes_unread;
128                 bytes_unread = 0;
129                 nextblock();
130         }
131         long next = current_block*PUP_BLOCK + PUP_BLOCK - bytes_unread;
132         fseek(fp,next,SEEK_SET);
133         fread(c,1,size,fp);
134         bytes_unread -= size;
135 }
136